Skip to main content

Token usage tracking wrapper for LLMs

Project description

Tokenator : Track, analyze, compare LLM token usage and costs

Have you ever wondered :

  • How many tokens does your AI agent consume?
  • How much does it cost to run a complex AI workflow with multiple LLM providers?
  • Which LLM is more cost effective for my use case?
  • How much money/tokens did you spend today on developing with LLMs?

Afraid not, tokenator is here! With tokenator's easy to use functions, you can start tracking LLM usage in a matter of minutes.

Get started with just 3 lines of code!

Tokenator supports the official SDKs from openai, anthropic and google-genai(the new one). LLM providers which use the openai SDK like perplexity, deepseek and xAI are also supported.

Installation

pip install tokenator

Usage

OpenAI

from openai import OpenAI
from tokenator import tokenator_openai

openai_client = OpenAI(api_key="your-api-key")

# Wrap it with Tokenator
client = tokenator_openai(openai_client)

# Use it exactly like the OpenAI client
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

Works with AsyncOpenAI and streaming=True as well! Note : When streaming, don't forget to add stream_options={"include_usage": True} to the create() call!

Cost Analysis

from tokenator import usage

# Get usage for different time periods
usage.last_hour()
usage.last_day()
usage.last_week()
usage.last_month()

# Custom date range
usage.between("2024-03-01", "2024-03-15")

# Get usage for different LLM providers
usage.last_day("openai")
usage.last_day("anthropic")
usage.last_day("google")

Example usage object

print(cost.last_hour().model_dump_json(indent=4))
{
    "total_cost": 0.0004,
    "total_tokens": 79,
    "prompt_tokens": 52,
    "completion_tokens": 27,
    "providers": [
        {
            "total_cost": 0.0004,
            "total_tokens": 79,
            "prompt_tokens": 52,
            "completion_tokens": 27,
            "provider": "openai",
            "models": [
                {
                    "total_cost": 0.0004,
                    "total_tokens": 79,
                    "prompt_tokens": 52,
                    "completion_tokens": 27,
                    "model": "gpt-4o-2024-08-06"
                }
            ]
        }
    ]
}

Cookbooks

Want more code, example use cases and ideas? Check out our amazing cookbooks!

Features

  • Drop-in replacement for OpenAI, Anthropic client
  • Automatic token usage tracking
  • Cost analysis for different time periods
  • SQLite storage with zero configuration
  • Thread-safe operations
  • Minimal memory footprint
  • Minimal latency footprint

Anthropic

from anthropic import Anthropic, AsyncAnthropic
from tokenator import tokenator_anthropic

anthropic_client = AsyncAnthropic(api_key="your-api-key")

# Wrap it with Tokenator
client = tokenator_anthropic(anthropic_client)

# Use it exactly like the Anthropic client
response = await client.messages.create(
    model="claude-3-5-haiku-20241022",
    messages=[{"role": "user", "content": "hello how are you"}],
    max_tokens=20,
)

print(response)

print(usage.last_execution().model_dump_json(indent=4))
"""
{
    "total_cost": 0.0001,
    "total_tokens": 23,
    "prompt_tokens": 10,
    "completion_tokens": 13,
    "providers": [
        {
            "total_cost": 0.0001,
            "total_tokens": 23,
            "prompt_tokens": 10,
            "completion_tokens": 13,
            "provider": "anthropic",
            "models": [
                {
                    "total_cost": 0.0004,
                    "total_tokens": 79,
                    "prompt_tokens": 52,
                    "completion_tokens": 27,
                    "model": "claude-3-5-haiku-20241022"
                }
            ]
        }
    ]
}
"""

Google (Gemini - through AI studio)

from google import genai
from tokenator import tokenator_gemini

gemini_client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))

# Wrap it with Tokenator
client = tokenator_gemini(gemini_client)

# Use it exactly like the google-genai client
response = models.generate_content(
    model="gemini-2.0-flash",
    contents="hello how are you",
)

print(response)

print(usage.last_execution().model_dump_json(indent=4))
"""
{
    "total_cost": 0.0001,
    "total_tokens": 23,
    "prompt_tokens": 10,
    "completion_tokens": 13,
    "providers": [
        {
            "total_cost": 0.0001,
            "total_tokens": 23,
            "prompt_tokens": 10,
            "completion_tokens": 13,
            "provider": "gemini",
            "models": [
                {
                    "total_cost": 0.0004,
                    "total_tokens": 79,
                    "prompt_tokens": 52,
                    "completion_tokens": 27,
                    "model": "gemini-2.0-flash"
                }
            ]
        }
    ]
}
"""

xAI

You can use xAI models through the openai SDK and track usage using provider parameter in tokenator.

from openai import OpenAI
from tokenator import tokenator_openai

xai_client = OpenAI(
            api_key=os.getenv("XAI_API_KEY"),
            base_url="https://api.x.ai/v1"
        )

# Wrap it with Tokenator
client = tokenator_openai(xai_client, db_path=temp_db, provider="xai")

# Use it exactly like the OpenAI client but with xAI models
response = client.chat.completions.create(
    model="grok-2-latest",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response)

print(usage.last_execution())

Other AI model providers through openai SDKs

Today, a variety of AI companies have made their APIs compatible to the openai SDK. You can track usage of any such AI models using tokenator's provider parameter.

For example, let's see how we can track usage of perplexity tokens.

from openai import OpenAI
from tokenator import tokenator_openai

perplexity_client = OpenAI(
            api_key=os.getenv("PERPLEXITY_API_KEY"),
            base_url="https://api.perplexity.ai"
        )

# Wrap it with Tokenator
client = tokenator_openai(perplexity_client, db_path=temp_db, provider="perplexity")

# Use it exactly like the OpenAI client but with perplexity models
response = client.chat.completions.create(
    model="sonar",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response)

print(usage.last_execution())

print(usage.provider("perplexity"))

Most importantly, none of your data is ever sent to any server.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokenator-0.2.0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokenator-0.2.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file tokenator-0.2.0.tar.gz.

File metadata

  • Download URL: tokenator-0.2.0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.16 Linux/6.8.0-1021-azure

File hashes

Hashes for tokenator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7b312cdccbe4fb941df0daf5e412a3b9b2344ac6923cf0ee5e8c6a7f3c47b258
MD5 6fe9c9f88a30c360696421a3add85762
BLAKE2b-256 5807e6c112c1d647a49b997464dfc570e8e6fba045797153f8ce5bd568b81c7c

See more details on using hashes here.

File details

Details for the file tokenator-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenator-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.10.16 Linux/6.8.0-1021-azure

File hashes

Hashes for tokenator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d23194f802222c390820b1a4104d96a30ff9c11305a0e1865aeaa19c7b5908b5
MD5 ef557ebedf9e7acb29837a26b84b7d1b
BLAKE2b-256 1e221966ff8957d608458c43b59cc59abd14dd0b0b84698a63c35a1606a3f204

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page