skillfed

tokencost

To calculate token and translated USD cost of string and message calls to OpenAI, for example when used by AI agents

tokencost v0.1.26 396.1K downloads/30d#6,971 on PyPI2,003
Permissive license AGING released

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 tokencost

uv

uv add tokencost

poetry

poetry add tokencost

Installing 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

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

llm token countingopenai cost estimationprompt pricing calculatorai api cost trackingtoken counter for llmsanthropic token countingllm billing estimation
llm-cost-trackingtoken-countingapi-pricing

More Artificial Intelligence packages