Tokonomics
Calculate LLM usage costs from token counts using LiteLLM's pricing data, and discover available models (pricing, context window, modalities) across many providers.
Installation
pip install tokonomics
# optional: exact token counting via tiktoken
pip install "tokonomics[tiktoken]"
Quickstart
import asyncio
from tokonomics import calculate_token_cost
async def main():
costs = await calculate_token_cost(
model="gpt-4o",
input_tokens=100, # tokens used in the prompt
output_tokens=50, # tokens used in the completion
)
if costs:
print(f"Input cost: ${costs.input_cost:.6f}")
print(f"Output cost: ${costs.output_cost:.6f}")
print(f"Total cost: ${costs.total_cost:.6f}")
else:
print("Could not determine cost for model")
asyncio.run(main())
Features
- Cost calculation for thousands of models from LiteLLM's pricing JSON
(
Decimalinput/output/total breakdown) - Flexible model names:
"gpt-4o","openai:gpt-4o","openai/gpt-4o"(case-insensitive) - Model capabilities: context limits, function calling, vision, audio, prompt caching
- Model discovery across providers (OpenAI, Anthropic, OpenRouter, Groq, Mistral, Gemini, GitHub Models, Copilot, DeepSeek, xAI, Ollama, models.dev, ...)
- Token counting with tiktoken, transformers or a heuristic fallback
- pydantic-ai integration via
calculate_pydantic_cost - Generated
Literaltypes of known model ids (tokonomics.ModelName) - Async API, fully typed, pricing data cached locally
Usage
Model costs and capabilities
from tokonomics import get_model_capabilities, get_model_costs, reset_cache
# Per-token prices; cache_timeout controls how long pricing data is cached (seconds)
costs = await get_model_costs("openai:gpt-4o", 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']}")
caps = await get_model_capabilities("gpt-4o")
if caps:
print(caps.max_input_tokens, caps.supports_vision, caps.supports_function_calling)
reset_cache() # clear the in-memory cache
Pydantic-AI integration
from tokonomics import calculate_pydantic_cost
# `usage` is a pydantic-ai usage object (anything with input_tokens / output_tokens)
costs = await calculate_pydantic_cost(model="gpt-4o", usage=result.usage())
if costs:
print(f"Total cost: ${costs.total_cost:.6f}")
Model discovery
from tokonomics import get_all_models
# Without `providers`, all providers whose API keys are set are queried.
models = await get_all_models(providers=["openai", "anthropic", "openrouter"])
for model in models[:5]:
print(model.pydantic_ai_id, model.context_window, model.pricing)
Providers read their API keys from the usual environment variables
(OPENROUTER_API_KEY, GROQ_API_KEY, MISTRAL_API_KEY, GEMINI_API_KEY, ...).
A synchronous variant is available as tokonomics.model_discovery.get_all_models_sync.
Token counting
from tokonomics import count_tokens
count_tokens("Hello world, how are you?", model="gpt-4o")
Uses tiktoken if installed, then transformers, then a rough approximation.
Data sources
Pricing and capability data come from LiteLLM's model_prices_and_context_window.json; model discovery uses the providers' model APIs and models.dev. Responses are cached locally (24 hours by default).
Requirements
- Python 3.13+
Documentation
Full API reference: https://phil65.github.io/tokonomics/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for tokonomics 1.3.0
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 |
|---|---|---|---|---|
| tokonomics-1.3.0.tar.gz | 72.3 kB | With provenance | Details for tokonomics-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tokonomics-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for tokonomics-1.3.0-py3-none-any.whl |
Total release size: 176.7 kB
Release files / tokonomics-1.3.0.tar.gz
| Download URL | tokonomics-1.3.0.tar.gz |
|---|---|
| Size | 72.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0dc95842b2e1cd1f34ad9132f6d0f146cfb2435118aa012ba22e5b2c5d1032d5
|
|
BLAKE2b-256 checksum How to use checksums |
142d07fca8763fec1697535190c65e5d7af52b54eb4294e1742d1c2f69ef1dda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logRelease files / tokonomics-1.3.0-py3-none-any.whl
| Download URL | tokonomics-1.3.0-py3-none-any.whl |
|---|---|
| Size | 104.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c9309a4b2c2822e5a9bba7a3166092e83301308e5bbcbd52bc6895761aeee87a
|
|
BLAKE2b-256 checksum How to use checksums |
ac863f52121cab088e33745b911af3351ffc29f604df71ee21d0f725c5985eac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logSecurity for tokonomics 1.3.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for tokonomics-1.3.0.tar.gz
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 Nov 23, 2026.
Transparency logA 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.