* Calculates the USD cost based on token usage and model cost configuration
(modelCosts: ModelCosts, usage: Usage)
| 177 | * Calculates the USD cost based on token usage and model cost configuration |
| 178 | */ |
| 179 | function tokensToUSDCost(modelCosts: ModelCosts, usage: Usage): number { |
| 180 | return ( |
| 181 | (usage.input_tokens / 1_000_000) * modelCosts.inputTokens + |
| 182 | (usage.output_tokens / 1_000_000) * modelCosts.outputTokens + |
| 183 | ((usage.cache_read_input_tokens ?? 0) / 1_000_000) * |
| 184 | modelCosts.promptCacheReadTokens + |
| 185 | ((usage.cache_creation_input_tokens ?? 0) / 1_000_000) * |
| 186 | modelCosts.promptCacheWriteTokens + |
| 187 | (usage.server_tool_use?.web_search_requests ?? 0) * |
| 188 | modelCosts.webSearchRequests |
| 189 | ) |
| 190 | } |
| 191 | |
| 192 | export function getModelCosts(model: string, usage: Usage): ModelCosts { |
| 193 | const shortName = getCanonicalName(model) |