Skip to content

[Bug]: Agents panel token counts for Claude subagents are context sizes, not totals: compaction erases earlier work, and Σ adds up context sizes #14003

Description

@Vantrongs

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/web

Steps to reproduce

  1. In a Claude thread, run subagents long enough that at least one auto-compacts.
  2. Watch that subagent's row and the Σ total at the bottom of the Agents panel before and after its compaction.

Expected behavior

A row shows how much the subagent has used, and this number does not go down when it compacts; the current context can be shown separately. Σ adds up comparable numbers.

Actual behavior

For Claude subagents both the row and Σ are built from the subagent's context size on its latest request.

  • Row. usage.total_tokens from task_progress / task_notification equals the context of the subagent's last API request (input + cache write + cache read + output). In one thread, the values reported for three subagents, 217,946, 311,267 and 317,140, match the last request in their transcripts exactly. A subagent continued with SendMessage reports 358,641 → 482,812 → 628,985 → 653,032: a growing context, not deltas.
  • Compaction. One subagent auto-compacted (compact_boundary: preTokens 966,517, postTokens 14,683). Its row went from 969,529 to 44,138 in the next update and ended at 805,020. Over 883 requests it wrote 3.86M uncached tokens (input, cache writes and output) and read 410M from cache; the row shows 0.8M.
  • Σ. This is the plain sum of these per-row values. For that thread it shows 15.5M, while the subagents processed 40M tokens without cache reads, or 2.16B with them. It also mixes Claude context sizes with Codex cumulative totals (CodexAdapter maps tokenUsage.total).

Cause, from main (de251fc):

  • normalizeTaskUsage (apps/server/src/provider/Layers/ClaudeAdapter.ts) copies usage.total_tokens into typedUsage.totalTokens.
  • Ingestion writes it to one activity per task with a fixed id, task-usage:<thread>:<task> (ProviderRuntimeIngestion.ts, task.progress). Each update replaces the previous one, so the value from before a compaction is gone before the client sees it. mergeUsageMax in foldSubagentActivities cannot bring it back.
  • AgentsPanel.tsx shows agent.usage.totalTokens as "N tok"; Σ sums the same field across agents.
  • The comments disagree with the data and with each other: RuntimeTaskUsage (packages/contracts/src/providerRuntime.ts) says Claude reports "per-activation deltas"; the mergeUsageMax comment says it is "cumulative per task".

Suggested fix

  • Treat Claude's total_tokens as the current context size and fix both comments.
  • Keep a second, cumulative number per task on the server. Sum per-request usage if the SDK forwards it for subagents; otherwise at least carry the high-water mark across drops, so a compaction does not erase earlier work.
  • Show both in the row, for example 805k ctx · 3.9M processed · 1 compaction. Make Σ sum the cumulative number, or label it as current context.

Related: #12013 (closed PR that assumed the row was already cumulative for Claude), #4650 and #13659 (the same task_progress values inflate the main thread's context meter), #5793 (Codex subagent totals).

Impact

Minor bug or occasional failure

Version or commit

main @ de251fc; installed 0.0.43-nightly.20260926.2282

Environment

Desktop app on Linux (NixOS); Claude Code 2.1.283 through the Agent SDK

Logs or stack traces

Screenshots, recordings, or supporting files

No response

Workaround

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken or behaving incorrectly.via-triageFiled through npx t3 triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions