Advisor turns roll up usage summed across iterations, doubling apparent context; auto-compact fires ~300-500K tokens early (subagents hit hardest)

Status Open
Reported on v2.1.219
Maintainer reply None cached
Activity 1 comment · opened Aug 7, 2026

Summary

When a turn invokes the server-side advisor tool, the final assistant message's usage object rolls up token counts summed across all iterations of the request — the pre-advisor message iteration and the post-advisor message iteration. Since both iterations carry the seat's full context, the top-level input_tokens + cache_creation_input_tokens + cache_read_input_tokens comes out at ≈ 2× the real context size.

Auto-compact eligibility reads that rolled-up figure, so any session (or subagent) that consults the advisor while above roughly half its auto-compact threshold is compacted on the spot, hundreds of thousands of tokens before its real context justifies it.

This confirms the "suspected cause (unverified)" in #81029 with transcript evidence, and extends it: subagent (Task tool) seats are hit hardest, because in orchestrated setups they consult the advisor at checkpoints while carrying large working contexts — and they get force-compacted mid-task.

Environment

  • Claude Code 2.1.219 → 2.1.224 (rollup format first observed in transcripts on 2.1.219, ~Jul 27; still present on 2.1.224)
  • Windows 10 (win32), native installer
  • autoCompactWindow: 650000 — explicitly configured on this machine, not the default. This is why the healthy baseline auto-compactions below fired at preTokens ≈ 617–620K; all observed trigger points should be read against that setting, not a default window.
  • Seats observed: sonnet-5 subagents (largest population) and opus-5 main sessions. fable-5 mains also appear in the dataset, but none compacted through the advisor path — every fable compaction was a genuine ~615K threshold crossing. The advisor-inflated main-session compactions all occurred on opus-5 mains that had inherited a session originally started on fable-5 (mid-session model switch); one such session shows both fable-5 threshold-crossing compactions and an opus-5 advisor-inflated one in the same transcript.
  • advisorModel: opus-5

Evidence (transcript excerpt)

Subagent transcript ~/.claude/projects/<project>/<session>/subagents/agent-<id>.jsonl, one request (requestId req_011CdmQV…), consecutive entries:

Entry N (pre-advisor chunk of the same request) — real context ~515K:

"usage": { "input_tokens": 2, "cache_creation_input_tokens": 783, "cache_read_input_tokens": 515122, ... }

Entry N+1 (final chunk of the same request, after the advisor ran) — top-level usage now reports ~1.03M:

"usage": {
  "input_tokens": 4,
  "cache_creation_input_tokens": 3249,
  "cache_read_input_tokens": 1031027,
  "output_tokens": 428,
  "iterations": [
    { "type": "message", "input_tokens": 2, "cache_read_input_tokens": 515122, "cache_creation_input_tokens": 783, "output_tokens": 65 },
    { "type": "advisor_message", "model": "claude-opus-5", "input_tokens": 516328, "cache_read_input_tokens": 0, "cache_creation_input_tokens": 0, "output_tokens": 13610 },
    { "type": "message", "input_tokens": 2, "cache_read_input_tokens": 515905, "cache_creation_input_tokens": 2466, "output_tokens": 363 }
  ]
}

Note 1031027 = 515122 + 515905 — the two message iterations' cache reads summed. The same ~515K tokens counted twice. (The advisor's own 516,328 input is not what's inflating it.)

Four minutes later, with no further context growth:

{ "type": "system", "subtype": "compact_boundary", "compactMetadata": { "trigger": "auto", "preTokens": 1037179 } }

Real context at compaction time: ~516K. Apparent: ~1.03M.

Impact — quantified across one machine, 8 days of transcripts

Scanned every local session/subagent transcript (~1,000 JSONL files). Per-day auto-compactions, and how many fired on the turn immediately following an inflated advisor rollup:

| day | auto-compacts | right after inflated advisor turn | subagent share | avg (preTokens − real ctx) |
|---|---|---|---|---|
| Jul 27 – Aug 1 | 1–3/day | 0 | 0 | — |
| Aug 3 | 5 | 1 | 1/5 | 378K |
| Aug 4 | 13 | 5 | 7/13 | 354K |
| Aug 5 | 11 | 3 | 4/11 | 338K |
| Aug 6 | 23 | 11 | 17/23 | 377K |
| Aug 7 (partial) | 3 | 1 | 1/3 | 388K |

  • Every subagent auto-compaction in the window immediately followed an advisor turn with the doubled rollup. Real context at fire time ranged 310K–517K.
  • Main sessions show the same pattern when they consult the advisor at high context — observed on opus-5 mains (sessions started on fable-5, switched to opus-5 mid-session) jumping 376K→765K, 400K→802K apparent in a single turn and compacting immediately. No fable-5 main compacted via this path.
  • The bug is latent until real context exceeds ~half the auto-compact threshold, which is why heavy orchestration days (long-lived seats at 300–500K real context, advisor checkpoints per seat) suddenly produce a wave of spurious compactions — 23 in one day vs a 1–3/day baseline.

Downstream, anything that derives context occupancy from the last assistant message's usage (statuslines, context monitors, handoff heuristics) over-reports by ~2× for the window after an advisor turn — #81029 documents the meter staying inflated until the next main-loop response.

Expected behavior

  • Context accounting for auto-compact should use the request's final state — the last type: "message" entry of usage.iterations — never a sum across iterations of the same context.
  • Arguably the transcript's top-level usage fields should themselves reflect final-state context (with the rollup preserved under iterations for cost accounting), so downstream consumers don't need iteration-awareness.

Repro sketch

  1. Configure an advisorModel; run any session/seat to real context ≥ ~55% of its auto-compact threshold.
  2. Trigger an advisor consultation.
  3. Observe the final assistant usage roll up to ~2× context, and compact_boundary { trigger: "auto" } fire within the next turn(s), with preTokens ≈ the doubled figure.

Side observation

The advisor_message iteration shows the full transcript re-sent uncached (input_tokens: 516328, cache_read_input_tokens: 0). At high contexts every advisor checkpoint is a full-price input pass for the advisor model; caching the advisor's prompt prefix would materially reduce cost for advisor-heavy workflows.

Related

  • #81029 — main-session manifestation of the same root cause; its "suspected cause (unverified)" section matches what the iterations arrays above confirm.

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗