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