Tokemon
A unified Python library for counting tokens across multiple LLM providers. Tokemon provides a simple, consistent interface to count tokens for OpenAI, Anthropic, Google AI, and xAI models.
Features
- Unified API for token counting across multiple providers
- Support for both synchronous and asynchronous operations
- Dynamic model discovery via provider APIs
- Type-safe responses with dataclass
Installation
pip install tokemon
Quick Start
from tokemon import tokemon, ProviderName, Mode
# Create a tokenizer for your preferred provider
tokenizer = tokemon(
model="gpt-4o",
provider=ProviderName.OPENAI.value,
mode=Mode.SYNC,
)
# Count tokens
response = tokenizer.count_tokens("Hello, world!")
print(response.input_tokens) # Number of tokens
print(response.model) # Model name
print(response.provider) # Provider name
Usage by Provider
OpenAI
OpenAI tokenization uses tiktoken and works offline without an API key.
from tokemon import tokemon, ProviderName, Mode
tokenizer = tokemon(
model="gpt-4o",
provider=ProviderName.OPENAI.value,
mode=Mode.SYNC,
)
response = tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
Anthropic
Note: Set the
ANTHROPIC_API_KEYenvironment variable before using the Anthropic provider.
export ANTHROPIC_API_KEY="your-api-key"
from tokemon import tokemon, ProviderName, Mode
tokenizer = tokemon(
model="claude-sonnet-4-5",
provider=ProviderName.ANTHROPIC.value,
mode=Mode.SYNC,
)
response = tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
Async example:
import asyncio
from tokemon import tokemon, ProviderName, Mode
async def main():
tokenizer = tokemon(
model="claude-sonnet-4-5",
provider=ProviderName.ANTHROPIC.value,
mode=Mode.ASYNC,
)
response = await tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
asyncio.run(main())
Google AI (Gemini)
Note: Set the
GEMINI_API_KEYenvironment variable before using the Google AI provider.
export GEMINI_API_KEY="your-api-key"
from tokemon import tokemon, ProviderName, Mode
tokenizer = tokemon(
model="gemini-2.5-flash",
provider=ProviderName.GOOGLE.value,
mode=Mode.SYNC,
)
response = tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
Async example:
import asyncio
from tokemon import tokemon, ProviderName, Mode
async def main():
tokenizer = tokemon(
model="gemini-2.5-flash",
provider=ProviderName.GOOGLE.value,
mode=Mode.ASYNC,
)
response = await tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
asyncio.run(main())
xAI (Grok)
Note: Set the
XAI_API_KEYenvironment variable before using the xAI provider.
export XAI_API_KEY="your-api-key"
from tokemon import tokemon, ProviderName, Mode
tokenizer = tokemon(
model="grok-3",
provider=ProviderName.XAI.value,
mode=Mode.SYNC,
)
response = tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
Async example:
import asyncio
from tokemon import tokemon, ProviderName, Mode
async def main():
tokenizer = tokemon(
model="grok-3",
provider=ProviderName.XAI.value,
mode=Mode.ASYNC,
)
response = await tokenizer.count_tokens("Hello, world!")
print(f"Token count: {response.input_tokens}")
asyncio.run(main())
Listing Available Models
Use tokemon_models() to discover models supported by each provider at runtime:
from tokemon import tokemon_models, ProviderName, Mode
# Get a provider instance
provider = tokemon_models(
provider=ProviderName.OPENAI.value,
mode=Mode.SYNC,
)
# List available models
models = provider.models()
print(models)
Async example:
import asyncio
from tokemon import tokemon_models, ProviderName, Mode
async def main():
provider = tokemon_models(
provider=ProviderName.ANTHROPIC.value,
mode=Mode.ASYNC,
)
models = await provider.models()
print(models)
asyncio.run(main())
Response Object
The count_tokens method returns a TokenizerResponse dataclass:
@dataclass
class TokenizerResponse:
input_tokens: int | None # Number of tokens in the input
model: str # Model name used for tokenization
provider: str # Provider name (openai, anthropic, google, xai)
Requirements
- Python >= 3.10
License
MIT License
Metadata
Release files for tokemon 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tokemon-0.1.1.tar.gz | 10.7 kB | Without provenance | Details for tokemon-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tokemon-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for tokemon-0.1.1-py3-none-any.whl |
Total release size: 19.9 kB
Release files / tokemon-0.1.1.tar.gz
| Download URL | tokemon-0.1.1.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9fb9a602f1c3291b46770a1799a2f3985972b822e4a62dbc0a1b85483c51aea0
|
|
BLAKE2b-256 checksum How to use checksums |
655962fbb234dfee8ea9224fa2122f600f074510fec56ad31f7fc0f5ea07403d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.0
|
Release files / tokemon-0.1.1-py3-none-any.whl
| Download URL | tokemon-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa246f3ff7cdd4b7e9750a0cdba5de9fdd2f939602b236398657d19d9f89771b
|
|
BLAKE2b-256 checksum How to use checksums |
a22b428d2a471d293278840de7fd6bf3a2152d1cde21ee2f952d8f4c8f8e722e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.0
|
Security for tokemon 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.