( cost: number, usage: Usage, model: string, )
| 276 | } |
| 277 | |
| 278 | export function addToTotalSessionCost( |
| 279 | cost: number, |
| 280 | usage: Usage, |
| 281 | model: string, |
| 282 | ): number { |
| 283 | const modelUsage = addToTotalModelUsage(cost, usage, model) |
| 284 | addToTotalCostState(cost, modelUsage, model) |
| 285 | |
| 286 | const attrs = |
| 287 | isFastModeEnabled() && usage.speed === 'fast' |
| 288 | ? { model, speed: 'fast' } |
| 289 | : { model } |
| 290 | |
| 291 | getCostCounter()?.add(cost, attrs) |
| 292 | getTokenCounter()?.add(usage.input_tokens, { ...attrs, type: 'input' }) |
| 293 | getTokenCounter()?.add(usage.output_tokens, { ...attrs, type: 'output' }) |
| 294 | getTokenCounter()?.add(usage.cache_read_input_tokens ?? 0, { |
| 295 | ...attrs, |
| 296 | type: 'cacheRead', |
| 297 | }) |
| 298 | getTokenCounter()?.add(usage.cache_creation_input_tokens ?? 0, { |
| 299 | ...attrs, |
| 300 | type: 'cacheCreation', |
| 301 | }) |
| 302 | |
| 303 | let totalCost = cost |
| 304 | for (const advisorUsage of getAdvisorUsage(usage)) { |
| 305 | const advisorCost = calculateUSDCost(advisorUsage.model, advisorUsage) |
| 306 | logEvent('tengu_advisor_tool_token_usage', { |
| 307 | advisor_model: |
| 308 | advisorUsage.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 309 | input_tokens: advisorUsage.input_tokens, |
| 310 | output_tokens: advisorUsage.output_tokens, |
| 311 | cache_read_input_tokens: advisorUsage.cache_read_input_tokens ?? 0, |
| 312 | cache_creation_input_tokens: |
| 313 | advisorUsage.cache_creation_input_tokens ?? 0, |
| 314 | cost_usd_micros: Math.round(advisorCost * 1_000_000), |
| 315 | }) |
| 316 | totalCost += addToTotalSessionCost( |
| 317 | advisorCost, |
| 318 | advisorUsage, |
| 319 | advisorUsage.model, |
| 320 | ) |
| 321 | } |
| 322 | return totalCost |
| 323 | } |
no test coverage detected