Managing Long Context
Compaction, summaries, scratchpads, and just-in-time retrieval: techniques that keep long-running agents focused and accurate.
The first hour of a long agent session is usually great. The agent reads the right files, makes a plan, and executes cleanly. Three hours in, something shifts. It re-reads files it already read. It forgets a decision you made together at the start. It reintroduces a bug it fixed an hour ago. Nothing about the model changed. Its context just filled up with the residue of everything it did.
Long-running work is where agents deliver the most value, and also where context discipline matters most. This lesson covers the core techniques experienced builders use to keep agents sharp over hours of work: fresh starts, compaction, scratchpad files, just-in-time retrieval, lean tool output, and isolating heavy work in subagents. You'll learn to use them both as an agent user (in tools like Claude Code) and as an agent builder.
Why long sessions degrade
Every action an agent takes adds to its context: the tool call, the tool's output, the model's reasoning about it. A coding agent that reads 30 files, runs the test suite eight times, and tries three approaches has accumulated a huge amount of text, most of which is no longer relevant.
As you learned in From Prompt Engineering to Context Engineering, the model's attention is a limited budget. Long context degrades it in predictable ways:
- Old tool output crowds out current signal. The full output of a test run from two hours ago is still there, competing with the one that matters now.
- Superseded plans linger. The agent sees plan A, the reasons it failed, and plan B, and sometimes drifts back toward A.
- Early decisions get buried. "We agreed not to touch the auth module" was said 150 turns ago, under a mountain of file contents.
- Every step gets slower and more expensive, because the whole context is reprocessed on each model call.
The techniques below all attack the same problem from different angles: keep the high-signal information close at hand and get the rest out of the way.