Long-running agents have one boring, expensive failure mode: the context only grows. Every tool call, every turn, every result gets appended, and you pay for the whole transcript on every request until you hit the window and the run dies. Context compaction is the built-in fix, and as of the January 2026 beta it's the same one-line switch whether you call Claude directly or through Amazon Bedrock. Here's the switch, both envelopes, and the one number that will lie to you about the cost.

The one-line answer: add {"type": "compact_20260112"} to context_management.edits and send the beta header compact-2026-01-12. When your input crosses the trigger (default 150,000 tokens), Claude summarizes the older turns into a single compaction block and automatically drops everything before it on your next request. The edit is identical on the Anthropic API and on Bedrock — only the request envelope and the model ID change.

The switch on the Anthropic API#

resp = client.beta.messages.create(
    betas=["compact-2026-01-12"],
    model="claude-opus-5",
    max_tokens=1024,
    messages=messages,
    context_management={"edits": [{"type": "compact_20260112"}]},
)
# Append the WHOLE response — compaction block included — to continue.
messages.append({"role": "assistant", "content": resp.content})

When Claude receives a compaction block in the history, it ignores every content block before it. You don't prune anything by hand; the summary carries the meaning forward and the token count resets down.

The same switch on Amazon Bedrock#

Same edit type, same beta flag — you just assemble the envelope Bedrock's InvokeModel expects instead of letting the SDK do it:

{
  "anthropic_version": "bedrock-2023-05-31",
  "anthropic_beta": ["compact-2026-01-12"],
  "max_tokens": 1024,
  "messages": [ ... ],
  "context_management": { "edits": [ { "type": "compact_20260112" } ] }
}

Post that body to a Bedrock Claude model ID or a cross-region inference profile (Opus 4.7 and later). The response still comes back with a compaction block you append to continue. That's the entire port: the version pin, the beta as a body field, a Bedrock model reference. The context-management logic doesn't move.

The number that lies to you#

Compaction is not a discount — it's a trade. The summarization runs as an extra turn, billed at your full pre-compaction input size. Pay it once, and every following turn is smaller. The problem is that the response hides it:

"usage": {
  "input_tokens": 23000,          // ← the small, post-compaction turn ONLY
  "output_tokens": 1000,
  "iterations": [
    { "type": "compaction", "input_tokens": 180000, "output_tokens": 3500 },
    { "type": "message",     "input_tokens": 23000,  "output_tokens": 1000 }
  ]
}

The top-level input_tokens reports only the non-compaction turns. The 180,000-token summarization pass is real, and it's billed — it's just tucked into usage.iterations. Sum every entry in iterations or your cost dashboard will quietly under-report the exact requests where compaction fired.

That billing shape is also the whole tuning rule. Because you eat one large turn to shrink the rest, compaction wins on genuinely long sessions and loses on short ones — which is why the default trigger sits at 150,000 tokens and why the floor is 50,000. Set it too low on a chatty-but-short agent and you'll pay for summaries you never needed. Three knobs let you shape it: trigger ({"type": "input_tokens", "value": N}, N ≥ 50000), pause_after_compaction: true (returns stop_reason: "compaction" so you can re-attach the freshest messages before continuing), and instructions (replaces the default summary prompt).

Compaction or context editing?#

They're different tools and they compose. Compaction condenses old turns you still want the gist of. Context editing — edits like clear_tool_uses — deletes content you don't, most often stale tool results, keeping nothing. Reach for compaction when the earlier conversation still matters; reach for context editing when it's disposable; put both in the same edits array when a long agent has some of each. We compared the two head-to-head for long-running agents in Context Editing vs Compaction, and walk through wiring them together with the memory tool in How to Combine Context Editing, Compaction, Memory, and Subagents.

The takeaway for a team of one: turning compaction on is one line you can ship today, and it's portable across the API and Bedrock without a rewrite. Just instrument usage.iterations before you trust the savings — the feature that keeps your agent alive also bills a turn your dashboard won't show you by default.