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

Function addToTotalSessionCost

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

Source from the content-addressed store, hash-verified

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

Callers 4

queryModelFunction · 0.85
queryModelGrokFunction · 0.85
queryModelOpenAIFunction · 0.85

Calls 11

addToTotalModelUsageFunction · 0.85
addToTotalCostStateFunction · 0.85
getGoalFunction · 0.85
updateGoalTokensFunction · 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