Client-Side Execution: Trace cycle detection runs locally in your browser JavaScript runtime without submitting agent logs to backend servers.

1. Purpose & The Autonomous Agent Loop Problem

Autonomous AI agents orchestrate tool executions using iterative patterns such as ReAct (Reasoning + Acting) or plan-and-solve execution loops. While agents can accomplish complex multi-step workflows, they frequently encounter unbounded execution loops:

  • Ambiguous Error Deadlocks: A tool returns an uninformative error (e.g., "Error: 500 Internal Server Error" or "File lock busy"). Lacking fallback heuristics, the LLM reissues the exact same tool call with identical arguments, hoping for a different outcome.
  • Oscillating Tool Trajectories: The agent gets trapped in a 2-step or 3-step cycle (e.g., Tool A checks a condition, determines Tool B is needed, but Tool B fails to satisfy Tool A's preconditions, causing Tool A to be called again: $A \to B \to A \to B$).
  • Runaway Token Consumption: Because agent conversation histories append each thought, action, and observation turn sequentially, an infinite loop rapidly inflates context size, consuming millions of tokens and exhausting API budgets within minutes.

2. How to Use the Detector

  1. Paste Execution Trace: Insert agent logs from LangChain, LangGraph, AutoGen, CrewAI, or custom function-calling runtimes.
  2. Or Select a Preset: Click Sample: Consecutive Loop (repeated service checks) or Sample: Oscillating Cycle (conflicting git status commands).
  3. Analyze: Click Detect Execution Loops. The algorithm extracts action signatures, maps tool frequencies, tracks consecutive identical invocations, and detects repeating $N$-step cyclic sub-sequences.
  4. Export Findings: Review severity alerts and click Copy JSON Report to include loop telemetry in your agent evaluation datasets.

3. Reproducible Cases & Walkthrough

Case A: 4x Consecutive Failed Tool Invocation

Trace Snippet:
Step 1: Action: execute_bash(cmd="systemctl status postgresql") -> failed
Step 2: Action: execute_bash(cmd="systemctl status postgresql") -> failed
Step 3: Action: execute_bash(cmd="systemctl status postgresql") -> failed
Step 4: Action: execute_bash(cmd="systemctl status postgresql") -> failed

Diagnostic Outcome: Flags Critical Loop: Consecutive identical action triggered 4 times without parameter mutation.

4. Production Architecture: Circuit Breakers for Agents

To safeguard production agent runtimes from infinite loops, implement these architectural controls:

  • Hard Turn Ceilings: Enforce strict upper bounds on tool calls per objective (e.g. maximum 15 tool calls per user task). Terminate with an escalation message if the ceiling is reached.
  • Action State Hashing: Compute an MD5/SHA256 hash of (tool_name, tool_arguments) before execution. Maintain a sliding-window set of recent action hashes. If the same hash appears 3 times in a row, trigger an automated circuit breaker.
  • Error Mutating Prompts: When a tool fails, dynamically inject guidance into the scratchpad: "Notice: The previous tool call failed with the above error. You must NOT retry the identical command without changing arguments or checking preconditions first."
  • Exponential Backoff with Jitter: For transient network or lock errors, force the agent runtime to wait with exponential backoff rather than immediately retrying on the next token generation pass.

5. Scope, Assumptions & Limitations

Diagnostic Scope Notice: Trace Pattern Heuristics

Heuristic loop detection inspects historical logs, not real-time process state:

  • Legitimate Polling Loops: Agents tasked with polling an asynchronous job status (e.g. waiting for a video render or database backup) may legitimately repeat check_status(job_id) multiple times. Add a polling counter or custom interval metadata to distinguish intentional polling from runaway loops.
  • Non-Deterministic Branches: Agents that change only minor sub-parameters in complex JSON schemas may not trigger simple string-equality checks. In production, serialize and sort JSON keys before computing action identity.

6. Related Tools & Research