* Calculates the USD cost based on token usage and model cost configuration
(modelCosts: ModelCosts, usage: Usage)
| 129 | * Calculates the USD cost based on token usage and model cost configuration |
| 130 | */ |
| 131 | function tokensToUSDCost(modelCosts: ModelCosts, usage: Usage): number { |
| 132 | return ( |
| 133 | (usage.input_tokens / 1_000_000) * modelCosts.inputTokens + |
| 134 | (usage.output_tokens / 1_000_000) * modelCosts.outputTokens + |
| 135 | ((usage.cache_read_input_tokens ?? 0) / 1_000_000) * |
| 136 | modelCosts.promptCacheReadTokens + |
| 137 | ((usage.cache_creation_input_tokens ?? 0) / 1_000_000) * |
| 138 | modelCosts.promptCacheWriteTokens + |
| 139 | (usage.server_tool_use?.web_search_requests ?? 0) * |
| 140 | modelCosts.webSearchRequests |
| 141 | ) |
| 142 | } |
| 143 | |
| 144 | export function getModelCosts(model: string, usage: Usage): ModelCosts { |
| 145 | const shortName = getCanonicalName(model) |