Jason Tan
AI Engineer
Backend Engineer
ML Engineer
Google Gemini
OpenAI
Python
Computer Software
pip install llm-costs
uv add llm-costs
from llm_costs import calculate_cost, get_model_pricing# Calculate cost from LangChain UsageMetadata structureresult = calculate_cost( provider="anthropic", model="claude-sonnet-4-20250514", usage={ "input_tokens": 1000, "output_tokens": 500, "total_tokens": 1500, },)print(f"Cost: ${result['cost']:.6f}") # Cost: $0.010500print(f"Input: ${result['breakdown']['input_cost']:.6f}")print(f"Output: ${result['breakdown']['output_cost']:.6f}")
result = calculate_cost( provider="anthropic", model="claude-sonnet-4-20250514", usage={ "input_tokens": 1000, "output_tokens": 500, "total_tokens": 1500, "input_token_details": { "cache_read": 5000, "cache_creation": 0, }, },)
result = calculate_cost( provider="anthropic", model="claude-sonnet-4-20250514", usage={"input_tokens": 1000, "output_tokens": 500, "total_tokens": 1500}, batch=True, # 50% discount)
pricing = get_model_pricing("anthropic", "claude-sonnet-4-20250514")print(f"Input: ${pricing['input']}/MTok") # Input: $3.0/MTokprint(f"Output: ${pricing['output']}/MTok") # Output: $15.0/MTok
from llm_costs.calculator import list_models, list_providersproviders = list_providers() # ['anthropic', 'openai', 'google']models = list_models("anthropic") # ['claude-opus-4-5-20251101', ...]
UsageMetadata
{ "input_tokens": int, "output_tokens": int, "total_tokens": int, "input_token_details": { "cache_read": int, # Cached tokens read "cache_creation": int, # Tokens written to cache }, "output_token_details": { "reasoning": int, # Reasoning tokens (o1/thinking models) },}
calculate_cost()
CostResult
{ "cost": 0.0105, # Total cost in USD "currency": "USD", "breakdown": { "input_cost": 0.003, "output_cost": 0.0075, "cache_read_cost": 0.0, # If applicable "cache_creation_cost": 0.0, # If applicable }, "pricing_used": { "input_per_mtok": 3.0, "output_per_mtok": 15.0, "batch_applied": False, "long_context_applied": False, },}
Posted Sep 26, 2026
Open-source Python lib for accurate LLM cost across Anthropic, OpenAI & Google — cache, batch & long-context aware. On PyPI, used in production.
0
1
AI Systems & Agent-Infrastructure Engineer