Add `AnthropicModel.compact_messages()` for on-demand native compaction
- Part of #11395
Why
Step 4 of the compaction plan in #11395 needs AnthropicModel.compact_messages(), so a trigger can ask Anthropic for its native summary when we choose, as OpenAIResponsesModel.compact_messages() does with /responses/compact. It sends Anthropic's beta compaction={'type': 'summarize'} parameter on POST /v1/messages. Each behavior below was checked against the live API (see the findings).
New public surface
AnthropicModel.compact_messages(), overridingModel.compact_messages()AnthropicModelProfile.anthropic_supports_on_demand_compaction: set byAnthropicProvider.model_profile()(it's a Messages API fact) for the models whosecapabilities.compaction.summarize.supportedis true onGET /v1/models, plus the Mythos models from the docs
User-visible behavior
compacted = await model.compact_messages(ctx, instructions='Keep the database decisions.')
#> ModelResponse(parts=[CompactionPart(content='<summary>', provider_name='anthropic', provider_details={'signature': '...'})],
#> usage=RequestUsage(input_tokens=790, output_tokens=859, ...), finish_reason='stop')
result = await agent.run('Which database?', message_history=[*messages, compacted]) # sends the block back
- Beta header:
compact-2026-09-04goes on the compaction request and on every request whose history carries a signed block._add_compaction_paramsno longer adds the defaultcompact_20260112edit to such a request, since Anthropic rejects the two together. - Settled request:
stop_sequences,output_config.format,task_budget.remainingandcontext_managementare left out, a forcingtool_choicebecomesauto, and the tool calls of a trailing response without results are dropped. Each of these returned a 400 live. The rest of the request stays the same, so it still reads the prompt cache. instructions: Anthropic replaces its whole summarization prompt with it, so a focus is wrapped in a full prompt. The method documents how this differs from OpenAI.- No summary: a 200 without a block (e.g.
stop_reason: max_tokens) raisesUnexpectedModelBehaviorinstead of returning an empty part. - Usage: the top-level usage is all zeros. The existing
map_usagealready adds theusage.iterationscompaction totals back in, so the response reports the real numbers. Its context size is stamped as the summary alone, soctx.context_window_useddoesn't read the old conversation back. - Signature: it's now kept in
CompactionPart.provider_detailsand sent back, both non-streamed and streamed (the block arrives whole incontent_block_start). - Unsupported model: raises
UserErrorbefore sending, based on the profile flag.
Verification
tests/models/anthropic/test_compact_messages.py records on claude-haiku-5-5: basic (streamed behind the scenes) with usage, focus (non-streamed), replay on claude-haiku-5-5 and claude-sonnet-4-6 (header present, billed 640/506 input tokens vs 790 for the conversation), settled parameters, the no-summary error, the unsupported-model error, and compaction plus replay on both Bedrock clients (test_compact_messages_on_aws). Docs: docs/models/anthropic.md ("Compacting on demand") and docs/capabilities/compaction.md.
What changes for existing users
Nothing. Histories without a signed block are sent exactly as before.
Platforms: verified live on the Claude API and on both Bedrock clients (AsyncAnthropicBedrock InvokeModel, AsyncAnthropicBedrockMantle Messages API), which Anthropic's docs don't list; Claude Platform on AWS (AsyncAnthropicAWS), Vertex AI and Foundry are documented but weren't reachable with our credentials. No client is narrowed. Open question: the block's tool_changes field isn't round-tripped, because no response returned it.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
Opened by Claude (Douwe's coding agent).