()
| 181 | } |
| 182 | |
| 183 | function formatModelUsage(): string { |
| 184 | const modelUsageMap = getModelUsage() |
| 185 | if (Object.keys(modelUsageMap).length === 0) { |
| 186 | return 'Usage: 0 input, 0 output, 0 cache read, 0 cache write' |
| 187 | } |
| 188 | |
| 189 | // Accumulate usage by short name |
| 190 | const usageByShortName: { [shortName: string]: ModelUsage } = {} |
| 191 | for (const [model, usage] of Object.entries(modelUsageMap)) { |
| 192 | const shortName = getCanonicalName(model) |
| 193 | if (!usageByShortName[shortName]) { |
| 194 | usageByShortName[shortName] = { |
| 195 | inputTokens: 0, |
| 196 | outputTokens: 0, |
| 197 | cacheReadInputTokens: 0, |
| 198 | cacheCreationInputTokens: 0, |
| 199 | webSearchRequests: 0, |
| 200 | costUSD: 0, |
| 201 | contextWindow: 0, |
| 202 | maxOutputTokens: 0, |
| 203 | } |
| 204 | } |
| 205 | const accumulated = usageByShortName[shortName] |
| 206 | accumulated.inputTokens += usage.inputTokens |
| 207 | accumulated.outputTokens += usage.outputTokens |
| 208 | accumulated.cacheReadInputTokens += usage.cacheReadInputTokens |
| 209 | accumulated.cacheCreationInputTokens += usage.cacheCreationInputTokens |
| 210 | accumulated.webSearchRequests += usage.webSearchRequests |
| 211 | accumulated.costUSD += usage.costUSD |
| 212 | } |
| 213 | |
| 214 | let result = 'Usage by model:' |
| 215 | for (const [shortName, usage] of Object.entries(usageByShortName)) { |
| 216 | const usageString = |
| 217 | ` ${formatNumber(usage.inputTokens)} input, ` + |
| 218 | `${formatNumber(usage.outputTokens)} output, ` + |
| 219 | `${formatNumber(usage.cacheReadInputTokens)} cache read, ` + |
| 220 | `${formatNumber(usage.cacheCreationInputTokens)} cache write` + |
| 221 | (usage.webSearchRequests > 0 |
| 222 | ? `, ${formatNumber(usage.webSearchRequests)} web search` |
| 223 | : '') + |
| 224 | ` (${formatCost(usage.costUSD)})` |
| 225 | result += `\n` + `${shortName}:`.padStart(21) + usageString |
| 226 | } |
| 227 | return result |
| 228 | } |
| 229 | |
| 230 | export function formatTotalCost(): string { |
| 231 | const costDisplay = |
no test coverage detected