High severity intermediate · Fix: 5-10 min

MaxIterationsExceeded

llama_index.agent.max_iterations.MaxIterationsExceeded

What this error means
The LlamaIndex agent exceeded its configured maximum number of iterations, indicating a possible infinite loop or failure to reach a stopping condition.

Stack trace

traceback
llama_index.agent.max_iterations.MaxIterationsExceeded: Agent exceeded max iterations of 5
  File "/app/llama_index/agent/agent.py", line 210, in run
    raise MaxIterationsExceeded("Agent exceeded max iterations of 5")
QUICK FIX
Increase the agent's max_iterations parameter or improve the prompt to ensure the agent reaches a stopping condition.

Why it happens

LlamaIndex agents run iterative reasoning loops to process queries. If the agent's stopping criteria are never met or the agent keeps generating new tasks indefinitely, it hits the max iterations limit to prevent infinite loops. This error signals that the agent logic or prompt is not converging.

Detection

Monitor agent run loops and catch MaxIterationsExceeded exceptions to log the iteration count and intermediate outputs, helping identify why the agent failed to stop.

Causes & fixes

1

Agent prompt or logic causes repeated cycles without reaching a stopping condition

✓ Fix

Refine the prompt or agent logic to include clear stopping criteria or maximum steps to ensure convergence.

2

Max iterations parameter is set too low for the complexity of the task

✓ Fix

Increase the max_iterations parameter in the agent configuration to allow more reasoning steps.

3

Agent is stuck due to ambiguous or conflicting instructions in the prompt

✓ Fix

Simplify and clarify the prompt instructions to avoid ambiguous loops and ensure the agent can decide to stop.

Code: broken vs fixed

Broken - triggers the error
python
from llama_index import LLMPredictor, ServiceContext, GPTVectorStoreIndex, Agent

agent = Agent(max_iterations=5)  # Too low max iterations
response = agent.run("Explain the process in detail")  # This line raises MaxIterationsExceeded
print(response)
Fixed - works correctly
python
import os
from llama_index import LLMPredictor, ServiceContext, GPTVectorStoreIndex, Agent

os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')  # Use env var for API key

agent = Agent(max_iterations=10)  # Increased max iterations to allow more steps
response = agent.run("Explain the process in detail with clear stopping criteria")  # Fixed prompt and config
print(response)  # Should complete without error
Increased max_iterations to allow more reasoning steps and improved prompt clarity to help the agent reach a stopping condition, preventing the max iterations error.

Workaround

Catch MaxIterationsExceeded in a try/except block, log the partial outputs, and optionally retry with a higher max_iterations or a simplified prompt.

Prevention

Design agent prompts with explicit stopping criteria and set a reasonable max_iterations limit based on task complexity to avoid infinite loops.

Python 3.9+ · llama_index >=0.5.0 · tested on 0.6.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.