Calcuate costs for LLM Usage based on token count
Project description
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.
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 tokonomics-1.2.18.tar.gz.
File metadata
- Download URL: tokonomics-1.2.18.tar.gz
- Upload date:
- Size: 65.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b40461c8c8ce71f4bc378c61a4743a55b20b5ddee50d27b9398c85c29279c17
|
|
| MD5 |
85b5b3f28413da87549cb7abbe2a65f6
|
|
| BLAKE2b-256 |
2e0ac8093f6836f898a9f587aa30786bba25f08e92c9c11f13f9e638b0b97688
|
Provenance
The following attestation bundles were made for tokonomics-1.2.18.tar.gz:
Publisher:
build.yml on phil65/tokonomics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokonomics-1.2.18.tar.gz -
Subject digest:
6b40461c8c8ce71f4bc378c61a4743a55b20b5ddee50d27b9398c85c29279c17 - Sigstore transparency entry: 992553162
- Sigstore integration time:
-
Permalink:
phil65/tokonomics@1ffb67df7d698187c605e364b01db8fdbe14a597 -
Branch / Tag:
refs/tags/v1.2.18 - Owner: https://github.com/phil65
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@1ffb67df7d698187c605e364b01db8fdbe14a597 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tokonomics-1.2.18-py3-none-any.whl.
File metadata
- Download URL: tokonomics-1.2.18-py3-none-any.whl
- Upload date:
- Size: 103.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ff42a59c16be309a0d8d29e66a079f0b1ed72dce4380a545d8f20c32e4c9ab8
|
|
| MD5 |
7e8a13bfcfeffe8620435df1d7b17cb2
|
|
| BLAKE2b-256 |
b1c4fc9b68acb9d10a44b2c7121c8e02094c46244f9e7b07fa86ef47ee2d1073
|
Provenance
The following attestation bundles were made for tokonomics-1.2.18-py3-none-any.whl:
Publisher:
build.yml on phil65/tokonomics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tokonomics-1.2.18-py3-none-any.whl -
Subject digest:
8ff42a59c16be309a0d8d29e66a079f0b1ed72dce4380a545d8f20c32e4c9ab8 - Sigstore transparency entry: 992553203
- Sigstore integration time:
-
Permalink:
phil65/tokonomics@1ffb67df7d698187c605e364b01db8fdbe14a597 -
Branch / Tag:
refs/tags/v1.2.18 - Owner: https://github.com/phil65
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@1ffb67df7d698187c605e364b01db8fdbe14a597 -
Trigger Event:
push
-
Statement type: