Context editing deletes old tool results or thinking blocks by rule, on the server, and generates nothing. Compaction has Claude write a summary that replaces older turns. Use clearing when old tool output is no longer needed, and compaction when the history must be remembered in shorter form.
Context editing
Send the context-management-2025-06-27 beta header and add a strategy to context_management.edits. clear_tool_uses_20250919 clears tool results once input passes trigger, which defaults to 100,000 input tokens. keep sets how many recent tool use and result pairs survive, default 3. exclude_tools protects named tools, clear_tool_inputs: true also clears the call parameters, and clear_at_least sets a minimum number of tokens to clear. Cleared results are replaced with placeholder text.
Clearing runs before the prompt reaches Claude. Your client keeps the full history. Clearing invalidates the cached prefix from the point where content was cleared, so set clear_at_least high enough to be worth the cache write. If you also use clear_thinking_20251015, it must be listed first in edits.
Compaction
Threshold compaction uses the compact-2026-01-12 beta header and the compact_20260112 edit. Its trigger defaults to 150,000 input tokens and must be at least 50,000. When it fires, the response starts with a compaction block. Append the whole response.content to messages. The API ignores every block before the compaction block on the next request. Set pause_after_compaction: true to stop with stop_reasoncompaction before Claude answers.
On-demand compaction uses the compact-2026-09-04 header and a top-level "compaction": {"type": "summarize"}. You decide when to compact, and the returned block goes first in messages in place of what it summarizes.
Cost
Compaction is a billed sampling step. It appears as a compaction entry in usage.iterations, and the top-level input_tokens and output_tokens leave it out, so sum the iterations for the true total. Sending the block back on later requests adds no compaction cost. Clearing generates no tokens at all.
Where the exam catches people
Believing keep sets when clearing starts: trigger does that, and keep sets how many recent tool pairs survive.
Believing appending only the response text is enough: drop the compaction block and the next request carries the full history again.
Believing compaction is free context management: the summary is a billed generation reported in usage.iterations.
Believing clearing leaves the cache intact: it invalidates the cached prefix from the cleared point onward.