agents/prod
04
Foundations · Chapter 4

Context engineering & multi-agent systems

Here's the single most important skill in agent engineering, and it follows directly from Chapter 1: because the model's only memory is its context window, deciding what goes into that window each turn is the whole game. Prompt engineering is a subset of this. The discipline has a name — context engineering — and it's also the reason multi-agent systems exist.

1 · Prompt engineering vs context engineering

Prompt engineering is phrasing the question well. Context engineering is the broader craft of curating the entire set of tokens present at inference — treating that finite window as a scarce budget and spending it on the smallest set of high-signal tokens. Every turn, the window is re-assembled from competing claims on that budget:

Instructionssystem prompt, rules, tool schemas, examples
Knowledgeretrieved docs, facts, memories
Historythe conversation + past tool results
Stateintermediate results, reasoning traces
Interactive · spend your context budget
Drag the sliders to load the window with system prompt, few-shot examples, history, retrieved docs, and memory. Watch it fill: past ~80% you hit "context rot" (recall degrades), and past 100% something must be dropped. This is why the strategies below exist — you're always budgeting.

2 · The four moves for managing context

Writepush data out of the window into an external scratchpad/memory; load it back on demand.
Selectretrieve only what's relevant right now (RAG — even applied to tool descriptions when you have hundreds of tools).
Compresssummarize as the window fills (e.g. Claude Code's auto-compact), preserving decisions and open threads.
Isolatesplit work across sub-agents/sandboxes so each keeps a clean, focused window.

A powerful modern move is just-in-time retrieval: keep lightweight identifiers (file paths, links, queries) in the window and load the actual data only when needed — like a human using an index instead of memorizing everything.

WRITEout to a store SELECTretrieve relevant COMPRESSsummarize ISOLATEsub-agents
The four moves for spending a scarce context budget — write it out, select what's relevant, compress the rest, or isolate work into separate windows.

3 · Why you can't just stuff the window

Dumping everything in backfires, in named ways: context poisoning (a wrong fact gets reused and compounds), distraction (over-reliance on history), confusion (irrelevant tools/docs), and clash (contradictory info). Combine that with "lost in the middle" (Chapter 1) and the guidance is concrete: aim for 60–80% utilization and prefer rolling summarization over raw accumulation.

Poisoning
a wrong fact is preserved and reused, compounding the error every turn
Distraction
the model over-relies on old history instead of the current task
Confusion
irrelevant tools or documents muddy the decision
Clash
two pieces of context flatly contradict each other

4 · When to reach for multiple agents

Multi-agent systems are the isolate move taken to its conclusion: a lead/orchestrator agent decomposes a task and spawns specialized workers that run in parallel — each with its own clean window — returning only a condensed 1,000–2,000-token summary. The detailed exploration stays isolated; only the distilled result comes back.

Lead agentdecomposes + synthesizes Worker · searchown clean window Worker · analyzeown clean window Worker · verifyown clean window each returns only a ~1–2k-token summary → lead merges
Orchestrator-worker: isolation as architecture. Powerful — but it costs.
The evidence — and the price

Anthropic's multi-agent researcher beat a single agent by >90% on research tasks, and token usage explained ~80% of the performance variance — but it burns roughly 15× the tokens of a chat. Use multiple agents for exactly three reasons: protect context, parallelize a wide search, or genuine specialization. Decompose along context boundaries, not task type (splitting by task creates a lossy "telephone game").

Structure beats brains

Reliability comes from how you wire agents, not how smart each is: decentralized independent agents amplified errors 17.2× vs a single agent; centralized orchestration held it to 4.4×; and an external adversarial inspector recovered 96.4% of faults.

Recap

  • The window is the model's whole memory — curating it each turn is the core skill.
  • Four moves: write, select, compress, isolate; target 60–80% fill.
  • Multi-agent = isolate at scale: big wins, ~15× tokens, orchestrate centrally, add a verifier.
Carry into Chapter 5

The most important "select" tool is retrieval. Next we build RAG end-to-end — and you'll run a live vector search that fetches the right context for a question.