1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Context Management

Context editing, compaction and memory as Claude API features: what each does to the conversation, and why the compaction block must be appended back verbatim.

Last updated

Production6 min readFirst readPrompt CachingThe Four Types of Agent Memory

After this section you can

  • Tell context editing, compaction and memory apart by what each one does to the conversation
  • Wire up compaction without silently dropping its state by extracting only the response text
  • Reason about what a context rewrite costs you in cache reads, and set thresholds from real runs
35

Context Management as an API Feature

Three mechanisms with three different jobs: clearing, summarising, and persisting. Picking the wrong one is how a long-running agent either forgets what it was doing or pays to re-read what it already knew.

THE CENTRAL IDEA

A long agent run fills its window with material that has already done its job — tool outputs read once, thinking from twenty turns ago. Context editing deletes that material. Compaction replaces it with a summary. Memory writes it somewhere that outlives the session. They are not alternatives; most long-horizon agents use all three, at different timescales.

Same problem, three different things done to the window
BEFORE AFTER CONTEXT EDITING clear_tool_uses tool result tool result cleared cleared Every turn survives; the bulky results inside them do not. Nothing is summarised — what is cleared is simply gone. COMPACTION compact_20260112 compaction room again Earlier turns collapse into a server-written block. Append response.content verbatim — text alone drops it. MEMORY memory tool / store session 1, all of it learned recalled session 2, empty A new session starts blank except what was written to disk — the only one that crosses a session boundary.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium