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:
- Rule 1: Any single
(tool, input)pair appears 3 or more times in the logs. - Rule 2: Any consecutive sequence of actions of length $L \ge 2$ repeats at least twice consecutively (e.g.
A -> B -> A -> Bis 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