BroadRouter
Get started

Context compaction

Updated

Context compaction is the practice of summarizing a conversation that is nearing the context window limit and continuing the session from that summary, so an agent keeps key decisions and state while dropping raw history such as old tool output.

Also called: compaction, auto-compact, conversation compaction, context summarization

Long agent sessions outgrow their context window. Compaction swaps the full history for a summary and carries on.

How it works

Anthropic’s context engineering post defines compaction as “the practice of taking a conversation nearing the context window limit, summarizing its contents,” and starting a new context window with that summary. In Claude Code, the post says, the model “preserves architectural decisions, unresolved bugs, and implementation details,” drops redundant tool output, and continues with the summary plus “the five most recently accessed files.”

The lightest form is tool result clearing. The same post calls it “one of the safest lightest touch forms of compaction”: old raw tool outputs deep in the history are removed, and everything else stays.

Tools expose compaction in different ways:

Tool How compaction runs
Claude Code Auto-compaction summarizes history near the limit; /compact Focus on ... runs it by hand with instructions on what to keep
OpenAI Responses API context_management with a compact_threshold compacts server-side when the token count crosses it, or call the standalone compact endpoint
Your own agent Ask the model to summarize the history, then start a new message list with the system prompt, the summary and the latest turns

A worked number: an agent at 180,000 tokens of history compacts to a 6,000-token summary plus 20,000 tokens of recent files. Every later turn sends about 26,000 tokens of history instead of 180,000 and growing.

Why it matters

  • Length. Without compaction, a long task stops when the context window fills.
  • Quality. Accuracy can drop well before the limit, a problem called context rot. A shorter, cleaner context can help.
  • Cost. On per-token billing, every turn resends the history. Compaction shrinks it, though the compaction request itself reads the full history once, and the rewritten prefix must be cached again (cached input tokens).
  • Risk. A summary can drop a detail that matters later. Write custom instructions on what to keep, and keep critical facts in a notes file.

On BroadNet Inference

BroadNet’s API is stateless Chat Completions: you send the full message list each time, and BroadNet’s docs do not describe a server-side compaction feature. Compaction happens in your agent or tool, which then sends the shorter history.

DeepSeek V4.1 Flash and GLM 5.3 accept 1,048,576 tokens of context, so compaction is needed less often, but the cost and quality points above still apply on pay-as-you-go. On a flat-rate plan there is no token meter, so compaction matters for quality and speed rather than the bill. Context management for long-running agents covers when to compact and what to drop first.

Questions

Does compaction lose information?

Yes, by design. A summary keeps what the summarizer judged important and drops the rest. Keep facts you cannot lose, such as requirements and open bugs, in a notes file the agent can reread.

Is compaction free?

No. Claude Code's docs point out that compacting reads the whole conversation it summarizes, so compacting a large context is itself a large request. Starting fresh with /clear costs nothing, but keeps no continuity.

Does compaction break prompt caching?

It rewrites the history, so the next request misses the cache for the rewritten part. Claude Code counts this as an expected cache rebuild. After that, the shorter history caches as usual.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity