Agent Foundry
All Problems

#71. Infinite Loop Detector

HardError RecoveryOrchestration

The Problem

Your data analysis agent has a graph that routes between an "analyze" step and a "fetch more data" step. The idea is that the agent keeps refining its analysis until it has a complete answer. In practice, the data is never "sufficient" and the agent loops forever — analyze → fetch → analyze → fetch — burning tokens and never returning. There is no loop detection or iteration limit, so the agent runs until it's killed. Your job is to detect when the agent is stuck in a repetitive cycle and break out after a configurable number of iterations with a meaningful message.

Examples

Example 1

User input: Analyze the Q4 sales data

Current (bad) output: (Agent loops infinitely: analyze → fetch → analyze → fetch → ... never returns)

Expected (good) output: After 5 iterations: "I've attempted to analyze the sales data 5 times but couldn't reach a complete result. Here's what I found so far: [partial analysis]. The data source may need to be checked."

Example 2

User input: Summarize customer feedback trends

Current (bad) output: (Infinite loop — the agent never completes.)

Expected (good) output: After hitting the iteration limit: "I've made 5 analysis attempts without converging on a final answer. Partial findings: [summary]. Consider providing more specific data or constraints."

Example 3

User input: What is 2 + 2?

Current (bad) output: (Simple queries may also get caught in the loop.)

Expected (good) output: The agent answers 4 on the first pass — no loop triggered, no iteration cap needed.

Your Task

Add infinite loop detection so the agent:

  • Tracks the actions it has taken and detects repetitive cycles.
  • Breaks out of the loop after a configurable maximum number of iterations (e.g. 5).
  • Returns a meaningful message describing what was attempted and why it stopped.
  • Does not interfere with queries that complete normally without looping.

Evaluation

Submissions are checked for the following:

  • Infinite loop is detected: The agent recognizes when it is repeating the same actions.
  • Loop is broken after N iterations: The agent exits after a configurable iteration cap.
  • Exit message is meaningful: The agent reports what it tried and why it stopped.
  • Non-looping flows still work: Normal queries that resolve without looping are unaffected.

Constraints

  • The agent must detect when it is repeating the same action and break out of the loop
  • The maximum iteration limit must be configurable
  • When the loop is broken, the agent must return a meaningful message about what it tried
  • The detection must work for both exact repeats and semantically similar repeated actions
Starter Code
from langgraph.graph import StateGraph, START, END
from typing import TypedDict, Annotated
import operator

class State(TypedDict):
    query: str
    result: str
    actions: Annotated[list[str], operator.add]

def analyze(state: State) -> State:
    return {"actions": ["analyze"], "result": "Need more data"}

def fetch_data(state: State) -> State:
    return {"actions": ["fetch_data"], "result": "Data insufficient"}

def should_continue(state: State) -> str:
    # BUG: Always routes back to analyze — creates an infinite loop
    if state["result"] == "Done":
        return "end"
    return "analyze"

graph = StateGraph(State)
graph.add_node("analyze", analyze)
graph.add_node("fetch_data", fetch_data)
graph.add_edge(START, "analyze")
graph.add_edge("analyze", "fetch_data")
graph.add_conditional_edges("fetch_data", should_continue, {"analyze": "analyze", "end": END})

app = graph.compile()

# Test: This will loop forever
result = app.invoke({"query": "Analyze sales data", "result": "", "actions": []})
print(result["result"])
Open in Google Colab
Evaluation Criteria0/4