Skip to content

Bug: Unhandled stop reason: model_context_window_exceeded in embedded agent sessions #35868

@daplm1974

Description

@daplm1974

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

  1. Run OpenClaw gateway with an agent configured for periodic tasks (heartbeat, cron, or announce)
  2. Over time, the embedded session accumulates context without proper compaction
  3. Eventually hits the model context window limit (e.g., 204k tokens for GLM-5)
  4. 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

  1. OpenClaw should automatically trigger compaction before context window is exceeded
  2. If context limit is somehow still reached, the error should be handled gracefully (not "Unhandled stop reason")
  3. 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

  1. Ensure embedded sessions respect compaction settings
  2. Add proper error handling for model_context_window_exceeded stop reason
  3. Consider auto-cleanup of embedded sessions that exceed context limits

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions