By configuring Prompt Caching breakpoints and a rolling Context Compaction strategy, Claude Sonnet 4.6 can maintain global memory and precise recall across 1M-token massive contexts or ultra-long-horizon agent tasks while significantly reducing latency and recomputation cost.
Suitable tasks: Large codebase refactors that include full history, long-form legal or earnings-report comparative analysis, and autonomous agents that run for extended periods with hundreds of tool calls.
Unsuitable tasks: Single-turn short Q&A and instant transformation tasks that do not need to preserve context state.
Applicable model version: Claude Sonnet 4.6 (claude-sonnet-4-6).
Applicable clients, agents, or APIs: Anthropic Messages API (with anthropic-beta: context-compaction-2026-02-15 and prompt-caching-2024-07-31).
Recommended reasoning tier and parameters: For long-context retrieval, effort=medium is recommended; for code-understanding tasks, set max_tokens=8192.
{
"model": "claude-sonnet-4-6",
"max_tokens": 8192,
"output_config": {
"effort": "medium"
},
"system": [
{
"type": "text",
"text": "You are a senior engineer responsible for deep code analysis and system architecture audits. Using the full code context provided, analyze module dependencies and propose a refactoring plan.",
"cache_control": { "type": "ephemeral" }
}
],
"context_management": {
"strategy": "compaction",
"compaction_trigger_tokens": 180000,
"compaction_target_tokens": 60000,
"preserve_recent_messages": 8,
"summary_prompt": "Preserve key technical decisions, the list of file changes, failed test cases, and outstanding to-dos; discard raw output from intermediate tool calls."
},
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "<codebase_context>\n... [insert codebase files injected via directory parsing here] ...\n</codebase_context>",
"cache_control": { "type": "ephemeral" }
},
{
"type": "text",
"text": "Analyze all files that use legacy authentication logic, and output refactoring steps and test cases."
}
]
}
]
}Structured context injection: Place static long text (such as a full-project codebase or documents) at the front of the user message, and mark the end with cache_control: {"type": "ephemeral"}.
Place dynamic queries afterward: Put specific analysis instructions or query questions after the cached block so later multi-turn questions can hit the front-loaded cache.
Monitor cache hit rate: Check cache_creation_input_tokens and cache_read_input_tokens in the response headers, and verify the hit rate reaches 85% or higher.
Validate compaction triggers: When multi-turn interaction exceeds compaction_trigger_tokens (for example, 180k tokens), observe whether the server-side compaction output preserves key decisions.
Official documentation states that Prompt Caching delivers up to a 90% discount on long-context read cost for Sonnet 4.6 and reduces time to first token (TTFT) to 20%-30% of the original.
Official long-context benchmarks (Needle In A Haystack / RULER): Sonnet 4.6 reaches 98.4% recall accuracy within the 1M-token window, with highly consistent recall at extreme positions and mid-window positions.
Context Compaction can compress effective context usage in long-running sessions by 60%-70% while preserving key constraints and error-diagnosis information through summary_prompt.
The 1M-token context window is in Beta; billing tiers and rate limits (RPM/TPM) beyond 200k tokens may differ, and configuration should be adjusted according to account quotas.
Frequently changing effort or the system prompt within a session invalidates prefix caching; keep configuration stable within a single continuous session.
Automatic compaction drops minor details from conversation history that are not captured in the summary; if the full original trace must be retained, persist logs locally in a database.
Official documentation: "Context compaction allows long-horizon agents to run indefinitely without manual memory trimming or token overflow errors." (quote from official documentation)
Claude Sonnet 4.6