Research study
Context rot: how to keep a long-running agent sharp
A bigger context window does not fix a long agent run. We checked the research and the vendor guides on compaction, notes files and subagents. Here is what holds up, what breaks, and a recipe you can copy.
TL;DR
- Long context rots. Chroma tested 18 models. Every model did better on a focused prompt of about 300 tokens than on the full prompt of about 113k tokens with the same answer inside.
- No single method wins. Compaction loses detail. Notes files only hold what the agent writes down. Subagents cost about 15x the tokens of a chat and can make conflicting choices.
- Use them as layers. Notes first. Clear stale tool output next. Delegate read-heavy work, not decisions. Compact last, as a safety net.
- The best public number backs layering. In Anthropic's agentic search eval, context editing alone gave 29% better results. Context editing plus a memory tool gave 39%.
The problem: more tokens, less focus
An agent runs in a loop. Each turn adds tool calls, tool results and messages. After 100 turns, most of the context is old search results and file reads that no longer matter.
Anthropic calls the result context rot: as the token count grows, the model's ability to recall information from the context goes down. The post says this shows up in all models. Some just degrade more gently than others.
LangChain's memory docs say the same in plain words: even if a model supports the full context length, most LLMs still perform poorly over long contexts. They get distracted by stale or off-topic content, and they get slower and cost more.
So the question is not "how do I fit more in?" It is "what is the smallest set of tokens the agent needs right now?"
What the evidence shows
1. Length alone hurts, even on easy tasks
Chroma's Context Rot report (July 2025) tested 18 models, including GPT-4.1, Claude 4, Gemini 2.5 and Qwen3. The tasks were kept simple on purpose, so length was the main variable.
- On LongMemEval, the full prompts averaged about 113k tokens. The focused prompts held only the relevant parts and averaged about 300 tokens. Across all models, scores were much higher on the focused prompts.
- Even a single distractor (text that looks related but does not answer the question) cut performance. Four distractors cut it more. The effect grew with input length.
- Models failed in different ways. Claude models tended to abstain when unsure. GPT models showed the highest rates of confident wrong answers when distractors were present.
For agents, old search results and half-relevant file reads are exactly this kind of distractor.
2. Position matters
Lost in the Middle (Liu et al., Stanford) found a U-shaped curve. Models use information best when it is at the start or the end of the context, and worst in the middle. With 20 documents, GPT-3.5-Turbo scored 75.8% when the answer was in the first document and 53.8% when it was in the 10th. That is below its 56.1% closed-book score, with no documents at all.
The same paper found that going past 20 retrieved documents improved GPT-3.5-Turbo by only about 1.5%, while adding latency and cost.
3. Clearing stale context works, and pairs well with memory
Anthropic's context management results are the closest thing to a head-to-head test of these methods. On an internal agentic search eval:
- Context editing (automatic clearing of stale tool calls and results) improved performance by 29% over baseline.
- The memory tool plus context editing improved it by 39%.
- In a 100-turn web search eval, context editing cut token use by 84% and let agents finish runs that would otherwise fail.
These are vendor numbers on an internal eval, so treat them as direction, not as a benchmark. But the direction is clear: removing stale content helps, and adding persistent notes on top helps more.
4. Subagents scale work, at a price
In Anthropic's multi-agent research system, a Claude Opus 4 lead with Claude Sonnet 4 subagents beat a single Claude Opus 4 agent by 90.2% on an internal research eval. On BrowseComp, token usage alone explained 80% of the performance variance. Separate context windows are a way to spend more tokens on a problem.
The cost is real: agents used about 4x more tokens than chat, and multi-agent systems about 15x. The post also says most coding tasks have fewer truly parallel parts than research, and that agents are not yet good at coordinating with each other in real time.
The three methods
Method 1: Compaction
How it works. When the conversation nears the limit, the model summarizes it and a new context window starts from the summary. In Claude Code, the summary keeps architectural decisions, unresolved bugs and implementation details, drops redundant tool output, and carries over the five most recently accessed files (Anthropic). OpenAI's Responses API can do this on the server when the context passes a compact_threshold you set (OpenAI compaction docs). LangChain has a SummarizationMiddleware that replaces older messages with a summary (LangChain docs).
The lightest form. Clear old tool results. Once a tool result is deep in the history, the agent rarely needs the raw output again. Anthropic calls tool result clearing one of the safest, lightest forms of compaction.
When it fails.
- It drops the detail that matters later. Anthropic warns that overly aggressive compaction can lose subtle context whose importance only shows up later.
- It is not enough on its own. In Anthropic's long-running harness work, Opus 4.5 with compaction still fell short of building a production-quality web app from a high-level prompt. Sessions ran out of context mid-feature, and the next session had to guess what happened. Compaction "doesn't always pass perfectly clear instructions to the next agent."
- You may not be able to read it. OpenAI's compaction item is encrypted and "not intended to be human-interpretable." You cannot check what it kept.
- Errors stack. If you compact a summary of a summary with nothing else to anchor it, small losses add up. Cognition notes that a good compressor is "hard to get right" and that you will still hit a limit (Cognition).
Method 2: Offloading to notes
How it works. The agent writes notes to a file outside the context window and reads them back later. Anthropic calls this structured note-taking: a to-do list, or a NOTES.md the agent keeps up to date. Claude's memory tool does the same through a file directory that persists across conversations (Anthropic).
The strongest example is Anthropic's long-running agent harness. The first session writes an init.sh, a claude-progress.txt log and a feature list (over 200 features for a claude.ai clone), all marked failing. Every later session starts the same way: read the progress file and the git log, pick one failing feature, test, commit, update the notes. Anthropic says the key insight was letting a fresh context window understand the state of work quickly.
When it fails.
- The agent forgets to write. A notes file only holds what the agent puts in it. If the run ends mid-task, the next session sees a gap. The harness fixes this by working one feature at a time and writing notes and a git commit at the end of each one.
- The agent edits what it should not. Anthropic moved the feature list to JSON because the model was less likely to change or overwrite JSON than Markdown. Agents may only flip a
passesfield. - The agent declares victory early. A later session saw progress and said the job was done. The fix was a full feature list with every item marked failing until tested end to end.
- The notes file rots too. If it only grows, it becomes another long, noisy context. Keep it short and rewrite it, do not just append. (This is our advice, based on the context rot findings above.)
Method 3: Delegation to subagents
How it works. A lead agent hands a focused task to a subagent with a clean context. The subagent may use tens of thousands of tokens exploring, but returns only a condensed summary, often 1,000 to 2,000 tokens (Anthropic). The search noise never reaches the lead.
When it fails.
- Vague briefs. Anthropic's early lead agent gave short instructions like "research the semiconductor shortage." Subagents misread the task or repeated each other's searches. Each subagent needs an objective, an output format, guidance on tools and sources, and clear task boundaries (Anthropic).
- Conflicting decisions. Cognition's example: one subagent builds a Super Mario style background, another builds a bird that does not match. Their rule: "Actions carry implicit decisions, and conflicting decisions carry bad results" (Cognition).
- Over-spawning. Early versions of Anthropic's system spawned 50 subagents for simple queries. They had to add scaling rules: 1 agent with 3 to 10 tool calls for simple fact-finding, more only for complex research.
- Cost. About 15x the tokens of chat. Only worth it when the task is valuable enough.
- Lossy hand-offs. Passing everything back through the lead is a "game of telephone." Anthropic has subagents write outputs to files and return lightweight references.
Where the two camps agree. Cognition argues against multi-agent systems, yet notes (as of June 2025) that Claude Code subagents work because they mostly answer a well-defined question and do not write code in parallel. Their investigative work stays out of the main history. That is the safe zone: delegate reading and searching, keep decisions in one thread.
Side by side
| Method | Best for | Main failure | Cost |
|---|---|---|---|
| Compaction | Long back-and-forth conversations | Loses detail; summaries of summaries drift | One extra model call per compaction |
| Offloading (notes, memory) | Iterative work with clear milestones | Only as good as what gets written; files bloat | Low: a few file reads and writes |
| Delegation (subagents) | Broad research, parallel exploration | Vague briefs, conflicting decisions | High: about 15x chat tokens |
The combined recipe
Our starting thesis was "notes first, delegate the noisy work, compact last." The evidence supports it with two changes: clear stale tool output before you reach for full compaction, and delegate reading, not deciding. Anthropic describes the same layered pattern for production agents with hundreds of turns: summarize finished phases, store key facts in external memory, and spawn fresh subagents with careful hand-offs when the limit nears (Anthropic).
- Notes first. Create the notes file before the first real step. Read it at the start of every session and after every compaction. Update it at each milestone.
- Clear stale tool output. Drop old tool results once they are used. Keep a reference (path, URL, query) instead of the raw output.
- Delegate read-heavy work. Send searches, large file reads and log digging to a subagent with a full brief. Ask for a short result, or a file path to a longer one.
- Keep decisions in one thread. The lead agent makes the calls and writes them to the notes file with the reason.
- Compact last. When the window still fills up, compact. Because the notes file holds the key facts, the summary only needs to cover the conversation.
- Put the important things at the edges. Load the notes file at the start of the context and the current task at the end, not in the middle.
Notes-file template
Keep it under one screen. Rewrite it, do not just append. Use JSON for anything the agent must not rewrite freely, such as a task list with pass or fail status.
# NOTES.md (agent working memory, read at every session start)
Last updated: 2026-11-16 14:20 UTC, session 7
## Goal
One sentence. What "done" looks like for the whole job.
## Done when
- [ ] Check 1 that proves the goal is met
- [ ] Check 2
## Never
- Things the agent must not do (send, delete, spend, push to main)
## Decisions (newest first, with the reason)
- 2026-11-16: Use Postgres, not SQLite. Reason: need concurrent writers.
- 2026-11-15: Skip the v1 API. Reason: it is deprecated in March.
## Current state
- What works now, verified how (test name, screenshot path)
- What is half done, and where it stopped
## Open questions
- Question, who can answer it, what is blocked until then
## Next step
- The single next action for the next session
## References (load on demand, do not paste)
- tasks.json (pass/fail status per task)
- git log --oneline -20
- docs/spec.md, logs/run-07.txt
A subagent brief that works
Objective: Find which of our 3 queue libraries supports exactly-once delivery.
Scope: Official docs and changelogs only. Do not edit any file.
Tools: web search, fetch. Max 15 tool calls.
Output: Table: library | supports it? | quote | link. Under 300 words.
Put raw notes in research/queues.md and return the path.
Stop when: All 3 are answered, or you hit the tool-call limit.
Checklist
- A notes file exists and has a goal, a "done when" list, a "never" list, decisions with reasons and one next step.
- Every session starts by reading the notes file and the recent history (for code, the git log).
- The agent works on one task at a time and updates the notes after each one.
- Task status lives in JSON, and the agent may only change the status field.
- Old tool results are cleared once used. A reference stays in their place.
- Subagents get read-heavy work only, with an objective, output format, tools and limits.
- Subagents return short results or a file path, not raw dumps.
- Only the lead agent makes decisions, and it writes each one down.
- Compaction is on as a safety net, and the agent re-reads the notes after it runs.
- Key facts sit at the start or end of the context, not in the middle.
- You measured it: run the same long task with and without each layer, and keep what moves the score.
Limits of this study
The strongest numbers come from vendors testing their own systems on internal evals. Chroma and Liu et al. test retrieval and question answering, not full agent runs. No public study we found compares compaction, notes and subagents head to head on the same long agent task. Treat the recipe as a strong default, then test it on your own workload.
Sources
All sources were read and checked on 16 Nov 2026.
- Anthropic, Effective context engineering for AI agents, 29 Sep 2025.
- Anthropic, How we built our multi-agent research system, 13 Jun 2025.
- Anthropic, Effective harnesses for long-running agents, 26 Nov 2025.
- Anthropic, Managing context on the Claude Developer Platform (context editing and the memory tool).
- Hong, Troynikov and Huber, Chroma, Context Rot: How Increasing Input Tokens Impacts LLM Performance, 14 Jul 2025.
- Liu et al., Lost in the Middle: How Language Models Use Long Contexts, arXiv 2307.03172.
- OpenAI, Compaction guide, API docs.
- LangChain, Short-term memory, docs.
- Walden Yan, Cognition, Don't Build Multi-Agents, 12 Jun 2025.