Certified Agent Harness Engineer
Course sections

Module 4 — Context & Memory Management

Module 4 — Context & Memory Management

The Module 1 spine works until it doesn't. It appends every user message, every assistant message, and every tool result to one growing array, resends that array on every step, and eventually receives a 400 from the provider saying the request exceeded the model's context length. There is no graceful degradation: the harness worked perfectly on turn 40 and is bricked on turn 41. Worse, the failure lands at the least convenient moment — deep into a task, after the agent has accumulated exactly the state that made it useful. This module replaces the naive append with a budget, a pruner, a compactor, and a spill path, and it does so in a specific order, because the cheap reclamations must be exhausted before the expensive ones. The anchor question is: what do you do when the conversation outgrows the window? The answer is not "summarise it" — that is the last resort before failure, not the first response.


Learning objectives

  • M4.1 Budget a context window across system prompt, history, tool results, and headroom.
  • M4.2 Assemble a system prompt from composable sources without unbounded growth.
  • M4.3 Detect context pressure and choose a response: prune, compact, spill, or fail.
  • M4.4 Implement compaction — summarisation boundaries, what must never be dropped, and how to verify fidelity.
  • M4.5 Prune tool results selectively (the highest-yield, lowest-risk reclamation).
  • M4.6 Spill large artefacts to disk and reference them by handle.
  • M4.7 Distinguish memory tiers: in-turn context, session persistence, and cross-session memory files.
  • M4.8 Exploit prompt caching — prefix stability, cache TTLs, and the ordering rules that preserve hits.

Prerequisites and estimated time

Prerequisites: Modules 1–3. You need the Module 1 spine (the ~150-line read/write/shell loop), the loop anatomy from Module 2 — especially the event-log-versus-projection distinction in M2.6 and token metering in M2.7 — and Module 3's result-shaping work (M3.5), which this module generalises from one tool to the whole conversation.

Estimated time: 5 hours — roughly 3 hours reading and worked examples, 2 hours on the lab.


Sections

  1. M4.1Budget a context window~9 min4 questions
  2. M4.2Assemble a system prompt without unbounded growth~6 min3 questions
  3. M4.3Detect pressure and choose a response: the escalation ladder~6 min4 questions
  4. M4.4Implement compaction~9 min5 questions
  5. M4.5Prune tool results~5 min2 questions
  6. M4.6Spill large artefacts~4 min2 questions
  7. M4.7Memory tiers~5 min2 questions
  8. M4.8Exploit prompt caching~18 min4 questions