ScriptsApr 9, 2026·2 min read

TokenCost — LLM Price Calculator for 400+ Models

Client-side token counting and USD cost estimation for 400+ LLMs. 3 lines of Python to track prompt and completion costs. Supports OpenAI, Anthropic, Mistral, AWS Bedrock. MIT, 2K+ stars.

SC
Script Depot · Community
Quick Use

Use it first, then decide how deep to go

This block should tell both the user and the agent what to copy, install, and apply first.

  1. Install:
pip install tokencost
  1. Calculate costs in 3 lines:
from tokencost import calculate_prompt_cost, calculate_completion_cost

cost = calculate_prompt_cost("Hello world", "gpt-4o")
print(f"Prompt cost: ${cost}")

Intro

TokenCost is a client-side token counting and price estimation library for 400+ LLM models with 2,000+ GitHub stars. It calculates the exact USD cost of prompts and completions using tiktoken, supporting OpenAI, Anthropic Claude, Google Gemini, Mistral, DeepSeek, Groq, and AWS Bedrock models. Perfect for AI agent developers who need to track and optimize API spending. Setup: pip install tokencost, 3 lines of code to get cost estimates.

See also: AI developer scripts on TokRepo.


TokenCost — Know What Your AI Costs

The Problem

LLM API costs add up fast. Different models have different pricing. Tracking costs across multiple providers and models is a pain — you often don't know the bill until it arrives.

The Solution

TokenCost gives you real-time, client-side cost estimation before and after API calls. No server needed, no API keys required for cost calculation.

Supported Providers (400+ Models)

Provider Models
OpenAI GPT-4o, GPT-4, GPT-3.5-turbo, o1, o3, etc.
Anthropic Claude Opus, Sonnet, Haiku (all versions)
Google Gemini Pro, Flash, Ultra
Mistral Mistral Large, Medium, Small
DeepSeek DeepSeek Chat, Coder
Groq Llama, Mixtral on Groq
AWS Bedrock All Bedrock model pricing

Usage Examples

from tokencost import calculate_prompt_cost, calculate_completion_cost

# Simple string cost
model = "claude-sonnet-4-20250514"
prompt_cost = calculate_prompt_cost("Explain quantum computing", model)
print(f"Prompt: ${prompt_cost}")

# Chat message format
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Write a haiku about coding"}
]
cost = calculate_prompt_cost(messages, "gpt-4o")
print(f"Conversation cost: ${cost}")

# Completion cost
completion = "Here is a haiku about coding:\nLines of logic flow\nBugs hide in the shadows deep\nTests bring peace of mind"
comp_cost = calculate_completion_cost(completion, "gpt-4o")
print(f"Completion: ${comp_cost}")

# Total cost
total = calculate_prompt_cost(messages, "gpt-4o") + calculate_completion_cost(completion, "gpt-4o")
print(f"Total: ${total}")

Integration with AI Agents

# Track agent costs over time
from tokencost import calculate_prompt_cost, calculate_completion_cost

total_cost = 0.0

def track_cost(prompt, completion, model):
    global total_cost
    cost = (calculate_prompt_cost(prompt, model) +
            calculate_completion_cost(completion, model))
    total_cost += cost
    return cost

# After each agent step
step_cost = track_cost(user_msg, agent_response, "claude-sonnet-4-20250514")
print(f"Step cost: ${step_cost:.6f} | Running total: ${total_cost:.4f}")

FAQ

Q: What is TokenCost? A: A Python library for client-side token counting and USD cost estimation across 400+ LLM models from OpenAI, Anthropic, Google, Mistral, and more.

Q: Is TokenCost free? A: Yes, fully open-source under the MIT license. No API keys needed for cost calculation.

Q: How accurate is the cost estimation? A: TokenCost uses tiktoken for token counting and regularly updated pricing data. Costs are estimates — actual billing may vary slightly due to provider-specific tokenization differences.


🙏

Source & Thanks

Created by AgentOps-AI. Licensed under MIT.

tokencost — ⭐ 2,000+

Thanks to the AgentOps team for making LLM cost tracking simple and accessible.

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets