The question
The loop works. What breaks after 50 tool calls?The answer
Raw files and logs crowd out the goal and current reasoning.What is in the context?
The model has no memory between calls. Each time it decides the next step, the harness sends it the whole conversation so far. That bundle of text is the context, and it has five parts:
The context window is the most text the model can take in at once, measured in tokens. The first four parts stay small. Tool results grow with every call.
What context rot looks like
Illustrative example. The user asks: “Fix the session-expiry bug. Don’t change theisExpired signature; other services call it.” Each box below is one block of text in the context. Box heights are roughly proportional to their size.
Context at call 1
System prompt
Tool definitions
User: Fix the session-expiry bug. Don’t change the
isExpired signature.Unused space
Context at call 50
System prompt
Tool definitions
User: Fix the session-expiry bug. Don’t change the
isExpired signature.read_file session.ts · 300 linessearch_code “expiresAt” · 40 matchesModel: “I’ll run the tests.”
run_command npm test · 6,000 lines of test logread_file session.ts · the same 300 lines, againapply_patch session.ts · edit appliedread_file invoice.ts · a dead endModel: “I’ll add a third parameter to
isExpired.” ✗Unused space
System promptTool definitionsUser messageModel messagesTool results
- Forgotten constraints. The agent proposes changing the exact signature the user said not to change.
- Repeated work. It rereads
session.tsbecause it lost track of having read it already. - Stale facts. The context holds the first read of
session.ts, from before the agent’s own edit, and the agent can reason from that old version. - Distraction. An unrelated failing test in the 6,000-line log pulls the agent away from the actual bug.
- Rising cost. The model has no memory, so every call resends the whole context. Here, call 1 sends about 8,000 tokens and call 50 about 125,000; across all 50 calls that adds up to more than 3 million input tokens. The 6,000-line log, added once at call 18, is paid for again on every call after it. The agent pays more per call and gets less reliable answers in return. Prompt caching can cut the price of the repeated part, but not the loss in quality.
How to fix it
Before each call, the harness rebuilds the context instead of letting it pile up. Every block gets one of three treatments. Illustrative example, using the same session at call 50:Call 50, before cleanup
System promptRetain
Tool definitionsRetain
User: Fix the session-expiry bug. Don’t change the
isExpired signature.Retainread_file session.ts · 300 lines, before the editDropsearch_code “expiresAt” · 40 matchesSummarizeModel: “I’ll run the tests.”Drop
run_command npm test · 6,000 lines of test logSummarizeread_file session.ts · the same 300 lines, againDropapply_patch session.ts · edit appliedRetainread_file invoice.ts · a dead endDropUnused space
Call 50, after cleanup
System prompt
Tool definitions
User: Fix the session-expiry bug. Don’t change the
isExpired signature.Summary:
isExpired is defined in session.ts and called by 3 services.Summary: 1 test fails: sessions expire 1 ms late at the boundary. Full log saved to disk.
Current diff: session.ts,
> changed to >=Unused space
System promptTool definitionsUser messageModel messagesTool resultsSummaries
- Retain what the agent needs on every call: the goal, the user’s constraints, the current diff, and the most recent errors. Keep them word for word.
- Summarize large results that still matter. The 6,000-line test log becomes one line: which test fails and why. The full log is saved outside the context, so the agent can reread it if needed.
- Drop what no longer helps: duplicate reads, file versions from before the agent’s own edit, dead ends, and the model’s old step-by-step messages.
Where does the removed content go?
Summarizing and dropping are only safe if nothing is lost for good. The full test log, the conversation history, and the task list still have to live somewhere outside the context, so the harness can bring back exactly what the next call needs. This is the second half of context management. This page decides what goes into the context on each call. Seven state stores covers where everything else lives, and how the context is rebuilt from it every turn.Source
anthropic.com/engineering — effective-context-engineering — Anthropic calls this “context rot”: as tokens accumulate, models measurably lose the ability to use the full window well. Left number-free on purpose — Anthropic’s writeup doesn’t quantify a single figure; treat it as a documented phenomenon, not a stat.Continue the walkthrough
Seven state stores
Continue to topic 4 of 14: where everything outside the context lives.