tokonomics
Class info¶
Re-exported classes¶
| Class | Module | Description |
|---|---|---|
| CopilotTokenManager | tokonomics.model_discovery.copilot_provider.token_manager | Manager for GitHub Copilot API tokens. |
| GroqProvider | tokonomics.model_discovery.groq_provider.provider | Groq API provider. |
| MistralProvider | tokonomics.model_discovery.mistral_provider.provider | Mistral AI API provider. |
| ModelCostTier | tokonomics.toko_types | Prices that apply to a request above a prompt size (LiteLLM's *_above_<N>k_tokens). |
| ModelCosts | tokonomics.toko_types | Cost information for a model. |
| ModelInfo | tokonomics.model_discovery.model_info | Unified model information from various providers. |
| ModelPricing | tokonomics.model_discovery.model_info | Pricing information for a model. |
| ModelProvider | tokonomics.model_discovery.base | Base class for model providers. |
| OpenRouterProvider | tokonomics.model_discovery.openrouter_provider.provider | OpenRouter API provider. |
| TokenCosts | tokonomics.toko_types | Detailed breakdown of token costs. |
| Usage | tokonomics.pydanticai_cost | Protocol matching pydantic-ai's Usage object structure. |
🛈 DocStrings¶
Tokonomics: main package.
Calcuate costs for LLM Usage based on token count.
CopilotTokenManager
¶
Manager for GitHub Copilot API tokens.
Source code in src/tokonomics/model_discovery/copilot_provider/token_manager.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
generate_headers
async
¶
Generate headers for GitHub Copilot API requests.
Source code in src/tokonomics/model_discovery/copilot_provider/token_manager.py
get_token
async
¶
Get a valid Copilot token, refreshing if needed.
Source code in src/tokonomics/model_discovery/copilot_provider/token_manager.py
get_token_sync
¶
Get a valid Copilot token, refreshing if needed.
Source code in src/tokonomics/model_discovery/copilot_provider/token_manager.py
is_available
¶
GroqProvider
¶
Bases: ModelProvider
Groq API provider.
Source code in src/tokonomics/model_discovery/groq_provider/provider.py
MistralProvider
¶
Bases: ModelProvider
Mistral AI API provider.
Source code in src/tokonomics/model_discovery/mistral_provider/provider.py
ModelCostTier
¶
Bases: TypedDict
Prices that apply to a request above a prompt size (LiteLLM's *_above_<N>k_tokens).
Source code in src/tokonomics/toko_types.py
ModelCosts
¶
Bases: TypedDict
Cost information for a model.
Source code in src/tokonomics/toko_types.py
cache_creation_input_token_cost
instance-attribute
¶
Price of an input token written to the prompt cache.
cache_read_input_token_cost
instance-attribute
¶
Price of an input token read from the prompt cache.
tiers
instance-attribute
¶
tiers: NotRequired[dict[int, ModelCostTier]]
Prompt-size threshold (tokens) -> prices for requests with more input tokens.
ModelInfo
dataclass
¶
Unified model information from various providers.
Source code in src/tokonomics/model_discovery/model_info.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
context_window
class-attribute
instance-attribute
¶
Maximum number of tokens that can be processed in one request.
created_at
class-attribute
instance-attribute
¶
Timestamp when the model was created/released.
description
class-attribute
instance-attribute
¶
Detailed description of the model's capabilities.
id_override
class-attribute
instance-attribute
¶
Optional override for the model ID used in API calls.
When set, this value is used instead of the auto-generated pydantic_ai_id. Useful for agents like Claude Code that need simple IDs (e.g., 'opus', 'sonnet').
input_modalities
class-attribute
instance-attribute
¶
Supported input modalities (text, image, audio, video, etc.).
is_deprecated
class-attribute
instance-attribute
¶
Whether this model version is deprecated.
is_embedding
class-attribute
instance-attribute
¶
Whether this model is primarily used for creating embeddings.
is_free
class-attribute
instance-attribute
¶
Whether this model is free to use.
max_output_tokens
class-attribute
instance-attribute
¶
Maximum number of tokens the model can generate in a response.
metadata
class-attribute
instance-attribute
¶
Provider-specific metadata that doesn't fit in standard fields.
output_modalities
class-attribute
instance-attribute
¶
Supported output modalities (text, image, audio, video, etc.).
owned_by
class-attribute
instance-attribute
¶
Organization that owns/created the model.
pricing
class-attribute
instance-attribute
¶
pricing: ModelPricing | None = None
Pricing information for using the model.
provider
class-attribute
instance-attribute
¶
Service provider name (e.g. OpenAI, Anthropic).
pydantic_ai_id
property
¶
Unique pydantic-ai style identifier for the model.
Returns id_override if set, otherwise returns '{provider}:{id}'.
pydantic_ai_variants
property
¶
Thinking level variants as pydantic-ai ModelSettings.
Returns a dict mapping variant name (e.g., 'high', 'max') to provider-specific model settings for that thinking level.
Only populated for models that support reasoning/thinking (based on metadata from models.dev).
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dict mapping variant name to ModelSettings dict. |
dict[str, dict[str, Any]]
|
Empty dict if model doesn't support reasoning variants. |
format
¶
Format model information as a human-readable string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted model information |
Source code in src/tokonomics/model_discovery/model_info.py
ModelPricing
dataclass
¶
Pricing information for a model.
Source code in src/tokonomics/model_discovery/model_info.py
completion
class-attribute
instance-attribute
¶
Cost per token for completion outputs.
image
class-attribute
instance-attribute
¶
Cost per image for input processing.
input_cache_read
class-attribute
instance-attribute
¶
Cost for reading from input cache.
input_cache_write
class-attribute
instance-attribute
¶
Cost for writing to input cache.
internal_reasoning
class-attribute
instance-attribute
¶
Cost for internal reasoning functionality.
prompt
class-attribute
instance-attribute
¶
Cost per token for prompt inputs.
web_search
class-attribute
instance-attribute
¶
Cost for web search functionality.
ModelProvider
¶
Bases: ABC
Base class for model providers.
Source code in src/tokonomics/model_discovery/base.py
models_cache_ttl
class-attribute
instance-attribute
¶
models_cache_ttl: float = MODELS_CACHE_TTL
Seconds discovery reuses this provider's parsed model list in memory.
get_models
async
¶
get_models() -> list[ModelInfo]
Fetch available models from the provider asynchronously.
Source code in src/tokonomics/model_discovery/base.py
OpenRouterProvider
¶
Bases: ModelProvider
OpenRouter API provider.
Source code in src/tokonomics/model_discovery/openrouter_provider/provider.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
TokenCosts
dataclass
¶
Detailed breakdown of token costs.
Source code in src/tokonomics/toko_types.py
Usage
¶
Bases: Protocol
Protocol matching pydantic-ai's Usage object structure.
Source code in src/tokonomics/pydanticai_cost.py
calculate_pydantic_cost
async
¶
calculate_pydantic_cost(
model: str, usage: Usage, *, cache_timeout: int = 86400
) -> TokenCosts | None
calculate_pydantic_cost(
model: str, usage: Usage | None, *, cache_timeout: int = 86400
) -> TokenCosts | None
Calculate costs from a pydantic-ai Usage object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the model used (e.g. "gpt-4", "openai:gpt-3.5-turbo") |
required |
usage
|
Usage | None
|
Token usage information from pydantic-ai |
required |
cache_timeout
|
int
|
Number of seconds to keep prices in cache (default: 24 hours) |
86400
|
Returns:
| Type | Description |
|---|---|
TokenCosts | None
|
TokenCosts | None: Detailed cost breakdown if pricing data available |
Source code in src/tokonomics/pydanticai_cost.py
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
count_tokens
¶
Count total number of tokens in text(s) with fallback strategies.
Uses following fallback order: 1. tiktoken (if available) 2. transformers (if available) 3. rough approximation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | Sequence[str]
|
Single text or sequence of texts to count tokens for |
required |
model
|
str | None
|
Optional model name for tiktoken (ignored in fallbacks) |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Total token count for all provided text(s) |
Source code in src/tokonomics/token_count.py
get_all_models
async
¶
get_all_models(
*,
providers: Sequence[ProviderType | Literal["models.dev"]] | None = None,
include_deprecated: bool = False,
max_age: timedelta | None = None,
) -> list[ModelInfo]
Fetch models from selected providers in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
providers
|
Sequence[ProviderType | Literal['models.dev']] | None
|
Sequence of provider names to use. Defaults to available providers. Use "models.dev" to fetch all models from the models.dev aggregator. |
None
|
include_deprecated
|
bool
|
Whether to include deprecated models. Defaults to False. |
False
|
max_age
|
timedelta | None
|
Only include models released within this duration (by their
|
None
|
Returns:
| Type | Description |
|---|---|
list[ModelInfo]
|
list[ModelInfo]: Combined list of models from all providers. |
Source code in src/tokonomics/model_discovery/__init__.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
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 |