#Does openclaw do auto compaction
1 messages · Page 1 of 1 (latest)
Yep â OpenClaw has auto-compaction (and you can also force it manually with /compact).
Auto-compaction (Pi runtime) happens when either:
- the model throws a context overflow â compact â retry, or
- after a successful turn when
contextTokens > contextWindow - reserveTokens
So to make it trigger earlier, you generally want a bigger reserveTokens (that makes the threshold hit sooner).
Easiest: force it anytime
In the chat:
/compact Focus on decisions + current TODOs
Make auto-compaction trigger earlier (config)
In ~/.openclaw/openclaw.json:
{
agents: {
defaults: {
compaction: {
// Bigger = compacts earlier
reserveTokens: 40000,
// Optional: how much recent context to keep after compaction
keepRecentTokens: 12000
}
}
}
}
Then restart the Gateway.
Docs:
- https://docs.openclaw.ai/concepts/compaction
- https://docs.openclaw.ai/reference/session-management-compaction
If you tell me what model/context window youâre on (or paste /status), I can suggest sane numbers so it doesnât end up compacting every turn.
even if the model suport 200k i barely want to do that. i think 50k is sweet point for what we are doing here. i use mostly custom provider, in this case i think 200k is the default context weindowïŒ so i will set a 150k reserve tokens?
If youâre on a custom provider and you didnât specify contextWindow for the model, then yes: OpenClaw currently defaults it to 200000 for custom providers. (Docs: https://docs.openclaw.ai/concepts/model-providers)
And your math is right:
- auto-compaction threshold is roughly:
contextWindow - reserveTokens - so with
contextWindow = 200kand you want compaction to kick in around~50k,
setreserveTokens â 150k.
Recommended config
Also make sure keepRecentTokens is below that threshold, otherwise you can end up âcompactingâ but still staying above the trigger point.
{
agents: {
defaults: {
compaction: {
reserveTokens: 150000, // triggers around 50k (if contextWindow is 200k)
keepRecentTokens: 20000 // keep last ~20k verbatim; summarize older stuff
}
}
}
}
Two gotchas
-
Compaction cost/frequency: doing this will make compaction happen much more often. That can save you per-turn context costs, but youâll pay extra âsummary turnsâ. Itâs worth it when your normal turns would otherwise drag huge context every time.
-
Make
contextWindowexplicit (best practice): if your proxy/model isnât actually 200k, set it inmodels.providers.<yourProvider>.models[]so OpenClawâs token math matches reality.
Deep dive on the trigger logic: https://docs.openclaw.ai/reference/session-management-compaction
If you paste your models.providers.<name>.models entry (just that block), I can suggest a couple of âsafeâ presets for a ~50k operating point.
Adding to what Krill said â another approach that works well if you want proactive compaction without touching config: have your agent monitor its own context usage during heartbeats.
You can call session_status which shows something like 45000/200000 tokens. Then in your HEARTBEAT.md, add a rule like:
If context usage >65%, proactively compact.
The agent will check context during each heartbeat cycle and trigger /compact before it ever hits the auto-compaction threshold. This gives you softer, more predictable compaction without risking it firing every turn from an aggressive reserveTokens setting.
Both approaches work â config-based for a hard threshold, heartbeat-based for a more dynamic "compact when I'm getting full" pattern.
Good idea in spirit, but small correction on whatâs actually possible today:
session_statusis an OpenClaw operator tool (what we can call here / what/statusshows you in chat). The LLM running your heartbeat doesnât have a âread my current contextTokensâ tool it can call.- And
/compactis a control command on inbound chat messages â it isnât something the agent can âinvoke internallyâ during a heartbeat run unless it literally receives/compactas an incoming message from an allowed sender.
So if you want âcompact around ~50kâ, the supported / predictable way is still the config knob:
reserveTokens â contextWindow - 50k (and keep keepRecentTokens comfortably below 50k so compaction doesnât immediately re-trigger).
If youâd like âcompact at X%â as a first-class feature, thatâd be a solid GitHub issue/feature request.