( cost: number, usage: Usage, model: string, )
| 277 | } |
| 278 | |
| 279 | export function addToTotalSessionCost( |
| 280 | cost: number, |
| 281 | usage: Usage, |
| 282 | model: string, |
| 283 | ): number { |
| 284 | const modelUsage = addToTotalModelUsage(cost, usage, model) |
| 285 | addToTotalCostState(cost, modelUsage, model) |
| 286 | if (feature('GOAL')) { |
| 287 | const { getGoal, updateGoalTokens } = |
| 288 | require('./services/goal/goalState.js') as typeof import('./services/goal/goalState.js') |
| 289 | const totalDelta = |
| 290 | (usage.input_tokens ?? 0) + |
| 291 | (usage.output_tokens ?? 0) + |
| 292 | (usage.cache_read_input_tokens ?? 0) + |
| 293 | (usage.cache_creation_input_tokens ?? 0) |
| 294 | const currentGoal = getGoal() |
| 295 | if (totalDelta > 0 && currentGoal?.status === 'active') { |
| 296 | const { logForDebugging: goalDbg } = |
| 297 | require('./utils/debug.js') as typeof import('./utils/debug.js') |
| 298 | goalDbg( |
| 299 | `[goal] cost: in=${usage.input_tokens ?? 0} out=${usage.output_tokens ?? 0} cache_r=${usage.cache_read_input_tokens ?? 0} cache_w=${usage.cache_creation_input_tokens ?? 0} delta=${totalDelta}`, |
| 300 | ) |
| 301 | updateGoalTokens(totalDelta) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | const attrs = |
| 306 | isFastModeEnabled() && usage.speed === 'fast' |
| 307 | ? { model, speed: 'fast' } |
| 308 | : { model } |
| 309 | |
| 310 | getCostCounter()?.add(cost, attrs) |
| 311 | getTokenCounter()?.add(usage.input_tokens, { ...attrs, type: 'input' }) |
| 312 | getTokenCounter()?.add(usage.output_tokens, { ...attrs, type: 'output' }) |
| 313 | getTokenCounter()?.add(usage.cache_read_input_tokens ?? 0, { |
| 314 | ...attrs, |
| 315 | type: 'cacheRead', |
| 316 | }) |
| 317 | getTokenCounter()?.add(usage.cache_creation_input_tokens ?? 0, { |
| 318 | ...attrs, |
| 319 | type: 'cacheCreation', |
| 320 | }) |
| 321 | |
| 322 | let totalCost = cost |
| 323 | for (const advisorUsage of getAdvisorUsage(usage)) { |
| 324 | const advisorCost = calculateUSDCost(advisorUsage.model, advisorUsage) |
| 325 | logEvent('tengu_advisor_tool_token_usage', { |
| 326 | advisor_model: |
| 327 | advisorUsage.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, |
| 328 | input_tokens: advisorUsage.input_tokens, |
| 329 | output_tokens: advisorUsage.output_tokens, |
| 330 | cache_read_input_tokens: advisorUsage.cache_read_input_tokens ?? 0, |
| 331 | cache_creation_input_tokens: |
| 332 | advisorUsage.cache_creation_input_tokens ?? 0, |
| 333 | cost_usd_micros: Math.round(advisorCost * 1_000_000), |
| 334 | }) |
| 335 | totalCost += addToTotalSessionCost( |
| 336 | advisorCost, |
no test coverage detected