tokencost
TokenCost is a Python library that calculates USD costs for API calls to 400+ LLM models by counting tokens in prompts and completions. It uses OpenAI's Tiktoken for most models and Anthropic's official token counting API for Claude 3+ models, enabling developers to estimate LLM expenses before or after API calls.
Key facts
Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.
| Field | Value |
|---|---|
| Repository | AgentOps-AI/tokencost |
| Owner | AgentOps-AI |
| Primary language | Python |
| License | MIT — OSI-approved |
| Stars | 2k |
| Forks | 107 |
| Open issues | 28 |
| Latest release | 0.1.26 (2025-08-13) |
| Last updated | 2025-09-05 |
| Source | https://github.com/AgentOps-AI/tokencost |
What tokencost is
Python package providing clientside token counting and cost estimation for LLM APIs. Leverages Tiktoken for OpenAI/general tokenization and Anthropic's beta token counting API for Claude 3+ models. Supports message-formatted prompts and raw strings with configurable model pricing data covering 400+ LLM variants.
Get the tokencost source
Clone the repository and explore it locally.
git clone https://github.com/AgentOps-AI/tokencost.gitcd tokencost# follow the project's README for install & configurationNeed it deployed, integrated, or customized instead? DEV.co ships production installs.
Best use cases
Implementation considerations
- Requires Tiktoken and Anthropic SDK dependencies; verify compatibility with your Python version and LLM provider SDKs.
- Pricing data is hardcoded and requires manual updates when providers change pricing; monitor GitHub releases or subscribe to notifications.
- Token counting for Claude 3+ relies on Anthropic's beta API, which may change; fallback to Tiktoken approximation is available but less accurate.
- No built-in caching of token counts; implement memoization if counting the same prompts repeatedly to avoid redundant computation.
- Output is cost in USD only; multi-currency support would require wrapper logic if needed.
When to avoid it — and what to weigh
- Real-time pricing accuracy critical — Pricing table is manual and may lag behind provider updates. Not suitable for systems requiring guaranteed real-time pricing alignment.
- Exotic or newly launched models — Coverage is broad (400+ models) but pricing data must be maintained; newly released models may not be present until maintainers update the pricing table.
- Non-token-based pricing models — Does not support usage-based pricing outside tokens (e.g., per-image costs, seat-based licensing) or custom enterprise pricing agreements.
- Strict accuracy requirements for billing — Estimates may diverge slightly from actual provider token counts (especially for older Claude models using Tiktoken approximation); not authoritative for legal/contract billing.
License & commercial use
Licensed under MIT (Massachusetts Institute of Technology License). This is a permissive OSI-approved license allowing commercial use, modification, and redistribution with attribution and no warranty.
MIT license explicitly permits commercial use. You may use TokenCost in closed-source products, SaaS applications, and for-profit systems. Attribution is required in source form or derivative notices. No patent protection is granted by the license itself; review Anthropic/OpenAI terms for their token counting APIs separately.
DEV.co evaluation signals
Editorial assessment — not user reviews. Directional, with an explicit confidence level.
| Signal | Assessment |
|---|---|
| Maintenance | Active |
| Documentation | Adequate |
| License clarity | Clear |
| Deployment complexity | Low |
| DEV.co fit | Strong |
| Assessment confidence | High |
No encryption or authentication built into the library. Cost calculations are deterministic and depend on public pricing data. When integrated with LLM SDKs, ensure API keys are managed securely per your provider's best practices. Token counting for Claude 3+ uses Anthropic's beta API; verify your Anthropic credentials are protected. The library itself does not store or log sensitive data.
Alternatives to consider
LiteLLM cost tracking
Integrated cost tracking within LiteLLM's unified LLM interface; useful if you are already using LiteLLM for provider abstraction and want cost as a side-effect.
OpenAI Tokenizer (official)
OpenAI's native tokenizer via tiktoken CLI or SDK; sufficient for OpenAI models alone but does not cover Anthropic, Gemini, or other providers.
Provider cost APIs (OpenAI/Anthropic official)
Direct API calls to OpenAI's usage endpoint or Anthropic's token counting beta; eliminates version skew but adds network latency and authentication overhead.
Build on tokencost with DEV.co software developers
Use TokenCost to estimate and track API costs in real-time. Install via pip and add cost visibility to your AI agents and LLM workflows in minutes.
Talk to DEV.coRelated open-source tools
Surfaced by semantic similarity across the DEV.co open-source index.
Related on DEV.co
Explore the category and the services that help you build with it.
tokencost FAQ
How often is the pricing table updated?
Does TokenCost charge a fee or require authentication?
Can I use TokenCost with proprietary or self-hosted models?
What is the accuracy of token counts?
Work with a software development agency
DEV.co helps companies turn open-source tools like tokencost into production software. Our software development services cover the full lifecycle — architecture, web development, integration, and maintenance — delivered by software developers and web developers who ship. Engage our software development agency to implement or customize it for your open-source observability stack.
Integrate cost tracking into your LLM applications
Use TokenCost to estimate and track API costs in real-time. Install via pip and add cost visibility to your AI agents and LLM workflows in minutes.