Skip to content

[Bug] Auto-compaction never fires when Anthropic prompt cache hit rate is ~100% #66520

@Neomail2

Description

@Neomail2

Summary

Auto-compaction does not trigger automatically even when the context window is exceeded (observed at 153% = 305k/200k), as long as the Anthropic prompt cache absorbs most tokens.

Environment

  • OpenClaw version: 2026.4.12 (1c0672b)
  • Model: anthropic/claude-sonnet-4-6
  • Compaction mode: safeguard
  • reserveTokensFloor: 30000

Observed behavior

/status output at time of issue:

Model: anthropic/claude-sonnet-4-6
Tokens: 3 in / 140 out
Cache: 100% hit · 305k cached, 99 new
Context: 305k/200k (153%) · Compactions: 0

Context was at 153% of the model window (305k > 200k) with 0 compactions. Auto-compaction should have fired at the contextWindow - reserveTokens threshold (≈170k), but never did. The user had to run /compact manually.

Root cause hypothesis

Pi's compaction threshold check appears to use the new tokens counter (99 in this case) rather than the total context size (305k). When Anthropic's prompt cache absorbs nearly all tokens (100% hit rate), the incoming token count seen by Pi's runtime is near zero, so the threshold contextTokens > contextWindow - reserveTokens is never satisfied — even though the actual context far exceeds the window.

In other words: prompt cache efficiency masks the true context load from the Pi compaction engine.

Expected behavior

Auto-compaction should trigger based on total context window usage (305k/200k), not on the count of non-cached input tokens for the current turn.

Workaround

Manual /compact command. Enabling compaction.notifyUser: true has no effect since compaction never triggers.

Steps to reproduce

  1. Start a long session with anthropic/claude-sonnet-4-6 and compaction.mode: safeguard
  2. Let the session grow until prompt cache hits approach 100%
  3. Continue the session past the context window limit (200k tokens)
  4. Observe: /status shows Context > 100% with Compactions: 0
  5. Auto-compaction never fires — manual /compact required

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions