TokenCost — Know What Your AI Costs
The Problem
LLM API costs add up fast. Different models have different pricing. Tracking costs across multiple providers and models is a pain — you often don't know the bill until it arrives.
The Solution
TokenCost gives you real-time, client-side cost estimation before and after API calls. No server needed, no API keys required for cost calculation.
Supported Providers (400+ Models)
| Provider | Models |
|---|---|
| OpenAI | GPT-4o, GPT-4, GPT-3.5-turbo, o1, o3, etc. |
| Anthropic | Claude Opus, Sonnet, Haiku (all versions) |
| Gemini Pro, Flash, Ultra | |
| Mistral | Mistral Large, Medium, Small |
| DeepSeek | DeepSeek Chat, Coder |
| Groq | Llama, Mixtral on Groq |
| AWS Bedrock | All Bedrock model pricing |
Usage Examples
from tokencost import calculate_prompt_cost, calculate_completion_cost
# Simple string cost
model = "claude-sonnet-4-20250514"
prompt_cost = calculate_prompt_cost("Explain quantum computing", model)
print(f"Prompt: ${prompt_cost}")
# Chat message format
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a haiku about coding"}
]
cost = calculate_prompt_cost(messages, "gpt-4o")
print(f"Conversation cost: ${cost}")
# Completion cost
completion = "Here is a haiku about coding:\nLines of logic flow\nBugs hide in the shadows deep\nTests bring peace of mind"
comp_cost = calculate_completion_cost(completion, "gpt-4o")
print(f"Completion: ${comp_cost}")
# Total cost
total = calculate_prompt_cost(messages, "gpt-4o") + calculate_completion_cost(completion, "gpt-4o")
print(f"Total: ${total}")Integration with AI Agents
# Track agent costs over time
from tokencost import calculate_prompt_cost, calculate_completion_cost
total_cost = 0.0
def track_cost(prompt, completion, model):
global total_cost
cost = (calculate_prompt_cost(prompt, model) +
calculate_completion_cost(completion, model))
total_cost += cost
return cost
# After each agent step
step_cost = track_cost(user_msg, agent_response, "claude-sonnet-4-20250514")
print(f"Step cost: ${step_cost:.6f} | Running total: ${total_cost:.4f}")FAQ
Q: What is TokenCost? A: A Python library for client-side token counting and USD cost estimation across 400+ LLM models from OpenAI, Anthropic, Google, Mistral, and more.
Q: Is TokenCost free? A: Yes, fully open-source under the MIT license. No API keys needed for cost calculation.
Q: How accurate is the cost estimation? A: TokenCost uses tiktoken for token counting and regularly updated pricing data. Costs are estimates — actual billing may vary slightly due to provider-specific tokenization differences.