How it works
- Older conversation turns are summarized into a compact entry.
- The summary is saved in the session transcript.
- Recent messages are kept intact.
toolResult entries when it picks a compaction split point. If the point lands inside a tool block, OpenClaw moves the boundary so the pair stays together and the current unsummarized tail is preserved.
The full conversation history stays on disk. Compaction only changes what the model sees on the next turn.
New configs default
agents.defaults.compaction.mode to "safeguard" (stricter guardrails, summary quality audits). Set mode: "default" explicitly to opt out.Auto-compaction
Auto-compaction is on by default. It runs when the session nears the context limit, or when the model returns a context-overflow error (in which case OpenClaw compacts and retries). Setagents.defaults.compaction.enabled: false to disable the embedded runtime’s proactive threshold compaction. OpenClaw’s preflight and overflow-recovery compaction paths remain available, as does manual /compact.
You will see:
embedded run auto-compaction start/completein normal Gateway logs.🧹 Auto-compaction completein verbose mode./statusshowing🧹 Compactions: <count>.
Before compacting, OpenClaw automatically reminds the agent to save important notes to memory files. This prevents context loss.
Overflow error patterns OpenClaw recognizes
Overflow error patterns OpenClaw recognizes
OpenClaw matches dozens of provider-specific overflow error strings (Anthropic, OpenAI, Bedrock, Gemini, Ollama, OpenRouter, and more). Common examples:
request_too_largecontext length exceededinput exceeds the maximum number of tokensinput token count exceeds the maximum number of input tokens(Bedrock)input is too long for the modelollama error: context length exceeded
Manual compaction
Type/compact in any chat to force a compaction. Add instructions to guide the summary:
agents.defaults.compaction.keepRecentTokens (default: 20,000) as its cut-point budget and keeps that recent tail in rebuilt context.
Configuration
Configure compaction underagents.defaults.compaction in your openclaw.json. The most common knobs are listed below; for the full reference, see Session management deep dive.
Using a different model
By default, compaction uses the agent’s primary model. Setagents.defaults.compaction.model to delegate summarization to a more capable or specialized model. The override accepts a provider/model-id string or a bare alias configured under agents.defaults.models:
agents.defaults.compaction.model override remains exact and does not inherit the session fallback chain.
Identifier preservation
Compaction summarization preserves opaque identifiers by default (identifierPolicy: "strict"). Override with identifierPolicy: "off" to disable. Custom guidance belongs in a compaction provider’s summarize() implementation.
Active transcript byte guard
Whenagents.defaults.compaction.maxActiveTranscriptBytes is set, OpenClaw
triggers normal local compaction before a run if transcript history reaches
that size. This is useful for long-running sessions where provider-side context
management may keep model context healthy while persisted transcript history
keeps growing. Set a positive byte count or size string such as "20mb" to opt
in; 0 or an unset value disables the guard. It does not split raw bytes; it
asks the normal compaction pipeline to create a semantic summary. For Codex
app-server sessions, the same threshold caps native rollout transcripts and
oversized native threads restart fresh.
Successor transcripts
A context engine may return an explicit compacted successor session identity. OpenClaw adopts that successor and records checkpoint metadata against it. The built-in SQLite compactor keeps the current session identity and does not create a second runtime transcript. OpenClaw no longer writes separate.checkpoint.*.jsonl copies for new
compactions. Existing legacy checkpoint files can still be used while referenced
and are pruned by normal session cleanup.
Compaction notices
By default, compaction runs silently. SetnotifyUser to show brief status messages when compaction starts and completes, and to surface a degraded notice when a pre-compaction memory flush is exhausted but the reply still continues:
Memory flush
Before compaction, OpenClaw can run a silent memory flush turn to store durable notes to disk. Setagents.defaults.compaction.memoryFlush.model when this housekeeping turn should use a local model instead of the active conversation model:
Pluggable compaction providers
Plugins can register a custom compaction provider viaregisterCompactionProvider() on the plugin API. When a provider is registered and configured, OpenClaw delegates summarization to it instead of the built-in LLM pipeline.
To use a registered provider, set its id in your config:
provider automatically forces mode: "safeguard". Providers receive the same compaction instructions and identifier-preservation policy as the built-in path, and OpenClaw still preserves recent-turn and split-turn suffix context after provider output.
The built-in quality audit and its corrective retries apply only to built-in
summarization. Configured provider output keeps the provider’s existing
validation semantics.
If the provider fails or returns an empty result, OpenClaw falls back to built-in LLM summarization.
Compaction vs pruning
Session pruning is a lighter-weight complement that trims tool output without summarizing.
Troubleshooting
Compacting too often? The model’s context window may be small, or tool outputs may be large. Try enabling session pruning. Context feels stale after compaction? Use/compact Focus on <topic> to guide the summary, or enable the memory flush so notes survive.
Need a clean slate? /new starts a fresh session without compacting.
For advanced configuration (reserve tokens, identifier preservation, custom context engines, OpenAI server-side compaction), see the Session management deep dive.
Related
- Session: session management and lifecycle.
- Session pruning: trimming tool results.
- Context: how context is built for agent turns.
- Hooks: compaction lifecycle hooks (
before_compaction,after_compaction).