Feature Request: Implement Agentic Loop Detection Service to Prevent Repetitive Actions

Resolved 💬 1 comment Opened Jul 24, 2025 by coygeek Closed Jul 26, 2025

Title: Feature Request: Implement Agentic Loop Detection Service to Prevent Repetitive Actions

Labels: feature-request, enhancement, agent, reliability, cost-saving

Is your feature request related to a problem? Please describe.

As an agentic tool, Claude Code can sometimes get stuck in a repetitive, non-productive loop. This can happen in a few ways:

  1. The agent repeatedly calls the same tool with the exact same arguments.
  2. The agent gets stuck "chanting," where it generates the same sentence or phrase over and over.

In non-interactive or scripted scenarios, such as those enabled by the Claude Code SDK, these loops can run unchecked. This leads to a poor user experience, scripts that appear to hang indefinitely, and significant waste of API tokens and associated costs.

The current --max-turns flag is a useful safeguard against infinitely long conversations, but it is a brute-force method. It does not catch tight loops that can occur within just a few turns, leading to wasted resources before the maximum turn limit is reached.

Describe the solution you'd like

I propose the implementation of a Loop Detection Service that actively monitors the agent's behavior during a turn to identify and halt repetitive loops early. This feature is inspired by a similar, effective implementation in Google's Gemini CLI (loopDetectionService.ts).

The service should detect two primary types of loops:

  1. Tool Call Loops: The service should track consecutive, identical tool calls. If the agent calls the exact same tool with the exact same arguments more than a set number of times (e.g., 5 consecutive times), it should be considered a loop. The check should be based on a hash or deep comparison of the tool name and its arguments.
  1. Content Loops (Chanting): The service should monitor the text generated by the model. If the model generates the exact same sentence consecutively more than a set number of times (e.g., 10 consecutive times), it should be flagged as a loop.

Behavior on Loop Detection:
When a loop is detected, the agent should:

  1. Immediately halt the current turn.
  2. Stop further tool calls or text generation in that turn.
  3. Output a clear, informative message to the user (or the SDK's output stream) stating that a loop was detected and the turn was aborted. For example: [Claude Code] Error: Repetitive tool call loop detected. Halting turn to prevent infinite execution.

Describe alternatives you've considered

The main alternative is the existing --max-turns CLI flag. While helpful, it doesn't solve the core problem of tight, inefficient loops that can burn through many tokens before the limit is ever reached. A dedicated loop detection service would be a more intelligent and immediate solution.

Additional Context

This feature would be a significant improvement for the reliability and cost-effectiveness of Claude Code, especially for developers using it in automated workflows via the SDK (en/docs/claude-code/sdk).

  • Reliability: It would prevent scripts from hanging, making Claude Code a more dependable tool for automation. This would be a great addition to the Troubleshooting documentation (en/docs/claude-code/troubleshooting), as some "hangs or freezes" may be caused by these loops.
  • Cost-Saving: By halting wasteful token usage early, this feature directly addresses concerns outlined in the Cost Management documentation (en/docs/claude-code/costs). It provides a smart safeguard against unexpected high bills from a runaway agent.

Implementing this feature would bring Claude Code's agentic capabilities more in line with other state-of-the-art tools and provide a crucial layer of safety and efficiency for all users.

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗