Every serious Claude Code operator hits this eventually.
The session was running cleanly — autonomous work, good tool use, coherent reasoning. Then it crossed a token threshold, compaction fired, and when the agent came back, something was different.
It was missing decisions it had already made. It started re-asking questions it had already answered. It looped on a task it had already completed. Or worse: it continued with apparent confidence, but subtly wrong — acting on context that no longer existed the way it remembered it.
This is a context death spiral. It's not a model bug. It's an architecture gap.
What Actually Happens During Compaction
Claude Code's autocompact system triggers when the conversation approaches the model's context window limit. It summarizes earlier parts of the session to create space for new tokens.
The summary is good but lossy. Specific decisions, exact states, intermediate results — these can survive or disappear depending on how the summary is constructed. When they disappear, the agent doesn't know. It continues with a subtly incomplete picture.
The result is coherence degradation: responses that are structurally valid but operationally wrong. The agent passes all the surface checks and fails the mission.
Why Architecture Fixes This (And Prompting Doesn't)
The instinct is to add instructions: "Always summarize your state before any long operation." "When you resume, first verify your context." These help at the margin. They don't close the gap.
The problem is structural. The compaction fires without the agent's control. The summary loses information without the agent's knowledge. No instruction set fully compensates for a guarantee that doesn't exist.
What works is a pre-execution gate — architecture that intercepts the autocompact trigger before it fires and enforces conditions that make compaction safe:
- Verify the session is at a clean task boundary (not mid-operation)
- Serialize critical state to disk before compaction
- Block compaction if conditions aren't met
- After compaction, execute a cleanup sequence before resuming task work
- If compaction fails three consecutive times, trip the circuit breaker and surface the failure
This is the difference between "hoping the summary preserves what matters" and "guaranteeing clean state before context is reduced."
The Threshold Architecture
Production-verified thresholds (from live autonomous sessions, not documentation):
- 160k tokens: Soft warning — optional checkpoint, serialize important state
- 167k tokens: Autocompact gate — the 6-condition check fires here
- 177k tokens: Hard block — full abort if gate conditions aren't met
The 6-condition gate:
- Current task is at a clean boundary (no mid-operation state)
- All critical decisions are serialized to disk
- Active file handles are closed
- In-progress tool calls have resolved
- Session checkpoint has been written
- No concurrent agent is holding a shared lock
Every condition must pass. One failure = no compaction until the condition is resolved. This is the only way to make the guarantee durable.
The 3-Strike Circuit Breaker
If compaction fails three consecutive times — gate fires but conditions never clear — the circuit breaker trips.
On trip:
- All active operations halt
- Failure reason is logged with full context
- Session state is serialized to recovery file
- Operator is notified (Telegram, log entry, or both)
Without this: the agent degrades silently session after session, context window shrinking with no clean resolution. The circuit breaker makes the failure visible instead of invisible.
Implementation
The full architecture — including all 6 gate conditions with pass/fail logic, the 3-strike circuit breaker implementation, the post-compaction cleanup sequence, and freshTailCount calibration for your session depth — is in the Agent Compaction Architecture SKILL.md.
Drop it into your agent's context. It works immediately.
The conceptual foundation (if you want to understand the mechanism before installing the architecture) is in Context Death Spiral Prevention — free.
Compaction Is Not the Only Failure Mode
Context death spirals are one of three failure modes that reliably destroy otherwise solid Claude Code deployments. The other two are unguarded shell execution and runaway agent loops.
If you're building toward production reliability, you likely need all three architectural fixes. They're bundled together with an integration guide in the Production Agent Ops pack ($69) — cheaper than buying them separately, and the integration guide covers how the three systems interact.
By @thebrierfox | Built from production Claude Code deployments running on IntuiTek¹ infrastructure
Top comments (0)