openclaw - 💡(How to fix) Fix Feature Request: Tool-use loop detection to prevent repeated writes to same path

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

  • After 3 consecutive identical calls, block the call and return an error

Root Cause

  1. No framework-level loop detection: OpenClaw has no mechanism to detect or block consecutive identical tool calls on the same target
  2. Generic tool return: write only returns "Successfully wrote X bytes" without signaling to the agent that this path was just written
  3. No max-repeat limit: There's no hard cap on how many times the same tool can be called with the same arguments in a row

Fix Action

Workaround

As a short-term fix, we added a rule to our agent's AGENTS.md:

禁止对同一路径连续执行 write 或 edit,除非先 read 确认内容需要修改
每次工具调用后,检查是否已经对同一目标执行过相同操作
如果检测到重复模式,立即切换到下一步
不要在 thinking block 中重复相同的计划超过一次
```\n

Code Example

禁止对同一路径连续执行 write 或 edit,除非先 read 确认内容需要修改
每次工具调用后,检查是否已经对同一目标执行过相同操作
如果检测到重复模式,立即切换到下一步
不要在 thinking block 中重复相同的计划超过一次
RAW_BUFFERClick to expand / collapse

Problem

An agent repeatedly wrote the same file 4 times in a row with nearly identical content. The write tool was invoked on the same path (src/app/api/intelligence/insights/route.ts) consecutively (seq 211, 213, 217 in the session), each time with ~3070 bytes of the same content. The agent's thinking block was also identical across all 4 iterations:

"The user wants to replace the hardcoded mock data... Let me plan the implementation: 1. Create a new API route... 2. Update the frontend... 3. Add loading state..."

The loop was only broken by a new user message arriving. The session remained in running state afterward.

Root Cause

  1. No framework-level loop detection: OpenClaw has no mechanism to detect or block consecutive identical tool calls on the same target
  2. Generic tool return: write only returns "Successfully wrote X bytes" without signaling to the agent that this path was just written
  3. No max-repeat limit: There's no hard cap on how many times the same tool can be called with the same arguments in a row

Impact

  • Wasted tokens (each iteration consumed context and LLM calls)
  • Session stuck in running state
  • User experience degraded (agent appeared unresponsive while looping)

Proposed Solutions

Option A: Framework-level loop guard (preferred)

  • Detect when the same tool is called N times in a row with the same key argument (e.g., same file path for write/edit)
  • After 2 consecutive identical calls, inject a system warning: "⚠️ You already wrote to this path. Consider reading back or moving to the next step."
  • After 3 consecutive identical calls, block the call and return an error

Option B: Enhanced tool return metadata

  • Include metadata in tool responses: {\"success\": true, \"path\": \"...\", \"wasJustWritten\": true, \"lastWrittenSeq\": 217}
  • Agents can use this to self-correct

Option C: Agent-configurable rules

  • Allow agents to define loop-prevention rules in their AGENTS.md / system prompt
  • This already works as a workaround but puts the burden on every agent

Workaround

As a short-term fix, we added a rule to our agent's AGENTS.md:

禁止对同一路径连续执行 write 或 edit,除非先 read 确认内容需要修改
每次工具调用后,检查是否已经对同一目标执行过相同操作
如果检测到重复模式,立即切换到下一步
不要在 thinking block 中重复相同的计划超过一次
```\n

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Feature Request: Tool-use loop detection to prevent repeated writes to same path