Back to course overview
Module 4Memory & state 11 min

Context management

Curating what the agent sees each turn: the layered context budget, relevance over volume, and treating the window as a managed resource.

Short-term and long-term memory converge on one discipline: deciding what occupies the context window on any given turn. The window is the agent's entire awareness — and a scarce, position-sensitive resource. Managing it deliberately is what separates agents that stay sharp over long tasks from ones that degrade into confusion.

The layered context budget

Think of each turn's context as layers competing for space, ranked by priority:

what goes in the window, in priority ordertext
1. System prompt + tool definitions   (fixed — the agent's identity/abilities)
2. The goal + current plan            (pinned — never compact these away)
3. Persistent scratchpad / notes      (compact working state)
4. Recent turns, verbatim             (the live working memory)
5. Summary of older turns             (compacted history)
6. Retrieved long-term memory         (only when relevant, capped)
───────────────────────────────────────────────────────────────
   everything else (raw old tool results, stale retrievals) → evicted

The principles under the budget

  • Relevance beats volume — again. The lesson that ran through RAG runs through agents: five relevant items outperform fifty mixed. More context is not more capability; past the relevant set, it's dilution you pay for.
  • Put the mutable at the edges. The goal at the top, the most recent results at the bottom — the positions the model attends to best. Bury the current sub-task in the middle and watch it get dropped.
  • Every token is a choice. A verbose tool result that stays verbatim for ten turns is a choice to spend ~4KB × 10 turns of attention on something used once. Shape, summarize, evict — deliberately.
  • Measure context pressure. Log per-turn token count; when trajectories routinely run hot, the fix might be upstream (leaner tool results, tighter reasoning) rather than more aggressive compaction.

This is the through-line of the whole course made concrete: an agent is a loop over a carefully curated context window. Tools decide what can enter; memory decides what persists; context management decides what's present now. Get this discipline right and most 'the agent got confused' bugs never happen.

The mental model

Treat the context window like a desk, not a warehouse. A good desk holds the current task, the reference you need right now, and a notepad of key findings — everything else is in drawers (your database, your memory store) retrievable on demand. Agents that treat the window as a warehouse — pile everything on, hope the model finds it — are the ones that lose the thread.