CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
One Parameter, Infinite Conversations: Server-Side Context Compaction in the Claude API

One Parameter, Infinite Conversations: Server-Side Context Compaction in the Claude API

Chris Harper

3 min read

Jul 6, 2026 · 20:04 UTC

AI
Workflow
Claude Code
Agents
Best Practices

TL;DR: Add one beta parameter to your Messages API call and Claude handles infinite-length agent conversations automatically — no client-side summarization loop, no session-splitting boilerplate.

Every long-running agent eventually hits the same wall: the context window fills up. The classic workaround is a client-side rolling summary — extract the conversation history, summarize it with a separate model call, replace the messages array, and continue. It works, but it's state management you have to build, test, and maintain.

Claude's server-side compaction removes that entire class of work. You opt in with one API edit, and the model handles it.

The API call

import anthropic

client = anthropic.Anthropic()

response = client.beta.messages.create(
    model="claude-sonnet-5-20260630",
    max_tokens=8096,
    betas=["interleaved-thinking-2025-05-14"],
    extra_headers={"anthropic-beta": "compact-2026-01-12"},
    context_management={
        "edits": [
            {
                "type": "compact_20260112",
                "instructions": "Preserve all code snippets, variable names, function signatures, and technical decisions. Summarize reasoning steps briefly.",
                "trigger_token_count": 80000   # fire when input reaches 80k tokens
            }
        ]
    },
    messages=conversation_history,
)

The beta header is compact-2026-01-12. The context_management.edits array takes a single compact_20260112 edit. The trigger_token_count sets when compaction fires — leave it out to use Claude's default (near-window-limit).

What happens when it fires

  1. Claude detects that input tokens reached trigger_token_count
  2. Generates a high-fidelity summary of the full conversation up to that point
  3. Replaces the conversation history with a compaction block containing the summary
  4. Continues the current response seamlessly — no visible break in the session

The resulting compaction block appears in response.content as a compaction_result type. Your next message turn picks it up automatically.

Tuning the instructions

The instructions field steers what the summary preserves. The default is generic; for code-heavy sessions, be explicit:

"instructions": "Always preserve: exact function names, class hierarchies, file paths, error messages, and any decision rationale. Summarize prose reasoning at high level."

For research agents: "Preserve all URLs, citations, data points, and factual claims verbatim. Compress narrative context."

When this matters most

  • Multi-hour coding sessions: a typical Claude Code session runs 30–120 tool calls with verbose outputs. Compaction fires once or twice per session and keeps quality high throughout.
  • Document-processing pipelines: chunked ingestion where each chunk adds to a growing context; compaction prevents runaway growth.
  • Customer support agents: long multi-turn conversations where you need to preserve facts stated early but not every word of the exchange.

What it doesn't replace

Compaction is for managing conversation history length. It doesn't clear stale tool outputs that are still in the messages array — for that, use context editing (remove specific message IDs from a turn). And it's not a substitute for good context hygiene upfront: keep system prompts tight, don't send large files when a summary will do, and use tool_result outputs judiciously.

Sources: Automatic context compaction — Claude Cookbook · Compaction — Claude Platform Docs · Context editing — Claude Platform Docs