Context engineering for long-running work
The context window looks like free memory and behaves like a scarce resource. Everything competes for it — instructions, tools, history, retrieved documents, every tool result of every turn — and as it fills, three costs rise together: money (you re-send it all, every request), latency, and attention — a model reasoning over a hundred stale tool results is worse at the current step than one reading a clean brief. Context engineering is the discipline of deciding, at every point in a run, what deserves to be in the window. On long-running agents it matters more than prompt wording.
Know what's actually in the window
Audit any agent and the composition surprises you: the system prompt and tool schemas (constant), the conversation (growing), and — dominating everything on tool-heavy runs — old tool results. The 4,000-line log file the agent read forty turns ago is still being re-sent, re-billed, and re-attended-to on every subsequent request. That observation drives the whole toolkit:
- Context editing clears stale tool results from history automatically once they age past usefulness — the cheapest lever, because it deletes what nothing needed anyway.
- Compaction summarizes the older conversation when the window approaches its limit, letting the run continue past what raw context could hold. The trade is real: a summary is lossy, so critical facts belong in memory, not just in history.
- Memory persists outside the conversation: give the agent a scratchpad — even one markdown file it reads at start and writes at end — and it carries decisions, learnings, and progress across compactions and across sessions. Agents with a memory surface measurably outperform amnesiac ones on multi-session work.
The doctrine: curate, don't accumulate
The mechanisms matter less than the posture. Just-in-time beats just-in-case: don't front-load everything the agent might need — give it search and read tools and let it fetch what the task actually demands, when it demands it. Summaries move forward, details stay behind: when a sub-task completes, what the rest of the run needs is the conclusion, not the transcript. Isolation is the strongest cut of all: hand a self-contained sub-task to a separate agent with a clean context — a subagent — and only its report returns to the parent. The parent stays lean; the subagent reads only its brief. That one idea powers Claude Code's architecture and the entire multi-agent lesson later.
And keep the caching lesson in force: the system prompt and tool list are the most re-read bytes in the whole system, so context discipline and cache discipline are the same discipline — stable prefix, curated middle, volatile tail.
You practice this daily, whatever the surface
This is not an API-only concern. A claude.ai chat that has wandered across five topics answers the sixth one worse — start a fresh chat and re-state what matters. A Claude Code session carries every file it has read; you will learn its /clear and /compact commands as manual context engineering. The instinct transfers everywhere: what does the model need in front of it for this step, and what is just weight?
What to take into the next lesson
Context is a budget; audit what fills it; edit the stale, compact the old, persist the important, isolate the parallel. With single-agent discipline in hand, the next lesson opens the integration layer that feeds every agent its tools — MCP — before the course turns to the tool you will use most: Claude Code.