Lesson 4 · Agent Orchestration
Context Engineering: The Window Is a Budget
Why long agent sessions get worse, and the two moves that fix it.
Your win for this lesson
When an agent session degrades, you can explain why (context rot) and choose a fix: compact the window, or move memory outside it into notes or subagents.
0. Warm-up
1. Idea one: the window is a finite budget
Everything the model sees in one call is its context: system prompt, CLAUDE.md, your messages, every file it read, every test output. Context engineering is curating and maintaining the best set of tokens in that window, not only writing the prompt.[1]
The window has a hard limit, but quality drops well before you reach it. Anthropic calls this context rot: as the number of tokens grows, the model's ability to accurately recall information from it decreases. The model has an "attention budget," and every token you add spends some of it.[1]
"Find the smallest set of high-signal tokens that maximize the likelihood of your desired outcome."Anthropic, Effective context engineering [1]
You have seen this already: an agent that was sharp at the start of a session forgets a decision you made an hour ago, or repeats a mistake it already fixed. The window isn't full yet, but it is noisy.
2. Idea two: compaction (shrink what's inside)
Compaction means summarising the conversation as it nears the limit and starting a fresh window with that summary. Claude Code's compaction keeps architectural decisions and unresolved bugs and drops redundant tool output. The lightest form is simply clearing old tool results.[1]
You use this daily: /compact in Claude Code, and the automatic compaction that kicks in when the window fills. The risk is that a summary can drop details that later turn out to matter.
3. Idea three: move memory outside the window
Instead of keeping everything in the window, keep it somewhere the agent can read back when it needs to. There are two forms:[1]
- Structured note-taking. The agent writes progress, decisions and to-dos to a file (a
NOTES.md, a to-do list) and reloads it later. Notes survive compaction and even a brand-new session. Anthropic's example is Claude playing Pokémon, keeping maps and progress in notes across thousands of steps. - Subagents. A helper explores in its own clean window and may spend tens of thousands of tokens doing it. It hands back only a condensed summary, often 1,000–2,000 tokens. The main window stays clean. This is what Claude Code's Explore subagent is for, and why you labelled it a harness feature doing context work in Lesson 1.
Why this matters for the rest of the course
Push idea three as far as it goes: throw away the window completely after every task, and keep all memory in files and git. That is the Ralph loop, where each iteration starts with a fresh context and the plan and specs live on disk.[2] Anthropic's long-running harness does the same with a progress file and a feature list.[3] You'll meet both next.
4. Recall
From memory: what is context rot, and what are the two kinds of fix (with the two forms of the second)?
Context rot: as the window fills, the model recalls what's in it less accurately. Context is a finite attention budget.
Fix 1, compaction: summarise the window and continue in a fresh one (or clear old tool results).
Fix 2, memory outside the window: (a) structured notes in files the agent re-reads; (b) subagents that work in their own window and return a short summary.
5. Practice: pick the fix
Primary source to read next (≈20 min)
"Effective context engineering for AI agents" (Anthropic, Sep 2025). Read the section on long-horizon tasks; it covers compaction, note-taking and subagents in more depth.
Stuck or curious?
Ask me in the terminal. Good questions: "When should I /compact vs /clear?" or "Is CLAUDE.md context engineering or harness engineering?"
Quick reference: The Five Zoom Levels · Six Orchestration Shapes