Calculate costs for LLM usage based on token counts using LiteLLM's pricing data.
pip install tokonomics
- Automatic cost calculation for various LLM models
- Detailed cost breakdown (prompt, completion, and total costs)
- Caches pricing data locally (24-hour default cache duration)
- Supports multiple model name formats (e.g., "gpt-4", "openai:gpt-4")
- Asynchronous API
- Fully typed with runtime type checking
- Zero configuration required
import asyncio
from tokonomics import calculate_token_cost
async def main():
# Calculate cost with token counts
costs = await calculate_token_cost(
model="gpt-4",
prompt_tokens=100, # tokens used in the prompt
completion_tokens=50, # tokens used in the completion
)
if costs:
print(f"Prompt cost: ${costs.prompt_cost:.6f}")
print(f"Completion cost: ${costs.completion_cost:.6f}")
print(f"Total cost: ${costs.total_cost:.6f}")
else:
print("Could not determine cost for model")
asyncio.run(main())
You can customize the cache timeout:
from tokonomics import get_model_costs, clear_cache
# Get model costs with custom cache duration (e.g., 1 hour)
costs = await get_model_costs("gpt-4", cache_timeout=3600)
if costs:
print(f"Input cost per token: ${costs['input_cost_per_token']}")
print(f"Output cost per token: ${costs['output_cost_per_token']}")
clear_cache()
You can retrieve the token limits for a model using get_model_limits
:
from tokonomics import get_model_limits
async def main():
# Get token limit information for a model
limits = await get_model_limits("gpt-4")
if limits:
print(f"Maximum total tokens: {limits.total_tokens}")
print(f"Maximum input tokens: {limits.input_tokens}")
print(f"Maximum output tokens: {limits.output_tokens}")
else:
print("Could not find limit data for model")
The function returns a TokenLimits
object with three fields:
total_tokens
: Maximum combined tokens (input + output) the model supportsinput_tokens
: Maximum number of input/prompt tokensoutput_tokens
: Maximum number of output/completion tokens
If you're using pydantic-ai, you can directly calculate costs from its Usage objects:
from tokonomics import calculate_pydantic_cost
# Assuming you have a pydantic-ai Usage object
costs = await calculate_pydantic_cost(
model="gpt-4",
usage=usage_object,
)
if costs:
print(f"Prompt cost: ${costs.prompt_cost:.6f}")
print(f"Completion cost: ${costs.completion_cost:.6f}")
print(f"Total cost: ${costs.total_cost:.6f}")
The library supports multiple formats for model names:
- Direct model names:
"gpt-4"
- Provider-prefixed:
"openai:gpt-4"
- Provider-path style:
"openai/gpt-4"
Names are matched case-insensitively.
Pricing data is sourced from LiteLLM's pricing repository and is automatically cached locally using hishel
. The cache is updated when pricing data is not found or has expired.
- Python 3.12+
httpx
platformdirs
upath
pydantic
(≥ 2.0)
This project is licensed under the MIT License - see the LICENSE file for details.