Token usage tracking wrapper for LLMs
Project description
Tokenator : Track and analyze LLM token usage and cost
Have you ever wondered about :
- How many tokens does your AI agent consume?
- How much does it cost to do run a complex AI workflow with multiple LLM providers?
- How much money/tokens did you spend today on developing with LLMs?
Afraid not, tokenator is here! With tokenator's easy to use API, you can start tracking LLM usage in a matter of minutes.
Get started with just 3 lines of code!
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"
}
]
}
]
}
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"
}
]
}
]
}
"""
Most importantly, none of your data is ever sent to any server.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tokenator-0.1.11.tar.gz.
File metadata
- Download URL: tokenator-0.1.11.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9297d1a6be647c4313c86a40343152dcc086ea7e896cb5de094634e09e06e6b
|
|
| MD5 |
52bab1227a5007d780e61cc6725b416f
|
|
| BLAKE2b-256 |
f91fe063634445ba3e6e7e47ad9ad08475a6d66cc8be7f63bdaef96590a620fb
|
File details
Details for the file tokenator-0.1.11-py3-none-any.whl.
File metadata
- Download URL: tokenator-0.1.11-py3-none-any.whl
- Upload date:
- Size: 18.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.10.15 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0ea0d33f3fe762da6009392253ba3f653f4c8cae90e559acc1cf0040a8a8a52
|
|
| MD5 |
44e5867580bbf20545a4f1afb781eb20
|
|
| BLAKE2b-256 |
6800f21d875af197df634800652aaca4abf7837396cc9b0af60483a6744bc622
|