Certopolis

Claude Certified Architect – Foundations

Context editing vs compaction in the Claude API

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_reason compaction 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.

Written from Anthropic's documentation

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. Your team enables compaction on a long-running assistant, sending the beta header compact-2026-01-12 and context_management: {"edits": [{"type": "compact_20260112"}]} on every request. Your loop extracts the text from each response and appends it to messages as an assistant turn. The conversation keeps growing until it exceeds the context window, as though compaction never ran. What change would most effectively resolve this issue?

    Right. In the game that’s Renown for your Context Management and Reliability building.

    Not that one: B is right. In the game your Context Management and Reliability building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Extracting only the text silently discards the compaction state, so every request arrives with the full uncompacted history.

    Written from platform.claude.com/docs/en/build-with-claude/compaction

  2. Your agent enables clear_tool_uses_20250919 on a request whose large stable prefix is cached. Clearing now fires often but removes only a few thousand tokens each time, and the resulting cache rewrites cost more than the clearing saves. What change would most effectively address this issue?

    Right. In the game that’s Renown for your Context Management and Reliability building.

    Not that one: D is right. In the game your Context Management and Reliability building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Because clearing invalidates the cached prefix from the clearing point onward, this is the parameter that stops a small clear from triggering an expensive cache rewrite.

    Written from platform.claude.com/docs/en/build-with-claude/context-editing

  3. Your agent accumulates large tool results that are never referenced again once acted upon, and your team has confirmed that nothing in them needs to be recalled later. Input tokens per turn are rising, and the team proposes handling this with compaction rather than with tool-result clearing, on the grounds that the two strategies cost about the same. How should you evaluate this proposal?

    Right. In the game that’s Renown for your Context Management and Reliability building.

    Not that one: A is right. In the game your Context Management and Reliability building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Tool-result clearing deletes content server-side with no generation at all. When nothing in the results needs to be recalled, the summary buys nothing and clearing is the cheaper strategy.

    Written from platform.claude.com/docs/en/build-with-claude/compaction