Glossary · CRITICAL · OWASP LLM10:2025 Unbounded Consumption

Infinite Loop in AI Agents

An infinite loop in an AI agent occurs when the agent enters an unbounded cycle of LLM calls, tool invocations, or state transitions that never terminates. Common causes include hand-written agent loops whose exit depends on model output, AgentExecutor with max_iterations=None and no time limit, LangGraph cycles that rely only on the recursion limit, and delegation loops between agents.

AgentExecutor Without Bounds

Vulnerable · python

# Default cap of 15 removed, and no time limit
agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent,
    tools=tools,
    verbose=True,
    max_iterations=None,
)
result = agent_executor.run("Analyze this data...")

Safer · python

# Bounded execution with safety guards
agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent,
    tools=tools,
    verbose=True,
    max_iterations=10,
    max_execution_time=30,  # seconds
    early_stopping_method="generate"
)

Questions

What causes infinite loops in AI agents?
Common causes include: custom while-loops where the model decides when to stop, LangChain AgentExecutor with max_iterations=None (the default cap is 15) and no max_execution_time, LangGraph cycles with no explicit exit condition that only stop at the recursion limit, and CrewAI delegation loops between agents.
How do you detect infinite loops in AI agents before deployment?
Static analysis tools like Inkog trace the control flow graph of your agent code and identify loops without deterministic termination guards. This includes missing max_iterations parameters, cycles in graph-based agents, and recursive tool calling patterns.
What is the cost impact of an AI agent infinite loop?
Each iteration is an LLM call, typically $0.01-0.10 with a growing context, so one stuck loop costs dollars an hour. The bill gets large when it happens in many sessions at once, or in a scheduled job nobody watches, until a rate limit or budget alert stops it.
How do you fix an infinite loop in LangChain AgentExecutor?
Set max_iterations and max_execution_time parameters: AgentExecutor(agent=agent, tools=tools, max_iterations=10, max_execution_time=30). This ensures the agent stops after a bounded number of steps or seconds.

How Inkog detects it

Inkog traces the control flow graph of your agent code and identifies LoopNodes without deterministic termination guards. It checks for missing max_iterations on AgentExecutor, cycles in LangGraph StateGraphs, and recursive agent delegation patterns.

Related