MCPcopy Create free account
hub / github.com/IIIIQIIII/claude-code / addToTotalSessionCost

Function addToTotalSessionCost

src/cost-tracker.ts:278–323  ·  view source on GitHub ↗
(
  cost: number,
  usage: Usage,
  model: string,
)

Source from the content-addressed store, hash-verified

276}
277
278export 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}

Callers 2

queryModelFunction · 0.85

Calls 9

addToTotalModelUsageFunction · 0.85
addToTotalCostStateFunction · 0.85
isFastModeEnabledFunction · 0.85
getCostCounterFunction · 0.85
getTokenCounterFunction · 0.85
getAdvisorUsageFunction · 0.85
calculateUSDCostFunction · 0.85
logEventFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected