From d393f23dfa06e54521e9ff1357fc38d11a3dd81f Mon Sep 17 00:00:00 2001 From: qqqys Date: Fri, 17 Apr 2026 10:37:13 +0800 Subject: [PATCH] fix(core): use output-only tokens and accumulate across subagent rounds Subagent token display had two bugs: - Used totalTokenCount (input+output) instead of candidatesTokenCount (output-only), causing mixed units when aggregated with main stream - Overwrote tokenCount per round instead of accumulating, so multi-round subagents only showed the last round's count Co-Authored-By: Qwen-Coder --- packages/core/src/tools/agent.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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, + ); } }, );