Define context pollution in a multi-step agent and explain why it degrades reasoning quality. Describe three distinct mitigation strategies with their tradeoffs.
Context pollution is the buildup of stale, noisy, or contradictory history that drowns the signal an agent needs. Fix it by compacting, summarising, and retrieving instead of appending.
Imagine taking notes on one long scroll while solving a hard problem. Every wrong turn, every dead end, every typo stays on the scroll. By hour two the scroll is enormous, and your useful notes are buried between eight crossed-out attempts and a paragraph you no longer care about. When you glance back to decide your next move, your eye lands on the failures instead of the goal, and you repeat a mistake. An agent has the same scroll: its context window. Each step appends more text, including failed tool calls and outdated results. Eventually the noise outweighs the signal, and the model reasons worse than it did at step three. The fix is to keep the scroll tidy: summarise old sections, drop dead ends, and pull back only the notes that matter right now.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
A long-running agent has exactly one place to keep what it knows: its context window. Every step appends to it. The original goal, each reasoning trace, every tool call and tool result, every error, and every retry all accumulate in one growing transcript that is replayed to the model on the next turn.
Context pollution is what happens when that transcript fills with content that hurts more than it helps. Stale observations that are no longer true, eight identical failed retries, multi-kilobyte raw tool dumps, and abandoned reasoning branches all stay in the window by default. The agent's useful signal becomes a small fraction of a large, noisy prompt.
The critical point for an interview is that this is a reasoning-quality problem, not merely a cost or token-limit problem. Even with a window large enough to hold everything, a polluted context makes the model reason worse.
Why pollution degrades reasoning
Two distinct mechanisms cause the damage. The first is the lost-in-the-middle effect. Transformer language models attend most reliably to content at the very start and very end of a long context, and systematically under-weight material buried in the middle. As the transcript grows, the original goal and key early constraints drift into that neglected middle band, so the model effectively stops seeing them.
The second mechanism is active misdirection. Stale and contradictory entries do not just waste space, they pull the next decision the wrong way. If a failed tool call sits in context, the model may treat it as a viable option and retry it. If an observation has been superseded, the model may reason from the outdated value as though it still holds.
Together these mean a run can get measurably worse over time. The agent that reasoned cleanly at step three starts looping, hallucinating consensus from contradictory notes, or ignoring a constraint by step thirty. Enlarging the window does not help, because lost-in-the-middle scales with length rather than being cured by it.
It also helps to separate pollution from plain overflow. Overflow is a capacity event: the transcript simply exceeds the model's token limit and gets hard-truncated. Pollution is a quality event that strikes well inside the limit, when the ratio of relevant to irrelevant tokens has collapsed even though there is room to spare. The two demand different responses. Overflow needs any size reduction; pollution needs the right tokens removed and the right ones kept, which is a harder and more selective problem.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Anthropic's Claude Code compacts long agent conversations, summarising earlier turns when the context approaches its limit so a coding session can run for hours without the goal scrolling away.
- Claude Opus 4.7 agents and Gemini 3.1 Pro both ship long context windows, yet teams still summarise and prune because raw recall degrades in the middle of very long transcripts.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you decide which context entries to keep versus drop without an expensive relevance model on every turn?
Combine cheap heuristics with structure: pin the goal and constraints, tag entries by type and recency, collapse superseded observations, and reserve a learned or embedding-based relevance score only for the ambiguous middle tier rather than scoring everything.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating pollution as only a token-cost problem. The deeper harm is degraded reasoning: stale and contradictory entries actively mislead the model, not just inflate the bill.
60 second bullets to scan on the way to the call.
Define context pollution and distinguish it from plain context overflow.
Explain the lost in the middle effect and why it worsens as context grows.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.