Skip to content

Add loop detection for repeated identical tool calls #16583

@craigkitterman

Description

@craigkitterman

Problem

When a session accumulates large context (126K+ tokens), the model can enter a degenerate loop where it makes the same tool call repeatedly without processing the result. In my case, it issued process.poll on the same sub-agent 5+ times in a row, became completely unresponsive to new user messages, and required a gateway restart to recover.

Root Cause

  1. Context degradation at high token counts causes the model to repeat actions mechanically
  2. No circuit breaker exists in OpenClaw to detect and interrupt repeated identical tool calls

Proposed Fix

Detect when a session makes N identical (or near-identical) tool calls in a row (e.g. 3-5), then either:

  1. Force a context compaction to reduce token count and restore model reasoning quality
  2. Inject a system message like: "You appear to be in a loop making the same tool call repeatedly. Stop and reassess what you're doing."
  3. Or both: inject the message first, and if it happens again, force compaction

This would catch this class of bug regardless of context length or which tool is being looped.

Impact

Session becomes completely unresponsive. User messages queue up but are never answered. Only fix is a manual gateway restart.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    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