Prompt caching, context editing, compaction and memory: which fixes which context problem
Four Claude context tools compared: what prompt caching, context editing, compaction and the memory tool each change, what they cost, and how they interact.
Four tools, four different problems
Long agent sessions run into two separate problems: cost and latency from sending the same large input again and again, and a context that fills with material the model no longer needs. Claude has a tool for each, and they are easy to mix up.
| Tool | What it changes | Use it when | Watch out for |
|---|---|---|---|
| Prompt caching | Reuses the processing of a stable prompt prefix | The same long prefix repeats across requests | Any change before a breakpoint misses the cache; entries last 5 minutes by default |
| Context editing | Clears old tool results or thinking blocks from requests | Bulky, reproducible tool output piles up | Clearing invalidates the cached prefix |
| Compaction | Replaces older turns with a summary Claude writes | A long conversation must keep going | A summary can lose details a later step needs |
| Memory tool | Files that Claude reads and writes through your storage | Knowledge must survive across sessions | It runs client-side: you store and secure the data |
Prompt caching
Caching stores the processed form of a prompt prefix so that later requests with the same prefix skip that work. Mark it with explicit cache_control breakpoints, or add a single top-level cache_control field for automatic caching, which moves the breakpoint forward as the conversation grows. A request has 4 breakpoint slots.
- Order the request from most to least stable: tools and system prompt first, changing content last. A timestamp or user ID near the start makes every prefix unique.
- Entries live for 5 minutes by default and refresh each time they're used. A 1-hour lifetime is available when traffic is less frequent.
- Pricing: 5-minute cache writes cost 1.25 times the base input price, 1-hour writes 2 times, and cache reads 0.1 times.
- When reads drop, cache diagnostics report why, for example model_changed, system_changed, tools_changed or messages_changed.
Context editing
Context editing removes content from the conversation before the prompt reaches Claude. Tool result clearing (clear_tool_uses_20250919) clears older tool results once context grows past a threshold you set, and thinking block clearing (clear_thinking_20251015) controls how much earlier thinking is kept. Use exclude_tools for tools whose results must survive, such as a short confirmation the final answer cites.
Clearing invalidates the cached prefix, so every clearing costs a cache write. Set clear_at_least so each clearing removes enough tokens to be worth that write.
Compaction
Compaction replaces the older turns of a conversation with a summary that Claude writes on the server. Anthropic's context editing page calls it the primary strategy for long-running conversations. With compaction on demand, your application decides when to compact. With compaction at a token threshold, the API compacts inside the request that reaches the threshold.
Two options protect what matters. Keep recent turns when the last few must reach Claude word for word, and write your own summarization prompt when the default summary drops something a later step needs. Values that must stay exact, such as identifiers, belong in your own application state, not in any summary.
The memory tool
The memory tool gives Claude a directory of memory files, under /memories, that persist between sessions. When it's enabled, Claude checks its memory directory before starting a task and stores what it learns as it works. The tool is client-side: Claude only requests file operations, and your application runs them against storage you control.
That makes memory the right place for durable, editable knowledge such as confirmed conventions or project facts. Scope it to whoever the knowledge belongs to, so one team's preferences don't leak into another's sessions, and give people a way to correct or delete entries.
Using them together
- Cache the stable parts: tools, system prompt and any reference material shared by every request.
- Clear bulky tool results with context editing, with clear_at_least set and the tools you can't lose excluded.
- Compact the conversation when it grows long, keeping recent turns verbatim if the current step depends on them.
- Keep what must outlive the session in memory, and keep exact identifiers in your own state.