← ~/pillars / context-engineering / compaction-and-context-rot
Compaction and context rot
What actually happens when the window fills, why compaction is lossy by design, and the externalisation patterns that keep multi-hour agent sessions coherent.
updated 2026-07-01 ~3 min read pattern 05/05 Context Engineering
on this page
Context
Every long agent session ends the same way if you let it: the window fills with file reads, tool output, and half-relevant history; attention spreads thinner across more tokens; and the agent starts re-deciding things it already decided. Anthropic calls the underlying effect context rot: as the token count grows, the model’s ability to accurately recall any given token shrinks, so a bloated window is not a bigger brain, it is a noisier one (effective context engineering).
The harness’s answer is compaction: when the window approaches its limit, Claude Code summarises the oldest history and continues on the summary (how Claude Code works). Compaction keeps the session alive. It does not keep the session’s knowledge: a summary is lossy by design, and what it drops is precisely the detail (the exact error text, the reason a path was rejected, the file you already ruled out) that stops an agent repeating work.
Forces
- Window size versus signal density. More context is only better while it is high-signal; past that point every added token dilutes the ones that matter.
- Compaction is automatic and unscheduled. It fires when the budget says so, not when the task reaches a clean boundary, so anything living only in the conversation can be summarised away mid-thought.
- Early decisions are the most at risk. The oldest turns compact first, and the oldest turns are usually where the plan, the constraints, and the rejected alternatives live.
- Exploration is the biggest polluter. Reading twenty files to find the right three fills the window with seventeen files of noise that compaction will eventually eat, along with the useful three.
Resolution
Budget context deliberately, and externalise what must survive.
Write decisions to files, not to the conversation. A plan file or NOTES.md updated as the work progresses is immune to compaction: the agent can re-read it at full fidelity at any point. Anthropic’s guidance calls this structured note-taking: persisting state outside the window so memory survives the window’s limits (effective context engineering). The rule of thumb: if losing a fact would cost more than a sentence to rediscover, it belongs in a file.
Do exploratory work in sub-agents. A sub-agent gets a fresh window, burns it on the noisy search, and hands back only a condensed conclusion. The main session pays a paragraph for what would have cost it twenty file reads. This is the context-isolation half of the orchestration pillar, applied defensively.
Prefer /clear at task boundaries over compaction mid-task. A deliberate reset at a clean boundary, with the plan and state already in files, starts the next task sharp. An automatic compaction mid-task keeps a degraded paraphrase of everything. Clearing feels wasteful and is usually the opposite (best practices).
Retrieve just in time instead of pre-loading. Loading documents “in case they help” spends the budget before the work starts. An agent that can navigate (via a repo map, a module index, an @import) pulls what it needs at the moment of need, at full relevance.
Worked example
A multi-hour refactor across a service’s persistence layer, run as one session, goes like this:
- Before starting, the agent writes the plan to a file: modules in scope, the invariant being preserved, the verification command. Frontmatter in that file, not conversation history, is now the source of truth.
- During exploration, “find every caller of the deprecated repository API” is dispatched to a sub-agent, which returns a twelve-line list. The main window never sees the forty files the sub-agent read.
- At each milestone, the plan file’s checklist is updated: done, blocked, decision taken and why. Cost: a few dozen tokens per update. Value: the session can compact, crash, or be resumed tomorrow without losing state.
- At the natural boundary (persistence layer done, service layer next), the session is cleared deliberately rather than left to auto-compact mid-module.
This site runs the same discipline on itself: the weekly refresh agent works
inside a fixed 30-turn budget, reads its state from pipeline/sources.yaml
and the rubric at the start of every run, and writes its conclusions into a
PR description rather than relying on anything surviving in-session. The
session is disposable; the files are not.