tokencost logo

tokencost

Free

Token counting & cost lookup

FreeFree tier
Inputs: text
Type
Open Source
Company
AgentOps-AI

About tokencost

Tokencost is an open-source Python library that provides client-side token counting and cost estimation for over 400 large language models (LLMs). It calculates the USD cost of prompts and completions by tokenizing strings and message lists using Tiktoken for OpenAI models and Anthropic's tokenizer for Claude models. The library offers simple functions like calculate_prompt_cost, calculate_completion_cost, count_message_tokens, and count_string_tokens, making it easy to integrate cost tracking into LLM applications and AI agents. It is maintained by AgentOps-AI and is available via pip install.

Key Features

LLM price tracking for major providers (OpenAI, Anthropic, etc.)
Accurate token counting using Tiktoken for OpenAI models
Simple API with functions: calculate_prompt_cost, calculate_completion_cost, count_message_tokens, count_string_tokens
Supports 400+ LLMs with up-to-date pricing data
Easy integration with OpenAI Python client and string-based prompts
Open-source and free to use

Pros & Cons

Pros
  • Open-source and free with no server dependencies
  • Supports a wide range of LLMs (400+) with automatic pricing updates
  • Client-side calculation protects sensitive prompt data
  • Lightweight and easy to install via pip
  • Active community and maintained by AgentOps-AI
Cons
  • Cost estimates may be approximate and need periodic updates for price changes
  • Limited to models with publicly listed pricing; custom models not supported
  • Token counting accuracy depends on underlying tokenizer (Tiktoken for OpenAI)

Best For

Estimating API costs for LLM-based applicationsBudgeting and monitoring token usage in AI agent workflowsPre-computing prompt costs before sending requests to OpenAIToken counting for context window managementIntegrating cost tracking into CI/CD pipelines for LLM apps

FAQ

What is tokencost?
Tokencost is an open-source Python library that estimates the USD cost of using major LLM APIs by calculating the cost of prompts and completions. It supports over 400 models.
How do I install tokencost?
Install via pip: pip install tokencost
How do I calculate the cost of a prompt?
Use calculate_prompt_cost(prompt, model) where prompt can be a string or a list of messages.
How does tokencost count tokens?
For OpenAI models, it uses Tiktoken. For Anthropic models above version 3, it uses the Anthropic beta tokenizer.
Which models are supported?
Tokencost supports over 400 LLMs including OpenAI (GPT-4, GPT-3.5), Anthropic (Claude 3, 3.5, Opus), and others.