skip to content

In OpenClaw, what does the automatic memory flush before compaction do, where does it write, and when is it skipped?

level: seniorimportance: should knowfreq 7%

answer

  1. a silent turn before summarising
  2. soft margin below the compaction threshold
  3. appends to today's daily note
  4. MEMORY.md is read-only here
  5. needs a writable workspace

basics

~20 s

Shortly before compaction, OpenClaw runs a silent turn telling the agent to append durable notes to today's memory/YYYY-MM-DD.md. It is on by default and skipped when disabled, when the workspace is read-only or absent, and on heartbeat turns and CLI backends.

solid answer

~40 s

When a session's context comes within a soft margin of the compaction threshold (`agents.defaults.compaction.memoryFlush.softThresholdTokens`, default 4000) or the transcript passes `forceFlushTranscriptBytes` (default `"2mb"`), OpenClaw runs a silent housekeeping turn. Its prompt tells the agent to append durable memories only to `memory/YYYY-MM-DD.md`, to treat `MEMORY.md`, `DREAMS.md`, `SOUL.md` and `AGENTS.md` as read-only, and to answer `NO_REPLY` if there is nothing to store. It runs on a private copy of the conversation, so its messages never reach the transcript, while its file writes persist. It runs once per compaction cycle and is skipped when `memoryFlush.enabled` is `false`, when the sandbox gives the session `workspaceAccess` `"ro"` or `"none"`, and on heartbeat turns and CLI backends. `memoryFlush.model` can pin it to a local model.

code

json · 13 lines
json
{
  "agents": {
    "defaults": {
      "compaction": {
        "memoryFlush": {
          "enabled": true,
          "softThresholdTokens": 4000,
          "model": "ollama/qwen3:8b"
        }
      }
    }
  }
}

go deeper

for a junior

Recall that before compaction OpenClaw quietly asks the agent to save important notes to today's daily memory file.

for a middle

Explain the triggers and the target: softThresholdTokens at 4000 by default, the 2mb transcript guard, once per cycle, and appending to memory/YYYY-MM-DD.md with MEMORY.md treated as read-only.

for a senior

Show you know when it silently does nothing: disabled, read-only or absent workspace in a sandbox, heartbeat turns and CLI backends, and that it is a prompt the model may answer with NO_REPLY.

for a principal

Weigh the flush's extra model turn against what long sessions lose without it, and decide whether isolating group sessions without workspace access is worth losing their notes.

## The problem it solves Long OpenClaw sessions eventually hit the model's context window. **Compaction** then summarises older turns so the session can continue; the detail of how compaction works belongs with agent runs. A summary keeps the gist and loses specifics, so a fact said once in a long conversation can disappear. The **automatic memory flush** gives the agent one chance to write durable notes to disk before that happens. ## When it fires - **Token trigger**: once projected context comes within `softThresholdTokens` of the compaction threshold. The default margin is **4000** tokens, set under `agents.defaults.compaction.memoryFlush`. - **Byte trigger**: once the model-visible transcript reaches `forceFlushTranscriptBytes`, default `"2mb"`, even when token counters are stale. `0` disables this guard. - **Once per compaction cycle**, tracked in the session row, so it does not repeat on every turn near the limit. For a 32,768-token window the built-in plan reserves 8,192 tokens and uses the 4,000-token margin, so flushing can start at 20,576 projected tokens while blocking compaction starts at 24,576. ## What it tells the agent The flush is a prompt, not a copy job. OpenClaw sends a silent "pre-compaction memory flush" turn whose instructions are: 1. store durable memories only in `memory/YYYY-MM-DD.md`, creating `memory/` if needed; 2. treat `MEMORY.md`, `DREAMS.md`, `SOUL.md` and `AGENTS.md` as read-only during the flush; 3. append to an existing daily file rather than overwrite it, and never create timestamped variants; 4. reply with the silent token `NO_REPLY` if there is nothing to store. The runtime backs the target up: a flush run is given only a read tool and an append-only write scoped to that daily file, so it cannot rewrite `MEMORY.md`. If the agent's tool policy removes `write` altogether, the flush cannot persist anything and the Gateway logs a warning. Within those limits the model decides what is worth saving: the flush raises the odds that a fact survives, it does not guarantee that a particular fact is saved. Promotion into `MEMORY.md` happens later, through dreaming or an explicit edit in a normal turn. ## What the user sees Nothing, by default. The flush runs against a private, detached view of the conversation: its internal prompts and replies never enter the original transcript, even if a new message interrupts it, while its writes to memory files are kept. With `agents.defaults.compaction.notifyUser` enabled, an exhausted flush produces a short degraded notice and the reply continues. A failed flush never resets the session or discards history. ## When it is skipped or saves nothing | Condition | Why | |---|---| | `memoryFlush.enabled: false` | switched off by the operator | | sandbox `workspaceAccess` is `"ro"` or `"none"` | the agent cannot write the daily file | | heartbeat turns | housekeeping is not run for them | | CLI backends | the flush runs only for embedded OpenClaw sessions | | no `write` tool survives tool policy | the turn cannot save; a warning is logged | The sandbox case catches people out. A common hardening pattern sandboxes group sessions with no workspace access; those sessions then never flush, and nothing from a long group conversation lands in the daily notes before compaction. A persisted sandbox requirement on a session overrides the agent's configuration here too. ## Keeping it cheap The flush is a full model turn. `agents.defaults.compaction.memoryFlush.model` pins it to another model, for example a local one, and the override is exact: it does not inherit the session's fallback chain, so a local-only flush never falls back to a paid model. That is a different key from `agents.defaults.compaction.model`, which only changes the model used to write the compaction summary. ## Where the flushed notes go next The flush is the first step of a longer path, not the end of it: 1. The flush appends durable notes to today's `memory/YYYY-MM-DD.md`. 2. The memory index picks up the changed file, so `memory_search` can find those notes in later sessions. 3. After a bare `/new` or `/reset`, today's and yesterday's notes are re-primed into the fresh session. 4. Dreaming may later promote the small durable part into `MEMORY.md`, if it passes the promotion gates. A session that compacts several times gets one flush per cycle, so a very long conversation can append to the daily note more than once. ## Things to check on an install - Long sessions that lose facts after compaction: confirm the flush is enabled and the workspace is writable for that session. - Daily notes that never grow: look for a read-only sandbox or a backend that skips the flush. - Unexpected spend near compaction: the flush is an extra turn; set `memoryFlush.model` if that matters.

  • A sandboxed group session in OpenClaw never adds anything to the daily notes before compaction. Why?
    The flush needs a writable workspace. If the sandbox gives the session `workspaceAccess` `"ro"` or `"none"`, a common setting for group sessions, OpenClaw skips the flush, so nothing is appended to `memory/YYYY-MM-DD.md` before the history is summarised. Either accept that trade for the isolation, or give that session a writable workspace mount.
  • Does the flush ever edit MEMORY.md?
    No. Its prompt names `memory/YYYY-MM-DD.md` as the only target and marks `MEMORY.md`, `DREAMS.md`, `SOUL.md` and `AGENTS.md` read-only, and the runtime enforces it: a flush run gets only read and an append-only write scoped to that daily file. Material reaches `MEMORY.md` later, through a dreaming sweep that promotes qualified items or through an explicit edit by the agent or owner in a normal turn.

  • The memory flush copies the whole conversation into MEMORY.md before compaction.
  • The flush guarantees that every fact from the conversation is saved.
  • The flush's housekeeping messages show up in the user's transcript.
  • Sandboxed sessions with a read-only workspace still run the flush.
  • Setting agents.defaults.compaction.model also changes the model the flush uses.
  • A failed flush resets the session and throws away its history.