mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-05-19 07:54:38 +00:00
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 <noreply@qwen.ai>
This commit is contained in:
parent
fd2767c9db
commit
d393f23dfa
1 changed files with 11 additions and 6 deletions
|
|
@ -486,16 +486,21 @@ class AgentToolInvocation extends BaseToolInvocation<AgentParams, ToolResult> {
|
|||
});
|
||||
|
||||
// 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,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue