cost_calculator
zocrates.utils.cost_calculator ¶
USD cost calculation and rate resolution engine for multi-agent chat sessions.
Provides tiered token pricing resolution
- Local & Free models (:free suffix, ollama/, vllm/, local/) -> $0.000000.
- OpenRouter Cached Catalog (primary ground truth from live API metadata).
- LiteLLM cost_per_token fallback (secondary only).
- Safe default -> $0.000000.
Derives cost strictly from prompt, completion, and prompt-cache-read tokens without double-counting reasoning/thoughts tokens.
_get_or_build_catalog_index ¶
Build and return an O(1) rate lookup dictionary from the OpenRouter models catalog.
Source code in zocrates/utils/cost_calculator.py
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 | |
_normalize_model_name ¶
Normalize model identifier for catalog lookup.
Source code in zocrates/utils/cost_calculator.py
aggregate_turn_cost ¶
Calculate aggregated USD cost and resolve agent model mapping for an assistant turn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assistant_events
|
list[Any]
|
Assistant event objects in the conversational turn. |
required |
default_model
|
str | None
|
Fallback model identifier. |
None
|
zotero_data_dir
|
Path | str | None
|
Optional Zotero data directory. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[float, dict[str, str]]
|
Tuple of (turn_cost_usd, agent_models_dict). |
Source code in zocrates/utils/cost_calculator.py
calculate_event_cost ¶
calculate_event_cost(model_name, prompt_tokens, candidates_tokens, cached_tokens=0, zotero_data_dir=None)
Calculate the USD cost for a single model generation event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str | None
|
The identifier of the model used. |
required |
prompt_tokens
|
int
|
Total prompt token count (including cached tokens). |
required |
candidates_tokens
|
int
|
Completion token count (includes thoughts; not double-counted). |
required |
cached_tokens
|
int
|
Number of prompt tokens read from cache. |
0
|
zotero_data_dir
|
Path | str | None
|
Optional Zotero data directory for catalog discovery. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
USD cost as a float. |
Source code in zocrates/utils/cost_calculator.py
clear_rate_cache ¶
Clear in-memory rate caches (useful for unit tests or after catalog refresh).
get_model_rates ¶
Resolve token rates for a model.
Returns:
| Type | Description |
|---|---|
float
|
(prompt_rate_per_token, completion_rate_per_token, cache_read_rate_per_token) |
float
|
in USD. |
Source code in zocrates/utils/cost_calculator.py
normalize_agent_role ¶
Normalize agent author string to standardized role name, or None if not an LLM agent.
Returns:
| Type | Description |
|---|---|
str | None
|
'coordinator', 'library_search', 'library_synthesizer', 'online_search', |
str | None
|
'online_synthesizer', 'user', or None for workflow nodes. |