Skip to content

[BUG] Auto-compaction over-estimates context and re-fires every turn after a failed summary #1581

Closed

Description

@edenreich

Summary

Auto-compaction fires far too early and then re-fires on every turn until one summary succeeds, because the trigger estimate is roughly 2.4x the provider's own prompt_tokens and a failed compaction leaves no trace that it was tried.

Seen on an infer-action v0.58.0 run with CLI v0.233.0 and ollama_cloud/glm-5.3-flash (context window 1,000,000, compact-auto-at: 50): https://github.com/edenreich/das-ist-berlin/actions/runs/39459666409

Steps to Reproduce

  1. Run infer agent with a reasoning model that returns reasoning_content on every turn (glm-5.3-flash) and INFER_COMPACT_AUTO_AT=50 on a 1M-window model.
  2. Let it read a few large files and call ~100 tools, so the real prompt reaches about 200k tokens.
  3. Watch the compaction_started / compaction_completed debug events against the token_usage.prompt_tokens the gateway reports.

Expected Behavior

  • current_tokens in compaction_started is close to the provider's last prompt_tokens, so a 50 percent threshold on a 1M window trips near 500k real tokens.
  • One compaction per crossing. If the summary call fails, the failure is logged as an event and the next turn does not immediately try again with the same context.

Actual Behavior

The gateway reported at most 265,907 prompt_tokens (median 177,887 over 114 requests), but the CLI's trigger estimate went 497k, 528k, 535k, 599k, 621k, 633k and fired six compaction_started events in six minutes before one compaction_completed (153 messages to 12):

{"auto_at_pct":50,"context_window":1000000,"current_tokens":497420,"force":true,"messages_before":139,"threshold":500000,"timestamp":"2026-10-20T17:04:57Z","type":"compaction_started"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":528097,"force":true,"messages_before":143,"threshold":500000,"timestamp":"2026-10-20T17:05:52Z","type":"compaction_started"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":535362,"force":true,"messages_before":146,"threshold":500000,"timestamp":"2026-10-20T17:07:37Z","type":"compaction_started"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":599009,"force":true,"messages_before":149,"threshold":500000,"timestamp":"2026-10-20T17:07:49Z","type":"compaction_started"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":621851,"force":true,"messages_before":151,"threshold":500000,"timestamp":"2026-10-20T17:08:50Z","type":"compaction_started"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":633329,"force":true,"messages_before":153,"threshold":500000,"timestamp":"2026-10-20T17:10:18Z","type":"compaction_started"}
{"messages_after":12,"messages_before":153,"timestamp":"2026-10-20T17:10:25Z","type":"compaction_completed"}
{"auto_at_pct":50,"context_window":1000000,"current_tokens":502734,"force":true,"messages_before":112,"timestamp":"2026-10-20T17:22:04Z","type":"compaction_started"}

The last one never completed either. Twelve minutes after the big compaction the agent was back over the (mis-estimated) threshold with 112 messages. Each attempt is a full summarisation request to the model, and after the one that succeeded the agent spent several turns re-orienting ("I've been context-compacted again. Let me re-establish state").

What the code does (internal/conversation):

  • When smartOptimize fails (summary request error, empty summary) OptimizeMessages logs and returns the messages unchanged. Nothing is emitted, so the stream shows compaction_started with no end, and the next turn crosses the same threshold and tries again. Fixed in fix(compaction): emit a failure event and back off after a failed summary #1584.
  • The trigger estimate (estimateTriggerTokens, the max of the gateway's LastInputTokens and TokenizerService.EstimateMessagesTokens) reported 497k to 633k while the provider reported at most 266k prompt_tokens on the same turns. Why the two diverge this much is not established. To find out: log both numbers per turn on a reasoning-heavy run and compare which messages the estimate charges that the provider does not.
  • The events above say force: true. Session rollover (session_rollover.go) gates on the same estimate and compacts with force, so it is the likely caller. To be confirmed from the logger output of a run.

Activity

  1. added
    bug
    on Oct 20, 2026
  2. added theissue type on Oct 20, 2026
  3. moved this from Todo to QA in 2026on Oct 20, 2026
  4. moved this from QA to Done in 2026on Oct 20, 2026
  5. inference-gateway-releaser commented on Oct 20, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.233.2 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

    bugreleased

    Type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions