diff --git a/packages/core/src/tools/agent.ts b/packages/core/src/tools/agent.ts index 2e417f5a9..8dbc10189 100644 --- a/packages/core/src/tools/agent.ts +++ b/packages/core/src/tools/agent.ts @@ -486,16 +486,21 @@ class AgentToolInvocation extends BaseToolInvocation { }); // Track real-time token consumption from subagent API calls. - // Each USAGE_METADATA event carries the cumulative totalTokenCount for the - // subagent session, so we replace (not accumulate) on every event. + // Each USAGE_METADATA event carries per-round usage, so we accumulate + // output tokens across rounds. We use candidatesTokenCount (output-only) + // to stay consistent with the main stream's chars/4 output-token estimate. + let accumulatedOutputTokens = 0; this.eventEmitter.on( AgentEventType.USAGE_METADATA, (...args: unknown[]) => { const event = args[0] as AgentUsageEvent; - const total = - event.usage?.totalTokenCount ?? event.usage?.promptTokenCount ?? 0; - if (total > 0) { - this.updateDisplay({ tokenCount: total }, updateOutput); + const outputTokens = event.usage?.candidatesTokenCount ?? 0; + if (outputTokens > 0) { + accumulatedOutputTokens += outputTokens; + this.updateDisplay( + { tokenCount: accumulatedOutputTokens }, + updateOutput, + ); } }, );