skip to content

In OpenClaw, what does `agents.defaults.contextPruning` in `cache-ttl` mode remove from a session's context, and how does that differ from compaction?

level: seniorimportance: nice to knowfreq 4%

answer

  1. only one message type
  2. waits for the cache to expire
  3. soft-trim, then hard-clear
  4. transcript is never rewritten
  5. server-side on direct Anthropic

basics

~20 s

OpenClaw's cache-ttl pruning trims or clears old tool results in what the model sees, leaving conversation text and the stored transcript untouched. Compaction instead summarizes the older conversation itself into one entry when the context window nears its limit.

solid answer

~50 s

Session pruning touches only `toolResult` messages: old exec output, file reads, search results. With `agents.defaults.contextPruning.mode: "cache-ttl"`, client-side routes wait for the cache TTL to expire (5 minutes when no `ttl` is set), skip the round below roughly 30% context use, soft-trim results over 4,000 characters to their first and last 1,500, and hard-clear old results to a placeholder when usage is still around 50% or more with at least 50,000 characters of prunable tool output left. The last three assistant turns and everything before the first user message are never pruned, and the original transcript is not rewritten. In 2026.9.9, direct Anthropic API-key requests hand the job to Anthropic's server-side tool-result clearing instead. Compaction differs in kind: it summarizes the conversation. Pruning is off unless configured, except that the bundled Anthropic plugin seeds `cache-ttl` with a `1h` TTL.

code

json · 14 lines
json
{
  "agents": {
    "defaults": {
      "contextPruning": {
        "mode": "cache-ttl",
        "ttl": "5m",
        "hardClear": {
          "enabled": true,
          "placeholder": "[Old tool result content cleared]"
        }
      }
    }
  }
}

go deeper

for a junior

Recall that OpenClaw pruning trims old tool results only, while compaction summarizes the conversation, and that neither removes the original transcript from disk.

for a middle

Explain how cache-ttl mode is switched on, the 5-minute TTL when none is set, soft-trim against hard-clear, and the two rules that protect recent turns and bootstrap reads.

for a senior

Show you can reason about a route: direct Anthropic API-key requests use server-side clearing, other routes prune locally, and turning pruning off leaves existing projections replaying until compaction or a reset.

for a principal

Weigh pruning as a cost lever: it cuts cache writes and delays compaction for tool-heavy agents, but it hides old tool output from the model, so pick which tools are prunable with tools.allow and tools.deny.

## What pruning targets A long OpenClaw session accumulates **tool output**: every exec result, file read and web search stays in the history the model is sent. That output inflates the context, raises cost and forces compaction sooner. **Session pruning** is OpenClaw's answer to that one problem. It works only on `toolResult` messages and leaves the normal conversation text, user and assistant alike, as it was. Two facts set it apart from anything that deletes data: - The stored transcript is never rewritten. Pruning keeps a projected view in memory and records it in a hidden transcript marker so the same view survives a Gateway restart. - It is opt-in for most providers. Pruning is off unless `agents.defaults.contextPruning.mode` is set to `cache-ttl`; the bundled Anthropic plugin seeds `cache-ttl` with a `1h` TTL the first time it resolves an Anthropic auth profile, but only for fields you have not set yourself. ## How a client-side pruning round works On cache-TTL-eligible routes that prune locally (Amazon Bedrock, Google, OAuth, proxies and others), each new round follows this sequence: 1. **Wait for the cache TTL.** With no `ttl` configured the TTL is 5 minutes. Each successful model request refreshes the clock; until the TTL elapses, no new pruning happens. 2. **Check size.** Below roughly 30% of the model's context window, the round is skipped. 3. **Soft-trim.** A tool result over 4,000 characters keeps its first and last 1,500 characters with `...` between them. 4. **Hard-clear.** If usage is still at or above roughly 50% and at least 50,000 characters of prunable tool content remain, old results are replaced with a placeholder, by default `[Old tool result content cleared]`. `hardClear.enabled: false` skips this step. 5. **Record the projection.** Later requests reuse the same projected bytes, so the cached prefix stays stable. Two safety rules apply regardless of thresholds: the last three assistant turns are never pruned, and nothing before the session's first user message is pruned, which protects the bootstrap reads of workspace files. ## Direct Anthropic requests In 2026.9.9, a request to provider `anthropic` over the Messages API with an API key and the default endpoint delegates pruning to Anthropic's **server-side tool-result clearing**. OpenClaw derives the parameters itself (for example, keeping the three most recent tool uses), the `ttl` does not gate this path, and the full local history is retained. OpenClaw logs a line such as `[anthropic] server-side context edit: cleared N tool results` when clearing occurs. ## Pruning versus compaction | | Pruning | Compaction | |---|---|---| | What changes | Old tool results, trimmed or cleared | Older conversation turns, replaced by a summary | | When | After the cache TTL, above size thresholds | Near the context limit, on overflow, or on `/compact` | | Stored transcript | Original entries kept; projection recorded in a marker | Summary entry added; full history stays on disk | | Scope | Tool results only | The whole conversation | They complement each other: pruning keeps tool output lean between compaction cycles, so compaction runs less often. ## Why it matters for cost A tool-heavy agent can spend most of its context on output it no longer needs: a directory listing from an hour ago, a full log file it already summarized, three versions of the same web page. Without pruning, every later request resends those bytes and keeps the cached prefix large. With pruning, the old results shrink once, the reduced view is reused on every later request, and the session reaches the compaction threshold later. The trade-off is that the model can no longer quote an old result word for word; if it needs the data again, it has to run the tool again. ## Operating notes - `agents.defaults.contextPruning.tools.allow` and `.deny` scope which tool names are prunable. - Setting `mode: "off"` stops new rounds, but existing projections keep replaying, even across restarts, until compaction drops those results or the session is reset. - `/new` and a session reset start without the old session's projections. - Pruning is a context-cost control, not a privacy control: the original tool output remains in the session store.

  • Why does OpenClaw wait for the cache TTL before starting a new pruning round?
    Pruning exists largely for prompt caching. Changing tool results while the cached prefix is still warm would invalidate it and force a new cache write, so client-side rounds start only after the TTL has elapsed. Once a round runs, its projection is reused byte-for-byte on later requests, which keeps the reduced prefix stable for caching.
  • If an operator sets `contextPruning.mode` back to `off`, does the model see full tool output again?
    Not for results already pruned. Turning pruning off stops new rounds, but the recorded projections keep replaying, including after a Gateway restart, until compaction removes those results from active history or the session is reset with `/new` or `/reset`.

  • OpenClaw's session pruning deletes old tool output from the stored transcript.
  • Pruning summarizes old conversation turns, just as compaction does.
  • Context pruning is on by default for every model provider.
  • Pruning can trim the latest assistant turns when context is tight.
  • On client-side routes, a new pruning round can start before the cache TTL expires.

Assumes OpenClaw 2026.9.9; the split between Anthropic server-side clearing and client-side pruning, and the Anthropic plugin's seeded defaults, are release-specific behaviour.