What is context pollution in a long-running agent and what mitigation strategies are available?
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.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Define pollution and separate it from overflow, name lost-in-the-middle and stale-entry misdirection as the two harm mechanisms, then walk summarisation, compaction, scratchpad memory, and sub-agent isolation with one tradeoff each, and close on retrieval over replay.
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.
- LangGraph exposes state reducers and checkpointing so a graph can trim or summarise message history per node instead of appending blindly across loop iterations.
- OpenAI's Swarm and multi-agent supervisor patterns isolate subtasks in child agents that return only a distilled result, keeping the orchestrator's context clean.
- Cursor and Cline coding agents store full file contents in a side index and inject only the relevant diff or symbol into the prompt rather than the whole repository.
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?
QWhat can go wrong when rolling summarisation runs on an already polluted context, and how do you guard against it?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.