Summary
Embedded agent sessions (used by heartbeat/cron/announce tasks) are not properly handling context window limits, resulting in repeated model_context_window_exceeded errors.
Reproduction
- Run OpenClaw gateway with an agent configured for periodic tasks (heartbeat, cron, or announce)
- Over time, the embedded session accumulates context without proper compaction
- Eventually hits the model context window limit (e.g., 204k tokens for GLM-5)
- Error is logged as
Unhandled stop reason: model_context_window_exceeded
Observed Behavior
From gateway logs:
08:23:30 [agent/embedded] embedded run prompt start: runId=f0b42d7b-c3cc-4e19-9d6b-0e4ad93feb8f sessionId=03d526d7-f019-4ed9-af92-0562c282be80
08:25:59 [agent/embedded] embedded run agent end: runId=f0b42d7b-c3cc-4e19-9d6b-0e4ad93feb8f isError=true error=Unhandled stop reason: model_context_window_exceeded
The session file grew to 2.6MB before failing.
Expected Behavior
- OpenClaw should automatically trigger compaction before context window is exceeded
- If context limit is somehow still reached, the error should be handled gracefully (not "Unhandled stop reason")
- Embedded sessions should be compacted just like regular sessions
Environment
- OpenClaw version: 2026.2.27
- Model: zai/glm-5 (contextWindow: 204800)
- Config:
compaction: { mode: "safeguard" }
Workaround
Deleting the bloated session file manually:
rm ~/.openclaw-trading/agents/main/sessions/<session-id>.jsonl
Then restart the gateway.
Suggested Fix
- Ensure embedded sessions respect compaction settings
- Add proper error handling for
model_context_window_exceeded stop reason
- Consider auto-cleanup of embedded sessions that exceed context limits
Summary
Embedded agent sessions (used by heartbeat/cron/announce tasks) are not properly handling context window limits, resulting in repeated
model_context_window_exceedederrors.Reproduction
Unhandled stop reason: model_context_window_exceededObserved Behavior
From gateway logs:
The session file grew to 2.6MB before failing.
Expected Behavior
Environment
compaction: { mode: "safeguard" }Workaround
Deleting the bloated session file manually:
Then restart the gateway.
Suggested Fix
model_context_window_exceededstop reason