Context and State
Every step of the loop appends to the conversation. Tool results, model replies, and retrieved documents all compete for the same finite window. Anthropic's context engineering guidance frames this as the central engineering problem for agents: find the smallest set of high-signal tokens that makes the desired behavior likely.
Why more context can hurt
Anthropic describes context rot: as the number of tokens grows, the model's ability to recall specific information from that context decreases. Context is therefore a finite resource with diminishing returns. Every token you add spends some of the model's attention budget, so each addition should earn its place.
Upfront or just in time
You can load information before the run, or give the agent references and let it load what it needs. Anthropic calls the second approach just-in-time retrieval. An agent that holds a file path or a stored query, and loads the file only when it needs it, usually spends less context than one that loads everything first. The trade-off is that exploration takes more steps and needs clear guidance so the agent does not wander.
Four techniques for long tasks
| Technique | What it does | Best when |
|---|---|---|
| Tool result clearing | Removes old raw tool outputs the agent no longer needs | Tool calls return large payloads |
| Compaction | Summarizes a long history and starts a fresh window from the summary | Long back-and-forth with many decisions |
| Structured note-taking | The agent writes progress notes outside the window and rereads them | Iterative work with clear milestones |
| Sub-agents | A focused agent explores in its own window and returns a short summary | Deep research that would flood the main context |
Practice
- Add a counter to your loop from lesson 0001 that prints the total characters in
messagesafter each step. Run a multi-step task and watch the growth. - Identify the largest tool result in that run. Decide whether the agent needs the full payload later or only a summary.
- Implement one technique from the table. Clearing old results is the simplest. Measure the character count again and confirm the task still succeeds.
Retrieval check
Why can adding more tokens to an agent's context sometimes make it worse?
Correct. Anthropic calls this context rot. The attention budget is finite, so each new token competes with the ones that matter.
Not quite. The cause is the finite attention budget. Recall degrades as the window fills, which is why every token should be high-signal.
Which technique keeps a long task's progress outside the context window?
Correct. The agent writes notes to persistent storage and rereads them when needed, so progress survives beyond the window.
Not quite. Only note-taking stores state outside the window. The other options either add tokens or increase the output limit.