tokencost
To calculate token and translated USD cost of string and message calls to OpenAI, for example when used by AI agents
What it is and what it does
TokenCost is a client-side utility for estimating the USD cost of LLM API calls by counting tokens and applying current pricing. It wraps OpenAI's tiktoken tokenizer for accurate token counting on OpenAI and older Claude models, and uses Anthropic's beta token counting API for Claude 3 and later. The package maintains a pricing table for major LLM providers and exposes simple functions to calculate prompt and completion costs from either message-formatted lists or raw strings.
You use it to estimate costs before sending requests to an LLM API, or to calculate the cost of responses you've already received. It integrates with the OpenAI and Anthropic client libraries and is commonly used in AI agent frameworks to track spending. The package depends on tiktoken, aiohttp, and anthropic as runtime dependencies.
Use it for:
- Estimate the cost of a prompt before sending it to OpenAI or Anthropic to stay within budget.
- Track total token costs in an AI agent that makes multiple LLM calls across different models.
- Count tokens in a string or message list without making an API call, using tiktoken directly.
- Compare pricing across different LLM models to choose the most cost-effective option for a task.
- Log and report spending on LLM APIs in production applications.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Counts tokens and estimates USD costs for LLM API calls across OpenAI, Anthropic, and other major providers before or after requests are sent.
Yes, if you build with LLM APIs and need cost visibility before or after requests. The package is straightforward, permissively licensed, and has low install friction. However, be aware that maintenance is aging—pricing tables may lag behind provider updates, and support for newly released models is not guaranteed. Verify that the models you use are in the pricing table before relying on it for cost estimates.
Install
tokencost on PyPI
pip
pip install tokencostuv
uv add tokencostpoetry
poetry add tokencostInstalling tokencost
Before you install
Low friction install with three runtime dependencies (tiktoken, aiohttp, anthropic). Maintenance status is aging—last commit was 2025-09-05 and the package has not had a release since 2025-08-13, though the repository remains active and is not archived.
License in practice
Licensed under MIT (permissive), so you can use, modify, and distribute freely with minimal restrictions.
Quickstart
pip install tokencost
from tokencost import calculate_prompt_cost, calculate_completion_cost
model = "gpt-3.5-turbo"
prompt = [{"role": "user", "content": "Hello world"}]
completion = "How may I assist you today?"
prompt_cost = calculate_prompt_cost(prompt, model)
completion_cost = calculate_completion_cost(completion, model)
print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}")
Requires Python 3.10 or later.
Verify before relying
- Whether pricing table is automatically updated when providers change rates, or requires manual maintenance.
- Support status for models released after the latest 2025-08-13 release.
- Whether aiohttp is used for async cost calculation or is an indirect dependency.
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — tiktoken, aiohttp, anthropic |
| Maintenance | aging — 366 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 396,149/month — #6,971 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: tokencost-0.1.26-py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
openai-messages-token-helperEstimates token usage for OpenAI Chat…
permissive · top 15,000 on PyPI
genai-pricesCalculates pricing for LLM API calls across…
permissive · top 1,000 on PyPI
gptcacheGPTCache provides semantic caching for LLM API…
permissive · top 15,000 on PyPI
fhlmifhlmi provides a unified async Python interface…
permissive · top 15,000 on PyPI
headroom-aiCompresses LLM prompts and agent outputs before…
permissive · top 15,000 on PyPI
agentopsAgentOps provides observability and monitoring…
permissive · top 15,000 on PyPI
litellm-enterpriseLiteLLM Enterprise provides a unified Python…
unclear · top 5,000 on PyPI
unclecode-litellmUnified Python interface to call many LLM…
permissive · top 5,000 on PyPI