Skip to content

Compaction trigger ignores provider-reported input tokens, so it fires well past the context window #1823

Open

Description

@danbarr

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.go maybeCompact compares estimateRequestTokens(...) against window * CompactionRatio and nothing else.
  • engine/agent/tokencount.go HeuristicTokenCounter uses charsPerToken = 4.
  • cmd/mecated/main.go defaults: --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 InputTokens plus 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 tiktoken the 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

Activity

  1. added
    bug
    on Sep 23, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions