Every model has a context window: the maximum number of tokens it can process. When a conversation approaches that limit, OpenClaw compacts older messages into a summary so the chat can continue.Documentation Index
Fetch the complete documentation index at: https://docs.openclaw.ai/llms.txt
Use this file to discover all available pages before exploring further.
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. If a split 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.
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). You will see:🧹 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.
Recognized overflow signatures
Recognized overflow signatures
OpenClaw detects context overflow from these provider error patterns:
request_too_largecontext length exceededinput exceeds the maximum number of tokensinput token count exceeds the maximum number of input tokensinput 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 is set, manual compaction honors that Pi cut-point and keeps the recent tail in rebuilt context. Without an explicit keep budget, manual compaction behaves as a hard checkpoint and continues from the new summary alone.
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 any provider/model-id string:
Identifier preservation
Compaction summarization preserves opaque identifiers by default (identifierPolicy: "strict"). Override with identifierPolicy: "off" to disable, or identifierPolicy: "custom" plus identifierInstructions for custom guidance.
Active transcript byte guard
Whenagents.defaults.compaction.maxActiveTranscriptBytes is set, OpenClaw triggers normal local compaction before a run if the active JSONL reaches that size. This is useful for long-running sessions where provider-side context management may keep model context healthy while the local transcript keeps growing. It does not split raw JSONL bytes; it asks the normal compaction pipeline to create a semantic summary.
Successor transcripts
Whenagents.defaults.compaction.truncateAfterCompaction is enabled, OpenClaw does not rewrite the existing transcript in place. It creates a new active successor transcript from the compaction summary, preserved state, and unsummarized tail, then keeps the previous JSONL as the archived checkpoint source.
Successor transcripts also drop exact duplicate long user turns that arrive
inside a short retry window, so channel retry storms are not carried into the
next active transcript after compaction.
Pre-compaction checkpoints are retained only while they stay below OpenClaw’s
checkpoint size cap; oversized active transcripts still compact, but OpenClaw
skips the large debug snapshot instead of doubling disk usage.
Compaction notices
By default, compaction runs silently. SetnotifyUser to show brief status messages when compaction starts and completes:
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.
If the provider fails or returns an empty result, OpenClaw falls back to built-in LLM summarization.
Compaction vs pruning
| Compaction | Pruning | |
|---|---|---|
| What it does | Summarizes older conversation | Trims old tool results |
| Saved? | Yes (in session transcript) | No (in-memory only, per request) |
| Scope | Entire conversation | Tool results only |
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).