The full answer
Treat the context window as a budget, not a bin. Load data just in time through tools instead of up front, clear old tool results once they have been used, compact the history into a summary when it grows, keep durable notes outside the context, and hand focused subtasks to subagents with fresh contexts. The goal, in Anthropic's words, is the smallest set of high-signal tokens that gets the job done.
Why it matters
Anthropic's context engineering post describes an "attention budget": every token added draws on the model's ability to attend to the rest, so quality degrades as context fills, well before the hard limit. A long-running agent that keeps every file it read and every search result will get slower, dearer and worse.
The techniques
- Just-in-time loading. Keep light references (file paths, query names, URLs) and fetch content with a tool when it is needed, rather than putting everything in the prompt at the start. This is how coding agents explore a codebase.
- Tool result clearing. Once the agent has used a large result (a file's contents, a search page), it rarely needs the raw text again. The Claude API's context editing can clear old tool results automatically once input passes a threshold you set, keeping the last few.
- Compaction. When the conversation nears its limit, replace older turns with a summary of decisions, open questions and current state. The Claude API can do this on the server, so you need no summariser of your own.
- Notes outside the context. The agent writes progress, plans and findings to a file or store (a to-do list, a NOTES.md) and reads it back when needed, including after compaction or in a new session.
- Subagents. A subagent explores a side question in its own clean context and returns only a short summary, so thousands of tokens of search never reach the main thread.
Design choices to state
- What must survive compaction: the task, constraints, decisions and their reasons, and unfinished work. Test that the agent can continue correctly from a summary alone.
- Tool design: results should be concise by default, with pagination and filters, so less needs clearing later.
- Prompt caching still matters: keep the stable prefix (tools, system prompt) unchanged so clearing later content does not break the cache more than necessary.
How to tell it is working
Track input tokens per turn, cost per task and task success over long runs. A healthy agent's context rises and falls; an unhealthy one climbs until it fails.
A good closing line: "I decide what the model needs to see for the next step, not what it has ever seen."
Last updated 16 October 2026
Sources