diff --git a/README.md b/README.md index 6d2a502..e763772 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ For Anthropic models above version 3 (i.e. Sonnet 3.5, Haiku 3.5, and Opus 3), w Units denominated in USD. All prices can be located in `model_prices.json`. -* Prices last updated Jan 30, 2024 from [LiteLLM's cost dictionary](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json) +* Prices last updated Jan 30, 2024 from [OpenRouter's model list](https://openrouter.ai/docs/api-reference/list-available-models). Use the optional `OPENROUTER_API_KEY` environment variable if authentication is needed when refreshing prices programmatically. | Model Name | Prompt Cost (USD) per 1M tokens | Completion Cost (USD) per 1M tokens | Max Prompt Tokens | Max Output Tokens | |:----------------------------------------------------------------------|:----------------------------------|:--------------------------------------|:--------------------|--------------------:| diff --git a/pyproject.toml b/pyproject.toml index 9ce218b..8d88ed7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ tokencost = ["model_prices.json"] [project] name = "tokencost" -version = "0.1.23" +version = "0.1.24" authors = [ { name = "Trisha Pan", email = "trishaepan@gmail.com" }, { name = "Alex Reibman", email = "areibman@gmail.com" }, diff --git a/tokencost/constants.py b/tokencost/constants.py index 619ef5a..ec41941 100644 --- a/tokencost/constants.py +++ b/tokencost/constants.py @@ -24,17 +24,26 @@ # Each completion token costs __ USD per token. # Max prompt limit of each model is __ tokens. -PRICES_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json" +# Endpoint to fetch the latest model information from OpenRouter. +# ``OPENROUTER_API_KEY`` may optionally be set for authentication. The static +# ``model_prices.json`` data is used as a fallback when the request fails. +PRICES_URL = "https://openrouter.ai/api/v1/models" async def fetch_costs(): - """Fetch the latest token costs from the LiteLLM cost tracker asynchronously. + """Fetch the latest token costs from OpenRouter asynchronously. + Returns: dict: The token costs for each model. Raises: Exception: If the request fails. """ - async with aiohttp.ClientSession(trust_env=True) as session: + headers = {} + api_key = os.getenv("OPENROUTER_API_KEY") + if api_key: + headers["Authorization"] = f"Bearer {api_key}" + + async with aiohttp.ClientSession(headers=headers, trust_env=True) as session: async with session.get(PRICES_URL) as response: if response.status == 200: return await response.json(content_type=None) @@ -45,7 +54,7 @@ async def fetch_costs(): async def update_token_costs(): - """Update the TOKEN_COSTS dictionary with the latest costs from the LiteLLM cost tracker asynchronously.""" + """Update :data:`TOKEN_COSTS` with the latest prices from OpenRouter.""" global TOKEN_COSTS try: fetched_costs = await fetch_costs() diff --git a/update_prices.py b/update_prices.py index b2a44c0..9959081 100644 --- a/update_prices.py +++ b/update_prices.py @@ -4,7 +4,7 @@ import json import re -# Update model_prices.json with the latest costs from the LiteLLM cost tracker +# Update model_prices.json with the latest costs from OpenRouter print("Fetching latest prices...") tokencost.refresh_prices(write_file=False)