Context Engineering · 7 min read
Context rot: how long contexts fail, and how to keep agents on track
Models advertise huge context windows, but accuracy drops well before they fill up. Here's what the research says about context rot, why it hits agents hardest, and the practical techniques that keep long-running agents focused.
Context windows keep getting bigger, and it’s tempting to read that as “the model can use everything I give it.” It can’t. Long before the window is full, models get worse at finding, weighing and acting on what’s in it.
This has a name now: context rot. It’s the reason an agent that was sharp for the first twenty steps starts repeating itself, forgetting constraints or declaring victory early. This post covers what the research shows, why agents are especially exposed, and the techniques that help. (For the basics, see our posts on context engineering and prompt and context compression.)
What the research shows
The term was popularised by a Chroma technical report from July 2025 (Hong, Troynikov and Huber). They tested 18 models, including GPT-4.1, Claude 4, Gemini 2.5 and Qwen3, on deliberately simple tasks while varying only the input length. The findings are worth knowing individually, because each one maps to a real failure:
- Performance fell as input grew, even on trivial tasks. In one test models simply had to repeat a sequence of words; accuracy degraded consistently as length increased.
- Weak matches suffer most. When the “needle” was semantically less similar to the question, accuracy fell faster with length. Real questions rarely use the same words as the answer.
- Distractors hurt. One plausible-but-wrong passage lowered performance; four compounded the drop.
- Structure mattered in a surprising way. Across all 18 models, haystacks of shuffled sentences did better than logically coherent text.
- Focused beats full. On a LongMemEval-based test, models answered far better from a focused ~300-token prompt than from the full ~113k-token conversation containing the same information.
The authors stress that their tasks were simple, so real applications are likely to degrade more.
This builds on older work. The 2023 Lost in the Middle paper (Liu et al.) found that models used information best when it sat at the start or end of a long input, and worse when it was in the middle. Interestingly, Chroma’s needle experiments found no notable position effect — a reminder that where things go matters less than how much else is competing with them.
Why agents get hit hardest
A chatbot gets one long prompt. An agent builds its own long prompt, one tool call at a time: file contents, search results, logs, its own reasoning, error messages. Most of it is relevant for a step or two and then becomes noise.
LOCA-bench (February 2026, Zeng, Huang and He) measures exactly this. It keeps the task fixed while growing the environment the agent has to work through. The drop is steep:
| Model | 8K | 128K | 256K |
|---|---|---|---|
| Claude-4.5-Opus | 96.0% | 34.0% | 14.7% |
| GPT-5.2-Medium | 72.0% | 38.7% | 21.3% |
| Gemini-3-Flash | 64.0% | 21.3% | 17.3% |
| DeepSeek-V3.2-Thinking | 78.7% | 10.7% | 6.7% |
Same task, same model, much worse results — just from more context.
Context rot also affects models used as checkers. A May 2026 paper, Classifier Context Rot (Martin and Roger), tested frontier models (Opus 4.6, GPT 5.4 and Gemini 3.1) as monitors that read coding-agent transcripts to flag dangerous actions. When a dangerous action came after 800K tokens of benign activity, the monitors missed it 2× to 30× more often than when the action appeared on its own. If you use an LLM to review long agent runs, your evaluation of that reviewer on short examples probably overstates how good it is.
Why it happens
Anthropic’s engineering team describes context as a finite resource with “diminishing marginal returns.” Their explanation: transformers let every token attend to every other, which gives “n² pairwise relationships for n tokens,” so attention gets “stretched thin” as context grows. They frame it as an attention budget: “Every new token introduced depletes this budget by some amount.”
That framing is the useful one in practice. The goal isn’t to fill the window. It’s to find the smallest set of high-signal tokens that gets the job done.
Techniques that keep agents on track
1. Clear stale tool results
Anthropic calls tool-result clearing the lightest form of compaction: once a tool result is deep in the history, the agent rarely needs the raw output again. Keep a short note of what it showed and drop the rest.
def clear_old_tool_results(messages, keep_last=3):
tool_idxs = [i for i, m in enumerate(messages) if m["role"] == "tool"]
for i in tool_idxs[:-keep_last]:
messages[i]["content"] = "[cleared: result already used; re-run tool if needed]"
return messages
2. Compact before you hit the wall
Compaction summarises a conversation that is nearing the limit and starts a fresh window from the summary. Anthropic notes that Claude Code’s compaction keeps architectural decisions, unresolved bugs and implementation details. The hard part is choosing what to keep: compaction is lossy, so test it on your own long runs.
3. Write notes outside the context
Structured note-taking means the agent writes progress to a file — a to-do list, a NOTES.md — and reads it back later. Anthropic’s write-up on long-running coding agents shows why: without it, agents tried to do too much at once, ran out of context mid-feature, and the next session inherited “a half-built, undocumented feature.” Later sessions sometimes saw partial progress and declared the job done.
Their fix was a harness: a JSON feature list with every feature marked failing, a progress file, and git commits after each feature. Each new session starts by reading the progress notes and git log, then running a basic end-to-end test before doing anything new.
4. Retrieve just in time
Instead of loading every document up front, give the agent lightweight handles — file paths, stored queries, links — and let it load data with tools when it needs it. Anthropic notes this trades speed for autonomy, and that a hybrid (some context up front, the rest on demand) is often best.
5. Use sub-agents for focused work
A sub-agent explores in its own clean window and returns a condensed summary — Anthropic suggests often 1,000–2,000 tokens. The main agent never sees the noise.
But this isn’t free. In LOCA-bench, running Claude-4.5-Opus inside the Claude Agent scaffold lowered its 128K score from 34.0% to 26.7%; the authors attribute this partly to sub-agents being misused and accumulating irrelevant context. Sub-agents help when tasks are cleanly separable and the summaries are tight.
6. Let code handle the bulk data
In LOCA-bench, programmatic tool calling — where the model writes code that calls tools and processes their output, so large results never enter the context — gave some of the biggest gains at 128K: GPT-5.2-Medium went from 38.7% to 49.3%, DeepSeek-V3.2-Thinking from 10.7% to 24.0%. Using Anthropic’s official implementation, Claude-4.5-Opus reached 49.3%, up from 34.0%.
What the numbers say about trade-offs
The same LOCA-bench table holds a warning: no technique wins everywhere.
- Effects are model-specific. For Gemini-3-Flash, a “context awareness” feature and a memory tool beat tool-result clearing. For DeepSeek-V3.2-Thinking, both of those reduced accuracy (to 4.0% and 8.0% from a 10.7% baseline).
- Gains are real but partial. Even the best strategies left 128K scores far below the 8K ones. Context management slows rot; it doesn’t cure it.
- Summaries lose things. Every compaction or sub-agent summary is a decision about what to forget.
- Monitoring gets harder too. If an LLM reviews long transcripts, chunk them or remind it of its task periodically — Martin and Roger found periodic reminders partially reduced the problem.
Where to start
- Measure your own curve. Run your agent’s eval set at several context sizes (pad with realistic tool output) and see where quality drops. Don’t rely on the advertised window.
- Log context size per step. Most “the agent got confused” bugs line up with a bloated context.
- Add tool-result clearing first. It’s cheap, simple and rarely harmful.
- Give long tasks a progress file and a feature list, and have each session start by reading them.
- Move bulk data processing into code the agent runs, rather than pasting large outputs into context.
- Test compaction and sub-agents on your model. The research shows the same technique can help one model and hurt another.
Bigger context windows are useful, but they’re capacity, not comprehension. Agents that stay on track are the ones whose builders treat every token as something that has to earn its place.