Lesson 15.03 · Phase 15 · Capstone: Your Own Agent Harness

Memory & context manager

What the model sees on each call is a decision. Make it in one place, with a budget, and never let it cut a tool result off from its call.

Learning objectives

  • Build a context manager as a beforemodel hook that keeps every call under a token budget.
  • Shrink history in two passes: clip old tool output first, then compact old turns into a summary.
  • Keep the history valid: never separate a tool result from the call that requested it.
  • Add long-term memory as remember/recall tools, with provenance and a guard against stored instructions, plus project instructions from HARNESS.md.

Each step re-sends the whole conversation. An agent paging through a 600-line log sends about 1,500 tokens at step 2 and over 6,000 by step 6. A long coding task blows through a 32k window in a few dozen steps. Then the provider rejects the call, or the model quietly loses the start of the task.

Loading the full lesson…