Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|:----------------------------------------------------------------------|:----------------------------------|:--------------------------------------|:--------------------|--------------------:|
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tokencost = ["model_prices.json"]

[project]
name = "tokencost"
version = "0.1.23"
version = "0.1.24"
authors = [
{ name = "Trisha Pan", email = "[email protected]" },
{ name = "Alex Reibman", email = "[email protected]" },
Expand Down
17 changes: 13 additions & 4 deletions tokencost/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion update_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down