Skip to main content

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 the isExpired 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 lines
search_code “expiresAt” · 40 matches
Model: “I’ll run the tests.”
run_command npm test · 6,000 lines of test log
read_file session.ts · the same 300 lines, again
apply_patch session.ts · edit applied
read_file invoice.ts · a dead end
Model: “I’ll add a third parameter to isExpired.” ✗
Unused space
System promptTool definitionsUser messageModel messagesTool results
At call 1, the user’s constraint (outlined in green) is one of only three things the model reads. By call 50, it is still there, but it sits above thousands of lines of files and logs. The model’s latest message (outlined in red) breaks the constraint it was given at the start. The symptoms:
  • Forgotten constraints. The agent proposes changing the exact signature the user said not to change.
  • Repeated work. It rereads session.ts because 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.
Nothing was deleted. The information is all still there; the model just uses it less reliably as the context grows. That is context rot.

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.Retain
read_file session.ts · 300 lines, before the editDrop
search_code “expiresAt” · 40 matchesSummarize
Model: “I’ll run the tests.”Drop
run_command npm test · 6,000 lines of test logSummarize
read_file session.ts · the same 300 lines, againDrop
apply_patch session.ts · edit appliedRetain
read_file invoice.ts · a dead endDrop
Unused 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.
The constraint is now one of six blocks instead of ten, and most of the window is free again. Each call costs fewer tokens, and the model reads what matters.

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.
Design decision: Rebuild the context before each call: retain, summarize, drop. Keep everything else outside the context, separated by lifetime and purpose.

Continue the walkthrough

Seven state stores

Continue to topic 4 of 14: where everything outside the context lives.