Repository navigation
Description
Summary
Automatic compaction triggers only when a heuristic estimate of the request reaches 0.8 × the context window. On tool- and code-heavy histories the default estimate (bytes / 4) undercounts badly, so compaction can fire only after the real request is larger than the window. The engine already receives the real input token count every turn, but the trigger never uses it.
Evidence
From one mecatui session (claude-opus-5-5 through a gateway, every request manifest reporting context_window: 128000, default --compaction=heuristic --tokenizer=heuristic):
- The trigger threshold was 0.8 × 128,000 = 102,400 estimated tokens.
- Over 8 runs, provider-reported input per turn grew from 40K to 153.6K with no compaction.
- For the last request before compaction, the request manifest recorded 334,821 message bytes, 37,482 system/instruction bytes, and 22,736 tool schema bytes. At bytes / 4 that's about 99K estimated tokens, below the trigger. The provider counted 153,591 for the same request, so the heuristic undercounted by about 35%.
- Compaction finally fired on the next prompt, when the estimate crossed 102.4K. Input then dropped to 45.7K.
The session only kept working because the model behind the gateway actually has a larger window than the 128K it reports. Against a model with a real 128K window, requests would start failing long before compaction ran.
Where
engine/agent/loop.gomaybeCompactcomparesestimateRequestTokens(...)againstwindow * CompactionRatioand nothing else.engine/agent/tokencount.goHeuristicTokenCounterusescharsPerToken = 4.cmd/mecated/main.godefaults:--tokenizer=heuristic,--compaction=heuristic.
Possible approaches
- Use provider truth as a floor. Trigger on the larger of the heuristic estimate and the last turn's reported
InputTokensplus the estimated size of messages added since that turn. The last turn's input count is authoritative for everything up to that point, so only the new suffix needs estimating. - Calibrate the heuristic. Derive the chars-per-token ratio for the session from observed (request bytes, reported input tokens) pairs, and use that for the trigger and the cascade target budget.
- Change the default tokenizer. Making
tiktokenthe default would help, but it's still a proxy for Anthropic tokenization and wouldn't catch provider-specific overhead, so it doesn't replace either approach above.
Whichever approach is chosen, the cascade's target budget (compactionTargetRatio) comes from the same estimate and would need the same correction. Otherwise a compaction pass may not bring the real request under the target.
Related
- mecatui: context and token counters are not restored on session resume #1822: this is why the context counter dropped sharply on the first turn after resume.
- token accounting: investigate inflated input/context counts and fix footer semantics #25, feat(budget): use weighted token-cost accounting for token ceilings #687: token accounting, but not the compaction trigger.
Activity