"""Server-side compaction configuration with the Python SDK (beta compact-2026-01-12): context_management.edits [{type: compact_20260112, trigger: {input_tokens: 50000}}] on claude-sonnet-5. With a tiny prompt no compaction is triggered, but the response shows the beta shape: usage.iterations[] and context_management.applied_edits []. Also demonstrates the Haiku rejection (compact_20260112 is 4.6+ only). STATUS: LIVE_VERIFIED 2026-09-18: Sonnet 5 200 with iterations [{type: message}]; Haiku 400 "'claude-haiku-4-5-20251001' does not support the 'compact_20260112' context management strategy." (~$0.0002). Run: .venv/bin/python examples/anthropic/context-management/compaction_config.py """ from __future__ import annotations import os import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from scripts import live # noqa: E402 import anthropic # noqa: E402 client = anthropic.Anthropic(max_retries=1) MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-sonnet-5") EDITS = [{"type": "compact_20260112", "trigger": {"type": "input_tokens", "value": 50000}, "instructions": "Summarize the conversation for continuation. Do not call any tools."}] msg = client.beta.messages.create(model=MODEL, max_tokens=8, betas=["compact-2026-01-12"], context_management={"edits": EDITS}, messages=[{"role": "user", "content": "Reply with OK."}]) print(MODEL, "->", msg.content[0].text, "| applied_edits:", msg.context_management.applied_edits if msg.context_management else None, "| iterations:", [(i.type, i.input_tokens, i.output_tokens) for i in (msg.usage.iterations or [])]) live.log_request("anthropic", "POST", "/v1/messages", 200, 0.0002, f"example context-management/compaction_config.py {MODEL} beta=compact-2026-01-12") try: client.beta.messages.create(model="claude-haiku-4-5-20251001", max_tokens=8, betas=["compact-2026-01-12"], context_management={"edits": EDITS}, messages=[{"role": "user", "content": "Reply with OK."}]) except anthropic.BadRequestError as e: print("haiku ->", e.status_code, e.body["error"]["message"]) live.log_request("anthropic", "POST", "/v1/messages", e.status_code, 0, "example context-management/compaction_config.py haiku unsupported")