AI Engineer Problems › Multi-Agent Systems

Agent Tool-Call Loop Detector

Medium · Coding · Multi-Agent Systems · Asked at LangChain, CrewAI

Agent Tool-Call Loop Detector

Unregulated agents often fall into infinite execution loops calling the same tools repetitively.

Write a function detect_agent_loops(history) that processes a list of tool call dictionary records: {"tool": str, "input": str, "timestamp": int}.

Your function must identify an infinite loop if:

  1. Rule 1: Any single (tool, input) pair appears 3 or more times in the logs.
  2. Rule 2: Any consecutive sequence of actions of length $L \ge 2$ repeats at least twice consecutively (e.g. A -> B -> A -> B is a loop of length 2 repeating twice).

Return True if a loop is detected, otherwise False.

Example

history = [
    {"tool": "web_search", "input": "python", "timestamp": 1},
    {"tool": "web_search", "input": "python", "timestamp": 2},
    {"tool": "web_search", "input": "python", "timestamp": 3}
]
detect_agent_loops(history)
# Returns: True

Solve it in the browser on GenAIDojo