core
Class info¶
🛈 DocStrings¶
Utilities for calculating token costs using LiteLLM pricing data.
calculate_token_cost
async
¶
calculate_token_cost(
model: str,
input_tokens: int | None,
output_tokens: int | None,
*,
cache_read_tokens: int | None = None,
cache_write_tokens: int | None = None,
cache_timeout: int = _CACHE_TIMEOUT,
) -> TokenCosts | None
Calculate detailed costs for token usage based on model pricing.
Combines input and output token counts with their respective costs to calculate the breakdown of costs. If a token count is None, it will be treated as 0 tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the model used (e.g. "gpt-4", "openai:gpt-3.5-turbo") |
required |
input_tokens
|
int | None
|
Number of tokens in the prompt/input, or None. Includes the cache read and cache write tokens (as pydantic-ai counts them). |
required |
output_tokens
|
int | None
|
Number of tokens in the completion/output, or None |
required |
cache_read_tokens
|
int | None
|
Input tokens read from the prompt cache, billed at the model's cache read price (the input price if it has none) |
None
|
cache_write_tokens
|
int | None
|
Input tokens written to the prompt cache, billed at the model's cache creation price (the input price if it has none) |
None
|
cache_timeout
|
int
|
Number of seconds to keep prices in cache (default: 24 hours) |
_CACHE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
TokenCosts | None
|
TokenCosts | None: Detailed cost breakdown if pricing data available.
|
Source code in src/tokonomics/core.py
get_available_models
async
¶
Get a list of all available model names from LiteLLM pricing data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_timeout
|
int
|
Number of seconds to keep data in cache (default: 24 hours) |
_CACHE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of available model names, sorted alphabetically |
Source code in src/tokonomics/core.py
get_model_capabilities
async
¶
get_model_capabilities(
model: str, *, cache_timeout: int = _CACHE_TIMEOUT
) -> ModelCapabilities | None
Get capabilities information for a model from LiteLLM data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the model to look up capabilities for |
required |
cache_timeout
|
int
|
Number of seconds to keep data in cache (default: 24 hours) |
_CACHE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
ModelCapabilities | None
|
ModelCapabilities | None: Model's capabilities if found, None otherwise |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there's an error fetching the capabilities data |
Source code in src/tokonomics/core.py
get_model_costs
async
¶
get_model_costs(model: str, *, cache_timeout: int = _CACHE_TIMEOUT) -> ModelCosts | None
Get cost information for a model from LiteLLM pricing data.
The LiteLLM table is downloaded once and kept for cache_timeout seconds; lookups
of unknown models don't download it again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the model to look up costs for |
required |
cache_timeout
|
int
|
Number of seconds to keep prices in cache (default: 24 hours) |
_CACHE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
ModelCosts | None
|
ModelCosts | None: Model's cost information if found, None otherwise |