# TokenCost — LLM Price Calculator for 400+ Models > Client-side token counting and USD cost estimation for 400+ LLMs. 3 lines of Python to track prompt and completion costs. Supports OpenAI, Anthropic, Mistral, AWS Bedrock. MIT, 2K+ stars. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use 1. Install: ```bash pip install tokencost ``` 2. Calculate costs in 3 lines: ```python from tokencost import calculate_prompt_cost, calculate_completion_cost cost = calculate_prompt_cost("Hello world", "gpt-4o") print(f"Prompt cost: ${cost}") ``` --- ## Intro TokenCost is a client-side token counting and price estimation library for 400+ LLM models with 2,000+ GitHub stars. It calculates the exact USD cost of prompts and completions using tiktoken, supporting OpenAI, Anthropic Claude, Google Gemini, Mistral, DeepSeek, Groq, and AWS Bedrock models. Perfect for AI agent developers who need to track and optimize API spending. Setup: `pip install tokencost`, 3 lines of code to get cost estimates. See also: [AI developer scripts on TokRepo](https://tokrepo.com/en/@Script%20Depot). --- ## 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) | | Google | 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 ```python 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:\ Lines of logic flow\ Bugs hide in the shadows deep\ Tests 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 ```python # 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. --- ## Source & Thanks > Created by [AgentOps-AI](https://github.com/AgentOps-AI). Licensed under MIT. > > [tokencost](https://github.com/AgentOps-AI/tokencost) — ⭐ 2,000+ Thanks to the AgentOps team for making LLM cost tracking simple and accessible. --- ## Quick Use 1. Install: ```bash pip install tokencost ``` 2. Calculate cost in three lines: ```python from tokencost import calculate_prompt_cost, calculate_completion_cost cost = calculate_prompt_cost("Hello world", "gpt-4o") print(f"Cost: ${cost}") ``` --- ## Introduction TokenCost is a client-side token counting and dollar-cost estimation library supporting 400+ LLM models, with 2,000+ GitHub stars. It uses tiktoken to calculate precise prompt and completion costs across OpenAI, Anthropic Claude, Google Gemini, Mistral, DeepSeek, Groq, and AWS Bedrock. Ideal for AI agent developers who need to track and optimize API spend. --- ## TokenCost — Calculate Your AI Spend Precisely ### Supported Providers (400+ models) | Provider | Models | |----------|--------| | OpenAI | GPT-4o, GPT-4, o1, o3, and more | | Anthropic | Claude Opus, Sonnet, Haiku | | Google | Gemini Pro, Flash, Ultra | | Mistral | Large, Medium, Small | | DeepSeek | Chat, Coder | ### Usage Example ```python from tokencost import calculate_prompt_cost, calculate_completion_cost # Chat message format messages = [ {"role": "user", "content": "Write a haiku about programming"} ] cost = calculate_prompt_cost(messages, "gpt-4o") print(f"Conversation cost: ${cost}") ``` ### FAQ **Q: What is TokenCost?** A: A Python library for client-side token counting and dollar-cost estimation across 400+ LLM models. **Q: Is it free?** A: Completely free and open source under MIT. No API key required to calculate costs. --- ## Source & Thanks > Created by [AgentOps-AI](https://github.com/AgentOps-AI). Licensed under MIT. > > [tokencost](https://github.com/AgentOps-AI/tokencost) — ⭐ 2,000+ --- Source: https://tokrepo.com/en/workflows/tokencost-llm-price-calculator-400-models-43b26691 Author: Script Depot