tokencost

0.1.26 · active · verified Sun Apr 12

tokencost is a Python library that provides client-side token counting and USD cost estimation for over 400 Large Language Models (LLMs) from providers like OpenAI, Anthropic, Google, and AWS Bedrock. It helps AI agents and LLM applications accurately track and manage API expenses by offering up-to-date pricing and model-specific tokenization, released frequently to reflect changing LLM provider costs.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to calculate the estimated token cost for both a prompt and its completion using `tokencost`. It includes an optional interaction with the OpenAI API for a realistic example, which requires the `OPENAI_API_KEY` environment variable to be set. The library automatically handles different tokenization methods for various LLM providers.

import os
from openai import OpenAI
from tokencost import calculate_prompt_cost, calculate_completion_cost

# Ensure OPENAI_API_KEY is set in your environment variables
openai_api_key = os.environ.get('OPENAI_API_KEY', 'YOUR_OPENAI_API_KEY')
if not openai_api_key or openai_api_key == 'YOUR_OPENAI_API_KEY':
    print("Warning: OPENAI_API_KEY environment variable not set. Using dummy key.")
    # This allows the example to run without an actual API call, but cost calculation still works.
    # For actual API calls, replace with a valid key or set the env var.

client = OpenAI(api_key=openai_api_key)
model = "gpt-3.5-turbo"
prompt_messages = [{ "role": "user", "content": "Say this is a test"}]

# Simulate an OpenAI API call (or make a real one if API key is valid)
try:
    chat_completion = client.chat.completions.create(
        messages=prompt_messages,
        model=model
    )
    completion_content = chat_completion.choices[0].message.content
    print(f"API Completion: {completion_content}")
except Exception as e:
    print(f"Could not make actual OpenAI API call (continuing with dummy completion for cost calculation): {e}")
    completion_content = "This is a test."

# Calculate costs
prompt_cost = calculate_prompt_cost(prompt_messages, model)
completion_cost = calculate_completion_cost(completion_content, model)
total_cost = prompt_cost + completion_cost

print(f"Prompt Cost: ${prompt_cost:.6f}")
print(f"Completion Cost: ${completion_cost:.6f}")
print(f"Total Estimated Cost: ${total_cost:.6f}")

view raw JSON →