Tokonomics
Calculate costs for LLM usage based on token counts using LiteLLM's pricing data.
Installation
pip install tokonomics
Features
- Automatic cost calculation for various LLM models
- Detailed cost breakdown (prompt, completion, and total costs)
- Caches pricing data locally (24-hour default cache duration)
- Supports multiple model name formats (e.g., "gpt-4", "openai:gpt-4")
- Asynchronous API
- Fully typed with runtime type checking
- Zero configuration required
Usage
import asyncio
from tokonomics import calculate_token_cost
async def main():
# Calculate cost with token counts
costs = await calculate_token_cost(
model="gpt-4",
input_tokens=100, # tokens used in the prompt
output_tokens=50, # tokens used in the completion
)
if costs:
print(f"Prompt cost: ${costs.input_cost:.6f}")
print(f"Completion cost: ${costs.output_cost:.6f}")
print(f"Total cost: ${costs.total_cost:.6f}")
else:
print("Could not determine cost for model")
asyncio.run(main())
You can customize the cache timeout:
from tokonomics import get_model_costs, clear_cache
# Get model costs with custom cache duration (e.g., 1 hour)
costs = await get_model_costs("gpt-4", cache_timeout=3600)
if costs:
print(f"Input cost per token: ${costs['input_cost_per_token']}")
print(f"Output cost per token: ${costs['output_cost_per_token']}")
clear_cache()
Pydantic-AI Integration
If you're using pydantic-ai, you can directly calculate costs from its Usage objects:
from tokonomics import calculate_pydantic_cost
# Assuming you have a pydantic-ai Usage object
costs = await calculate_pydantic_cost(
model="gpt-4",
usage=usage_object,
)
if costs:
print(f"Prompt cost: ${costs.input_cost:.6f}")
print(f"Completion cost: ${costs.output_cost:.6f}")
print(f"Total cost: ${costs.total_cost:.6f}")
Model Name Support
The library supports multiple formats for model names:
- Direct model names:
"gpt-4" - Provider-prefixed:
"openai:gpt-4" - Provider-path style:
"openai/gpt-4"
Names are matched case-insensitively.
Data Source
Pricing data is sourced from LiteLLM's pricing repository and is automatically cached locally using hishel. The cache is updated when pricing data is not found or has expired.
Requirements
- Python 3.12+
httpxplatformdirsupathpydantic(≥ 2.0)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Release files for tokonomics 1.2.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tokonomics-1.2.18.tar.gz | 65.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokonomics-1.2.18-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 169.5 kB
Release files / tokonomics-1.2.18.tar.gz
| Download URL | tokonomics-1.2.18.tar.gz |
|---|---|
| Size | 65.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b40461c8c8ce71f4bc378c61a4743a55b20b5ddee50d27b9398c85c29279c17
|
|
BLAKE2b-256 checksum How to use checksums |
2e0ac8093f6836f898a9f587aa30786bba25f08e92c9c11f13f9e638b0b97688
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 25, 2026.
Transparency logRelease files / tokonomics-1.2.18-py3-none-any.whl
| Download URL | tokonomics-1.2.18-py3-none-any.whl |
|---|---|
| Size | 103.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ff42a59c16be309a0d8d29e66a079f0b1ed72dce4380a545d8f20c32e4c9ab8
|
|
BLAKE2b-256 checksum How to use checksums |
b1c4fc9b68acb9d10a44b2c7121c8e02094c46244f9e7b07fa86ef47ee2d1073
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 25, 2026.
Transparency log