Mahal Systems

Overview › 7Orchestration › b.Bounding execution › i.

Detect stalls and loops: warn, then stop early

Control flow General

Track repeated identical actions (same tool, same arguments; same search query) and turns with no new information. Inject a warning at a small streak (about 5 identical calls), refuse exact-repeat queries, and end the run early at a longer streak (about 8 identical failures) or when progress has stalled, instead of running to the turn cap. A prompt line telling the agent not to repeat itself is a weak version of the same thing and is not a substitute.

Tier 2 because an execution bound: runaway loops are hard failures.

When it applies

  • In the code: no comparison of a tool call or query against previous ones; the loop ends only at a fixed turn cap
  • In traces: tool spans with identical name and arguments repeated 3 or more times in a run; the same query string issued twice; runs ending at the turn cap; failure rows labeled redundancy or state-tracking-failure
  • In eval results: turn-cap and timeout outcomes as a share of failures

How

  1. In the loop, hash each tool call (name plus arguments) and each query; keep the last N.
  2. On a streak of 5, inject a 'you are repeating; change approach' message; on 8 identical failures, stop with the best answer so far.
  3. Define 'stalled' for the task (no new file, result or fact in K turns) and stop on it.
  4. Measure loop and turn-cap failures before and after.

How to measure it

  • before after (tier 2): loop and turn-cap failure rate on the same cases; time per case

To compare the change against your agent's current version, with the same cases and enough repeats to see past run-to-run noise, use aot-evals.

Expected impact

  • Quality: improves, medium: adaptive termination +5.6% on FACTS; anti-loop prompt line GAIA +0.04, flat on SimpleQA (VeRO)
  • Cost: improves, medium
  • Performance: improves, medium: the template with query dedup ran about 2x faster, bundled

Evidence

Evidence comes from: Code generation, Web research.

  1. TerminalBench-2

    Part of the Filesystem-B=178 commit; contributed to AgentTimeoutError rising 16 to 23 as crashing tasks migrated to timeouts rather than passes.

    Scale AI, "VeRO: A Harness for Agents to Optimize Agents" (ICML 2026) · Feb 2026 · paper §A.4

  2. library pattern

    Hash recent tool calls, inject a warning on repeat.

    Scale AI, VeRO code release · cookbook (master-agent-optimization-cookbook, Infinite Loop Prevention)

  3. Detect identical-call streaks; warn, then terminateNot measured

    Held fixed across all conditions. The reminder texts and thresholds are given.

    Fan et al., "An Empirical Study of Harness Design for Coding Agents" · Sep 2026 · §2.4 Stuck detection; §3.1; App 7.4 Fig 20

  4. Hash repeated tool calls to detect stuck loopsNot measured

    Hermes hashes the call signature (SHA-256), "warning after two identical failures and halting after eight—but the hard stop ships disabled"; Gemini CLI aborts on "five identical tool calls or ten identical content chunks"; OpenCode's "three consecutive byte-identical tool calls raise a doom_loop permission ask"; OpenHands StuckDetector covers 5 scenarios.

    Barbaste et al., "Harness Engineering: Anatomy, Architecture, and Evolution of Coding Agents -- A Source-Code Study of Eleven Systems" · Jul 2026 · Sec 6.2; Table 5; Table 11 "Stuck Detection"; Recommendation 18

  5. Add a periodic LLM self-check for loopsNot measured

    Gemini CLI LoopDetectionService: "after thirty turns in a single prompt an LLM-based self-check runs at an adaptive interval—a two-tier design"; paper calls this a "platform-tier investment".

    Barbaste et al., "Harness Engineering: Anatomy, Architecture, and Evolution of Coding Agents -- A Source-Code Study of Eleven Systems" · Jul 2026 · Sec 6.2 (Gemini CLI); Recommendation 18

  6. Cap consecutive malformed responses and wall-clock timeNot measured

    Mini-SWE-Agent v2.4 "added a wall-clock time limit on runs and a cap on consecutive malformed-action responses"; Rec 18: "do ship the cheap caps, which cost a dozen lines."

    Barbaste et al., "Harness Engineering: Anatomy, Architecture, and Evolution of Coding Agents -- A Source-Code Study of Eleven Systems" · Jul 2026 · Sec 6.2 (Mini-SWE-Agent); Sec 10.10; Recommendation 18

  7. GAIA

    "Strengthened anti-loop prompt instructions" took validation 0.26 to 0.30 (best of run).

    Scale AI, "VeRO: A Harness for Agents to Optimize Agents" (ICML 2026) · Feb 2026 · paper Figure 14

  8. SimpleQA

    "Loop/entity prompt guidance" flat (0.86 to 0.85).

    Scale AI, "VeRO: A Harness for Agents to Optimize Agents" (ICML 2026) · Feb 2026 · paper Figure 15

  9. FACTS

    +5.6%; "invented adaptive termination to prevent infinite loops".

    Scale AI, "Can AI Agents Build Better AI Agents?" · tricks table (Minimal template)

  10. GAIA/FACTS/SimpleQA

    Runtime efficiency; Evidence-Based agents ran about 2x faster than Tool-Centric ones (26.2s vs 56.6s per sample for Knight).

    Scale AI, "Can AI Agents Build Better AI Agents?" · tricks table (Evidence-Based template)

Also known as

  • Infinite-loop prevention by hashing recent calls

Related

In Claude Code

This optimization is a skill in aot-kit, our open-source Claude Code plugin: /aot-kit:detect-loops-and-stalls. Its library entry holds every source. Have new evidence, or a correction? Suggest a change.

Before you go...

Build better agents, weekly

Our latest research, performance benchmarks, optimization strategies, and updates around AOT development.

One email a week, nothing else. Unsubscribe anytime.