Context Window & Compaction
Every model has a context window (max tokens it can see). Long-running chats accumulate messages and tool results; once the window is tight, OpenClaw compacts older history to stay within limits.What compaction is
Compaction summarizes older conversation into a compact summary entry and keeps recent messages intact. The summary is stored in the session history, so future requests use:- The compaction summary
- Recent messages after the compaction point
Configuration
Use theagents.defaults.compaction object in your openclaw.json to control compaction behavior.
Fields
Examples
Disable compaction entirely:Auto-compaction (default on)
When a session nears or exceeds the model’s context window, OpenClaw triggers auto-compaction and may retry the original request using the compacted context. You’ll see:🧹 Auto-compaction completein verbose mode/statusshowing🧹 Compactions: <count>
Manual compaction
Use/compact (optionally with instructions) to force a compaction pass:
Context window source
Context window is model-specific. OpenClaw uses the model definition from the configured provider catalog to determine limits.Compaction vs pruning
- Compaction: summarises and persists in JSONL.
- Session pruning: trims old tool results only, in-memory, per request.
OpenAI server-side compaction
OpenClaw also supports OpenAI Responses server-side compaction hints for compatible direct OpenAI models. This is separate from local OpenClaw compaction and can run alongside it.- Local compaction: OpenClaw summarizes and persists into session JSONL.
- Server-side compaction: OpenAI compacts context on the provider side when
store+context_managementare enabled.
Tips
- Use
/compactwhen sessions feel stale or context is bloated. - Large tool outputs are already truncated; pruning can further reduce tool-result buildup.
- If you need a fresh slate,
/newor/resetstarts a new session id.
Troubleshooting
Compaction triggers but the session still hits context limits The summary may be larger than expected, or recent messages alone already exceed the window. Check yourkeepRecentTokens and reserveTokens settings. If recent messages are unavoidably large (for example, huge tool outputs), combine compaction with session pruning (contextPruning) to trim tool results before the LLM call.
Identifier (ticket ID, branch name) lost after compaction
The default identifierPolicy: "strict" adds built-in guidance, but identifiers buried deep in tool output may not survive. Switch to "custom" and provide explicit identifierInstructions naming the identifier patterns you care about (for example "Preserve all PROJ-* ticket IDs exactly"). Confirm with /compact Focus on ticket IDs and branch names.
enabled: false still seems to compact
Config is read at gateway start. Restart the gateway after changing compaction.enabled. Also verify the config is under agents.defaults.compaction, not a top-level key.
Memory flush runs unexpectedly or too often
The flush fires when the transcript file reaches memoryFlush.forceFlushTranscriptBytes. Set it to 0 to disable size-based triggers, or increase the value. Set memoryFlush.enabled: false to turn off pre-compaction memory flushes entirely.
Related: Session pruning · Memory · Session management · OpenAI provider