docs/concepts/session-pruning.md
Session pruning trims old tool results from the context before each LLM call. It reduces context bloat from accumulated tool outputs (exec results, file reads, search results) without rewriting normal conversation text.
<Info> Pruning is in-memory only -- it does not modify the on-disk session transcript. Your full history is always preserved. </Info>Long sessions accumulate tool output that inflates the context window. This increases cost and can force compaction sooner than necessary.
Pruning is especially valuable for Anthropic prompt caching. After the cache TTL expires, the next request re-caches the full prompt. Pruning reduces the cache-write size, directly lowering cost.
Pruning runs in cache-ttl mode, gated on both a time check and a context-size check:
softTrimRatio (default 0.3), skip pruning and keep the TTL clock running.... in between.hardClearRatio (default 0.5) and at least minPrunableToolChars (default 50,000) of prunable tool content remains, hard-clear those results: replace their content with a placeholder (default [Old tool result content cleared]).Two safety rules apply regardless of thresholds: the most recent keepLastAssistants assistant turns (default 3) are never pruned, and nothing before the session's first user message is ever pruned (protects bootstrap reads like SOUL.md/USER.md).
Only toolResult messages are eligible; normal conversation text is left alone. Use agents.defaults.contextPruning.tools.{allow,deny} to scope which tool names are prunable.
OpenClaw also builds a separate idempotent replay view for sessions that persist raw image blocks or prompt-hydration media markers in history.
user or toolResult history are replaced with [image data removed - already processed by model].[media attached: ...], [Image: source: ...], and media://inbound/... are replaced with [media reference removed - already processed by model]. Current-turn attachment markers stay intact so vision models can still hydrate fresh images.The bundled Anthropic plugin auto-configures pruning and heartbeat cadence the first time it resolves an Anthropic (or Claude CLI) auth profile, but only for fields you have not already set explicitly:
| Auth mode | contextPruning.mode | contextPruning.ttl | heartbeat.every |
|---|---|---|---|
| OAuth/token (including Claude CLI reuse) | cache-ttl | 1h | 1h |
| API key | cache-ttl | 1h | 30m |
If you set agents.defaults.contextPruning.mode or agents.defaults.heartbeat.every yourself, OpenClaw does not override them. This auto-default only fires for Anthropic-family auth; other providers get pruning off unless you configure it.
Pruning is off by default for non-Anthropic providers. To enable:
{
agents: {
defaults: {
contextPruning: { mode: "cache-ttl", ttl: "5m" },
},
},
}
To disable: set mode: "off".
| Pruning | Compaction | |
|---|---|---|
| What | Trims tool results | Summarizes conversation |
| Saved? | No (per-request) | Yes (in transcript) |
| Scope | Tool results only | Entire conversation |
They complement each other -- pruning keeps tool output lean between compaction cycles.
contextPruning.*)