@@ -81,7 +81,6 @@ export async function prepareReplyAgentPayloads(state: {
8181 fallbackAttempts,
8282 fallbackExhausted,
8383 fallbackTransition,
84- hasBillableUsageBuckets,
8584 modelUsed,
8685 payloadArray,
8786 preserveUserFacingSessionState,
@@ -483,13 +482,14 @@ export async function prepareReplyAgentPayloads(state: {
483482
484483 await signalTypingIfNeeded ( guardedReplyPayloads , typingSignals ) ;
485484
486- if ( isDiagnosticsEnabled ( cfg ) && hasNonzeroUsage ( usage ) ) {
487- const input = usage . input ?? 0 ;
488- const output = usage . output ?? 0 ;
489- const cacheRead = usage . cacheRead ?? 0 ;
490- const cacheWrite = usage . cacheWrite ?? 0 ;
485+ const diagnosticUsage = runResult . meta ?. agentMeta ?. diagnosticUsage ?? usage ;
486+ if ( isDiagnosticsEnabled ( cfg ) && hasNonzeroUsage ( diagnosticUsage ) ) {
487+ const input = diagnosticUsage . input ?? 0 ;
488+ const output = diagnosticUsage . output ?? 0 ;
489+ const cacheRead = diagnosticUsage . cacheRead ?? 0 ;
490+ const cacheWrite = diagnosticUsage . cacheWrite ?? 0 ;
491491 const usagePromptTokens = input + cacheRead + cacheWrite ;
492- const totalTokens = usage . total ?? usagePromptTokens + output ;
492+ const totalTokens = diagnosticUsage . total ?? usagePromptTokens + output ;
493493 const contextUsedTokens = deriveContextPromptTokens ( {
494494 lastCallUsage : runResult . meta ?. agentMeta ?. lastCallUsage ,
495495 promptTokens,
@@ -500,8 +500,13 @@ export async function prepareReplyAgentPayloads(state: {
500500 model : modelUsed ,
501501 config : cfg ,
502502 } ) ;
503- const costUsd = hasBillableUsageBuckets
504- ? estimateUsageCost ( { usage, cost : costConfig } )
503+ const hasDiagnosticBillableUsageBuckets =
504+ diagnosticUsage . input !== undefined ||
505+ diagnosticUsage . output !== undefined ||
506+ diagnosticUsage . cacheRead !== undefined ||
507+ diagnosticUsage . cacheWrite !== undefined ;
508+ const costUsd = hasDiagnosticBillableUsageBuckets
509+ ? estimateUsageCost ( { usage : diagnosticUsage , cost : costConfig } )
505510 : undefined ;
506511 emitTrustedDiagnosticEvent ( {
507512 type : "model.usage" ,
0 commit comments