ScriptsApr 9, 2026·2 min read

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.

TL;DR
Python library for client-side token counting and USD cost estimation across 400+ LLM models.
§01

What it is

TokenCost is a Python library that counts tokens and estimates USD costs for over 400 LLM models. In three lines of code, you can calculate the cost of any prompt or completion across OpenAI, Anthropic, Mistral, AWS Bedrock, and other providers. The library runs entirely client-side, using local tokenizers to count tokens without sending data to any API.

Developers building LLM applications who need to track costs, set budgets, or compare pricing across providers benefit from TokenCost. It is especially useful for applications that route between multiple models and need real-time cost visibility.

§02

How it saves time or tokens

TokenCost eliminates the need to manually look up pricing pages and count tokens for each provider. Instead of maintaining a spreadsheet of model prices, you call one function and get the USD cost. This saves time during development and enables automated cost monitoring in production. Pre-checking prompt costs before sending them to the API prevents budget overruns.

§03

How to use

  1. Install TokenCost via pip
  2. Import the cost calculation functions
  3. Pass your prompt text and model name to get the USD cost
§04

Example

from tokencost import calculate_prompt_cost, calculate_completion_cost

# Calculate prompt cost
prompt = 'Explain quantum computing in simple terms.'
cost = calculate_prompt_cost(prompt, model='gpt-4o')
print(f'Prompt cost: ${cost:.6f}')

# Calculate completion cost
completion = 'Quantum computing uses qubits...'
cost = calculate_completion_cost(completion, model='gpt-4o')
print(f'Completion cost: ${cost:.6f}')

# Compare across models
for model in ['gpt-4o', 'claude-sonnet-4-20250514', 'mistral-large']:
    c = calculate_prompt_cost(prompt, model=model)
    print(f'{model}: ${c:.6f}')
§05

Related on TokRepo

§06

Common pitfalls

  • Model prices change frequently; update TokenCost regularly to keep the pricing database current
  • Token counts are estimates based on local tokenizers; actual API billing may differ slightly due to special tokens
  • Some models use different tokenizers; ensure TokenCost supports your specific model variant for accurate counts

Frequently Asked Questions

Which LLM providers does TokenCost support?+

TokenCost supports 400+ models across OpenAI, Anthropic, Mistral, Cohere, AWS Bedrock, Google Vertex AI, and others. The pricing database is updated with each release to reflect current model prices.

Does TokenCost call any external APIs?+

No. TokenCost runs entirely client-side. It uses local tokenizers to count tokens and a bundled pricing database to calculate costs. No data is sent to any external service.

How accurate are the cost estimates?+

TokenCost uses the same tokenizers as the providers (tiktoken for OpenAI, etc.) for accurate token counts. Pricing is based on published rates. Minor differences may occur due to special tokens or rounding.

Can I use TokenCost to set budget limits?+

Yes. You can pre-calculate the cost of a prompt before sending it to the API and reject requests that exceed a budget threshold. This is useful for preventing runaway costs in production applications.

Is TokenCost free?+

Yes. TokenCost is open-source under the MIT license. There are no usage fees or restrictions. You can use it in commercial projects without any cost.

Citations (3)
🙏

Source & Thanks

Created by AgentOps-AI. Licensed under MIT.

tokencost — ⭐ 2,000+

Thanks to the AgentOps team for making LLM cost tracking simple and accessible.

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets