DEV.co
Open-Source Observability · AgentOps-AI

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.

Source: GitHub — github.com/AgentOps-AI/tokencost
2k
GitHub stars
107
Forks
Python
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
RepositoryAgentOps-AI/tokencost
OwnerAgentOps-AI
Primary languagePython
LicenseMIT — OSI-approved
Stars2k
Forks107
Open issues28
Latest release0.1.26 (2025-08-13)
Last updated2025-09-05
Sourcehttps://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.

Quickstart

Get the tokencost source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/AgentOps-AI/tokencost.gitcd tokencost# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Cost tracking in AI agent systems

Integrate into agentic workflows to estimate token costs per API call before sending requests, enabling budget controls and cost attribution per agent action.

LLM API billing analysis

Post-hoc cost calculation for audit trails and billing reconciliation against actual provider invoices, supporting multi-model deployments.

Model selection and optimization

Compare cost/token tradeoffs across model variants (e.g., gpt-4o vs gpt-4o-mini) to inform deployment decisions without vendor APIs.

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.

SignalAssessment
MaintenanceActive
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitStrong
Assessment confidenceHigh
Security considerations

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.

Software development agency

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.co

Related 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?
Unknown. The README does not document update frequency. Monitor GitHub releases or the pricing_table.md file for changes. Pricing may lag behind provider announcements.
Does TokenCost charge a fee or require authentication?
No. TokenCost is free and open-source. Token counting is local (no API calls except Claude 3+ which uses Anthropic's API). You must authenticate with your LLM providers separately.
Can I use TokenCost with proprietary or self-hosted models?
Not directly. TokenCost includes pricing for 400+ known models. For custom models, you would need to extend the library with your own tokenizer and pricing data.
What is the accuracy of token counts?
OpenAI models: high accuracy via Tiktoken. Claude 3+: high accuracy via Anthropic beta API. Older Claude models: approximate using Tiktoken's cl100k_base encoding (may have minor variance).

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.