
Anthropic Messages API Adds On-Demand Conversation Compaction for Autonomous Agents
Anthropic Messages API Adds On-Demand Conversation Compaction for Autonomous Agents
Autonomous coding harnesses, multi-agent frameworks, and conversational assistants frequently encounter the physical boundaries of large language model context windows. When an agent runs through dozens of iterative tool executions, inspects file trees, runs build commands, and parses terminal outputs, the conversation history rapidly consumes tens of thousands of tokens.
Until now, managing this token growth in the Claude ecosystem relied either on client-side manual slicing, local proxies like RTK, or Anthropic’s automated threshold compaction. Automated threshold compaction silently summarized message history whenever the total input hit a fixed ceiling, such as 150k tokens. While effective for simple chat interfaces, automated eviction introduced severe side effects for agentic harnesses. A compaction trigger could fire synchronously in the middle of a delicate tool chain, breaking tool-result associations and invalidating recent thinking traces introduced with Claude Fable 5.1.
On September 14, 2026, Anthropic released on-demand conversation compaction in beta for the Messages API (/v1/messages). Enabled via the compact-2026-09-04 beta header, this capability gives developers explicit, programmatic control over when and how conversation history is compressed into cryptographically signed summary blocks.
What Changed
The new on-demand compaction interface decouples context compression from arbitrary token count thresholds. Instead of reacting to an unexpected context overflow mid-turn, developers and agent harnesses can now schedule compaction at natural semantic boundaries: after completing a milestone task, following a git commit, or before launching a broad subagent inquiry.
The mechanism operates through two primary components:
- The
compact-2026-09-04Beta Header: Activates the on-demand compaction parser and serialization engine on Anthropic API endpoints. - Top-Level
compactionControl Parameter: Configured as{"type": "summarize"}, instructing the API to distill the submitted message history into a signed compaction block without producing conversational text output.
Rather than returning plain unstructured prose, the API terminates the turn with stop_reason: "compaction" and returns a structured, cryptographically signed compaction block containing an internal state token and message digest.
Technical Architecture and Lifecycle
The on-demand compaction lifecycle establishes a deterministic three-stage pipeline that protects recent reasoning while discarding ephemeral operational noise:
The client tracks message history through multiple tool calls, terminal outputs, and intermediate thinking blocks as total token volume expands toward capacity.
The harness issues a dedicated compaction request at a clean task boundary, distilling older turns while preserving recent verbatim messages and emitting a signed summary block.
Subsequent API requests prepend the signed compaction block ahead of recent turns. The signed block remains eligible for prompt caching, cutting latency and billing costs.
Implementation Guide and API Syntax
To compact an existing conversation, send a request to /v1/messages including the beta header and the compaction parameter block:
{
"model": "claude-3-7-sonnet-20250219",
"max_tokens": 4096,
"compaction": {
"type": "summarize"
},
"messages": [
{ "role": "user", "content": "Analyze repository architecture and run unit tests." },
{ "role": "assistant", "content": "Running test suite across all packages." },
{ "role": "user", "content": "Exit code 0, 42 tests passed." }
]
}
The API processes the uncompacted message history, produces an encrypted distillation, and immediately completes the turn with stop_reason: "compaction" without emitting conversational text:
{
"id": "msg_01948bd78a224",
"type": "message",
"role": "assistant",
"content": [
{
"type": "compaction",
"summary": "User requested repo architecture analysis and test suite execution. Initial tests exited with code 0 across 42 tests. Architectural focus set on data ingest.",
"signature": "sig_comp_20260914_9f81a7b4c90e2",
"compacted_range": {
"start_index": 0,
"end_index": 2
}
}
],
"stop_reason": "compaction",
"stop_sequence": null,
"usage": {
"input_tokens": 142800,
"output_tokens": 640,
"compaction_tokens": 2850
}
}
When dispatching subsequent conversation turns, the client includes the signed block directly inside the messages array:
{
"model": "claude-3-7-sonnet-20250219",
"max_tokens": 4096,
"messages": [
{
"role": "user",
"content": [
{
"type": "compaction",
"summary": "User requested repo architecture analysis...",
"signature": "sig_comp_20260914_9f81a7b4c90e2"
}
]
},
{ "role": "assistant", "content": "Understood. The test baseline is verified." },
{ "role": "user", "content": "Proceed with refactoring the database connector." }
]
}
Architectural Comparisons
Understanding the trade-offs between automated threshold compaction and on-demand compaction is essential for production agent engineering:
Context Compression Strategies
| Metric or Behavior | Automated Threshold Compaction | On-Demand Compaction |
|---|---|---|
| Trigger Mechanism | Fixed input token limit (e.g., 150k) | Explicit client API call |
| Timing Predictability | Unpredictable; fires during any turn | Deterministic; controlled by harness |
| Tool Loop Safety | Risk of severing tool_use and tool_result | Safe; scheduled between discrete operations |
| Thinking Block Handling | Can truncate active reasoning chains | Preserves recent verbatim thinking blocks |
| Verification Artifact | Internal server state | Cryptographically signed payload |
| Client Storage | Stateless server session | Client stores signed compaction block |
| Prompt Cache Reusability | Reset upon dynamic eviction | High cache hit rate on static summary block |
Key Invariants and Critical Failure Modes
Developers integrating on-demand compaction must account for five structural constraints:
- Payload Immutability: The returned
signatureverifies the cryptographic integrity of thesummarystring and its internal metadata. Any client-side string editing, formatting modification, or parameter truncation immediately invalidates the signature, causing the API to reject subsequent turns with an HTTP 400 validation error. - Preceding History Exclusion: Once a signed compaction block is used, summarized messages must be removed from the request history. Leaving the summarized turns in front of the signed block results in an HTTP 400 rejection.
- Unfulfilled Tool Call Gate: The compaction request will fail with an error if the final assistant turn in the history contains an unfulfilled tool call (
tool_usewithout a matchingtool_result). Compaction must only be requested when tool interactions are fully resolved. - Feature Exclusivity: The
compactionparameter cannot be combined with the legacy automatedcontext_managementparameter within the same request. - Dual Billing Accounting: Compaction requests incur input token charges for the full uncompacted prefix analyzed, plus output token charges for the synthesized summary. Billing monitors must aggregate these summarization costs into their operational metrics rather than treating compaction calls as zero-cost maintenance.
Conclusion
Anthropic’s on-demand conversation compaction addresses a fundamental operational friction point in autonomous AI software development. By providing deterministic, signature-backed context distillation through the compact-2026-09-04 beta header, developers can construct persistent, multi-day agent workflows without fearing context overflows or tool loop corruption.