ccstatusline icon indicating copy to clipboard operation
ccstatusline copied to clipboard

feat: total session tokens including sub-agents

Open elhoim opened this issue 5 months ago • 0 comments

Summary

Adds subagent-inclusive session token accounting. The existing token widgets count only the main agent — tokens spent by Task-tool sub-agents are invisible. This wires sub-agent usage into token counting, reusing the same sub-agent transcript discovery that already backs the speed-metrics widgets.

Two surfaces (opt-in, zero change for existing setups):

  • (s)ubagents toggle on the existing Tokens Input / Output / Cached / Total widgets. When on, that widget's number includes sub-agents, shows a Σ marker, and a [+sub] modifier in the editor.
  • New Session Total Tokens widget (tokens-session-total) — session grand total including sub-agents, with an optional (b)reakdown of input/output/cache: Σ Total: 152k (in 90k/out 40k/cache 22k).

How it works

  • getTokenMetrics(path, { includeSubagents }) sums referenced subagents/agent-*.jsonl files via the existing getReferencedSubagentIds → getSubagentTranscriptPaths discovery.
  • Default behavior is byte-identical — the option defaults off; existing widgets and the stdin context_window path are unchanged.
  • Double-count guard: when separate sub-agent files exist, inline isSidechain rows are dropped from the main pass (they're represented by the files); when no files exist (older transcript format), inline sidechain rows are still counted. contextLength remains a main-chain-only concept.
  • The subagent-inclusive figure is computed once per render, on demand — only when a session-total widget or a subagents-enabled token widget is configured (RenderContext.sessionTokenMetrics). No extra file I/O otherwise.

Testing

  • New unit tests: data layer (incl. double-count guard, multi/unreferenced sub-agents, back-compat lock), the metadata helper, the four widget toggles, and the new widget. 49 tests pass including the pre-existing TokensWidgets suite (no regression).
  • tsc --noEmit and eslint clean across the project; bun run build succeeds; example-payload smoke render verified.

🤖 Generated with Claude Code

elhoim avatar Jun 13 '26 13:06 elhoim