What is the best way to configure this setting and what prompts will be the best?
"compaction": {
"mode": "safeguard",
"reserveTokensFloor": 20000,
"memoryFlush": {
"enabled": true,
"softThresholdTokens": 30000,
"prompt": "Write any lasting notes to memory/YYYY-MM-DD.md; reply with NO_REPLY if nothing to store.",
"systemPrompt": "Session nearing compaction. Store durable memories now."
}
},
#compaction
1 messages · Page 1 of 1 (latest)
This config lives under agents.defaults.compaction (or per-agent under agents.list[].compaction). What you pasted matches the documented shape.
Docs worth keeping open while tuning:
- Compaction config ref: https://docs.openclaw.ai/gateway/configuration-reference#agentsdefaultscompaction
- Memory flush details + threshold formula: https://docs.openclaw.ai/concepts/memory
- Deep dive (why the floor exists, “silent” NO_REPLY turns): https://docs.openclaw.ai/reference/session-management-compaction
What each knob actually does (and how I’d tune it)
mode: "safeguard"
default= normal compaction summarizationsafeguard= chunked/guardrailed summarization for very long histories
If you routinely have long sessions or lots of tool output,safeguardis the safer default.
reserveTokensFloor
OpenClaw enforces a minimum “headroom” (reserveTokens) for Pi embedded sessions:
- If Pi’s
reserveTokensis lower than this floor, OpenClaw bumps it up. - Purpose: leave enough room for “housekeeping” (like the memory flush turn) before compaction becomes unavoidable.
Rule of thumb:
- Leave it at the default-ish 20k unless you have a reason.
- If you’re using a small context window model, a 20k floor can cause compaction to kick in way earlier than you expect (because you’re reserving a huge chunk of the window). In that case, lower it.
memoryFlush.softThresholdTokens
This controls how early the pre-compaction flush triggers.
From the docs, the flush triggers when the session estimate crosses:
contextWindow - reserveTokensFloor - softThresholdTokens
So:
- Bigger
softThresholdTokens⇒ flush happens earlier (further away from the hard limit). - Smaller ⇒ flush happens later (risking “too late to write memory comfortably”).
Your 30000 is very early for most setups. It’s not “wrong” (it’s only one extra silent turn per compaction cycle), but most people will be fine with something like 4k–10k.
Also note:
- It only runs when the workspace is writable (read-only sandboxes skip it).
- It runs once per compaction cycle (tracked in
sessions.json), so it won’t spam every message.
Prompts: what works well in practice
You generally want:
- Explicit instructions to append (not overwrite)
- A tight schema (bullets, max count)
- A hard requirement to return
NO_REPLYso nothing leaks to the user
Recommended systemPrompt
Keep it short and “authority” level:
systemPrompt: "Silent pre-compaction memory flush. If anything durable should be saved, append it to memory/YYYY-MM-DD.md. Do not produce user-visible output; end with NO_REPLY."
Recommended prompt (good default)
This one tends to produce high-signal notes without bloat:
prompt: [
"Before compaction, save any durable context that would be costly to lose.",
"",
"If needed, APPEND to memory/YYYY-MM-DD.md (do not overwrite).",
"Write at most 10 bullet points, focusing on:",
"- decisions made",
"- open tasks / next steps",
"- important commands/config values/URLs",
"- user preferences / constraints",
"",
"If there's nothing worth saving, reply exactly: NO_REPLY",
"After writing the file, reply exactly: NO_REPLY"
].join("\\n")
If you run in group/public channels
Add a safety line to avoid dumping sensitive chat logs into memory:
"Do NOT store secrets or private personal data. Summarize, don’t quote large blocks."
A solid “start here” config
(Uses conservative thresholds that match the docs’ intent)
agents: {
defaults: {
compaction: {
mode: "safeguard",
reserveTokensFloor: 20000,
memoryFlush: {
enabled: true,
softThresholdTokens: 6000, // try 4000–10000
systemPrompt: "Silent pre-compaction memory flush. Store durable memories now. End with NO_REPLY.",
prompt: "Before compaction, append any lasting notes to memory/YYYY-MM-DD.md (max 10 bullets, decisions + next steps + key config). Reply NO_REPLY if nothing to store; after writing reply NO_REPLY."
}
}
}
}
How to tell if it’s tuned right
- Turn on verbose temporarily and watch for
🧹 Auto-compaction complete. - Use
/statusto see compaction count + context pressure. - If you notice memory flush happening too late (it misses key context), increase
softThresholdTokens. - If it’s happening annoyingly early, decrease it.
If you tell me what model context window you’re targeting (roughly: 16k/32k/128k/200k), I can suggest a more precise reserveTokensFloor + softThresholdTokens pair.
у моей модели контекст в 1М токенов, я хочу с.кономить чтобы очистка была на 500К токенов
Если у модели окно 1М, то по умолчанию авто-компакция будет срабатывать слишком поздно (почти у лимита), и вы реально можете начать “скармливать” модели сотни тысяч токенов → дорого.
Чтобы компакция/порог считались как будто окно = 500K, используйте кап на эффективное окно контекста:
1) Ограничьте “эффективное” окно до 500K
В конфиге:
{
agents: {
defaults: {
contextTokens: 500000,
compaction: {
mode: "safeguard",
reserveTokensFloor: 20000,
memoryFlush: {
enabled: true,
softThresholdTokens: 50000,
systemPrompt: "Silent pre-compaction memory flush. Store durable memories now. End with NO_REPLY.",
prompt: "Before compaction, append any lasting notes to memory/YYYY-MM-DD.md (max 10 bullets: decisions + next steps + key config). If nothing to store, reply NO_REPLY. After writing, reply NO_REPLY."
}
}
}
}
}
Почему это решает задачу: OpenClaw будет считать contextWindow = contextTokens (если он меньше реального окна модели), и авто-компакция начнёт происходить примерно в районе ваших 500K, а не 1M.
2) Как выбрать softThresholdTokens при цели “компакция на 500K”
Триггер memory flush (из доков) происходит примерно когда:
contextWindow - reserveTokensFloor - softThresholdTokens
При contextTokens: 500000, reserveTokensFloor: 20000:
- если
softThresholdTokens: 50000→ flush около 430K - если
softThresholdTokens: 30000→ flush около 450K - если
softThresholdTokens: 100000→ flush около 380K (раньше, но безопаснее)
3) Важно про термин “очистка”
Компакция не удаляет историю с диска — она сворачивает старый контекст в summary и дальше в модель уходит уже summary + свежие сообщения. Это и даёт экономию токенов.
Если скажете, какой у вас типичный “ответ” по длине (например, 2–5k токенов или 20k+), я подскажу, стоит ли поднимать reserveTokensFloor (например до 50k) чтобы не упираться в headroom при больших выводах.
Which is better: the Kimi K 2.5 or the Gemini 3 Flash?