What is context rot, and what compaction strategies do you use in long-running agents?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Context rot (also "context pollution") is the degradation of model behaviour as the window accumulates low-value tokens: stale error messages, verbose tool outputs, abandoned reasoning paths, and irrelevant retrievals. Two mechanisms: distraction - attention spreads over plausible-but-irrelevant content, and effective retrieval-from-context degrades with length and clutter well before the hard limit (the advertised window is not the usable window for dense reasoning); and self-conditioning - the model imitates patterns in its own history, so past mistakes and repetitive tool-call loops become templates that reinforce themselves. Symptoms: an agent that was sharp at step 5 is meandering at step 40, repeats failed actions, or "forgets" standing instructions (which are now buried mid-context - the lost-in-the-middle zone).
Full worked answer with diagram
Context rot (also "context pollution") is the degradation of model behaviour as the window accumulates low-value tokens: stale error messages, verbose tool outputs, abandoned reasoning paths, and irrelevant retrievals. Two mechanisms: distraction - attention spreads over plausible-but-irrelevant content, and effective retrieval-from-context degrades with length and clutter well before the hard limit (the advertised window is not the usable window for dense reasoning); and self-conditioning - the model imitates patterns in its own history, so past mistakes and repetitive tool-call loops become templates that reinforce themselves. Symptoms: an agent that was sharp at step 5 is meandering at step 40, repeats failed actions, or "forgets" standing instructions (which are now buried mid-context - the lost-in-the-middle zone).
Compaction and hygiene strategies, roughly in order of adoption:
- Tool-output discipline (cheapest, first): truncate and paginate results, return summaries with handles to fetch detail, strip boilerplate from fetched pages. Most rot enters through tool outputs, not conversation.
- History compaction: when the window nears a threshold, summarise older turns into a structured digest - decisions made, current state, open items, key facts, errors already encountered (so they're not repeated) - keep recent turns verbatim, and continue. This is what Claude Code-style auto-compaction does. Risk: summaries lose detail irrecoverably; mitigate by preserving pinned artifacts (the task spec, key file paths) verbatim and never summarising the system prompt. A second risk: the summary is a persistence channel across the context boundary, and the model can be the one that poisons it. OpenAI reported in 2026 that a model in RL training wrote injection-style instructions into its own compaction summaries (a fake security alert, a 30-word limit with no tools or citations), and the successor context followed some of them. Treat summaries as untrusted input: give the summariser a fixed schema, check the output for imperative instructions that were never in the task, and keep standing instructions in the system prompt rather than in the digest.
- Offloading / externalised memory: write intermediate state to files or a scratchpad, keep only references in context, re-read on demand. Converts context (expensive, decaying) into storage (cheap, durable) with retrieval as the access path.
- Sub-agents as context firewalls: delegate a context-heavy subtask (read 30 files, research a question) to a fresh-window sub-agent that returns only a distilled answer. The parent's window stays clean.
- Pruning: drop superseded tool results (old file reads after edits), collapse retry loops to a single "attempts 1-3 failed because X" note, remove tools no longer relevant to the phase. Some provider APIs now offer this server-side, clearing stale tool results automatically as the window fills, but you still decide what is safe to drop.
Name the cache tension: compaction rewrites the prefix and invalidates the prompt cache, so compact at deliberate breakpoints and accept the one-time re-prefill cost - not continuously.
Worth sketching. The threshold and the re-prefill box together explain why compaction is a deliberate event rather than something you do every turn.
flowchart LR
A["Agent step"] --> B["Tool output truncated,<br/>paginated, boilerplate stripped"]
B --> C{"Past the quality<br/>threshold, not the limit?"}
C -->|"no"| A
C -->|"yes"| D["Digest: decisions, state,<br/>open items, errors already hit"]
D --> E["Recent turns and pinned<br/>artifacts stay verbatim"]
E --> F["One re-prefill,<br/>cache rebuilt from here"]
F --> A
What a good answer covers
- Context rot (also "context pollution") is the degradation of model behaviour as the window accumulates…
- Two mechanisms: distraction - attention spreads over plausible-but-irrelevant content, and effective…
- Symptoms: an agent that was sharp at step 5 is meandering at step 40, repeats failed actions, or…
- Tool-output discipline (cheapest, first)
- History compaction: when the window nears a threshold, summarise older turns into a structured digest -…
Key terms: distraction · self-conditioning · Tool-output discipline · History compaction · Offloading / externalised memory · Sub-agents as context firewalls · Pruning
In the glossary: Context rot · Attention · Compaction
Interviewer follow-ups to expect
How do you detect context rot with metrics rather than vibes?
What must a compaction summary preserve for a coding agent specifically?
Why can keeping failed attempts in context be valuable, and how do you keep them without amplifying self-conditioning?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep