High severity intermediate · Fix: 5-15 min

MaxIterationsReached

langchain.agents.agent.MaxIterationsReached

What this error means
LangChain's agent exceeded the configured maximum number of iterations, causing the agent loop to stop to prevent infinite execution.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    agent.run(input)
  File "/usr/local/lib/python3.9/site-packages/langchain/agents/agent.py", line 210, in run
    raise MaxIterationsReached(f"Agent stopped after {self.max_iterations} iterations.")
langchain.agents.agent.MaxIterationsReached: Agent stopped after 15 iterations.
QUICK FIX
Increase the agent's max_iterations parameter or improve prompt clarity to ensure the agent finishes within the iteration limit.

Why it happens

LangChain agents run in a loop to iteratively call tools or LLMs until a stopping condition is met. If the agent does not reach a conclusion within the configured max_iterations, it raises MaxIterationsReached to avoid infinite loops caused by ambiguous prompts or logic errors.

Detection

Monitor agent execution time and iteration count; catch MaxIterationsReached exceptions to log the input and intermediate steps causing the loop to exceed limits before failure.

Causes & fixes

1

The agent prompt or logic causes it to cycle without reaching a stopping condition.

✓ Fix

Refine the prompt or tool outputs to ensure the agent can reach a final answer within fewer iterations.

2

max_iterations parameter is set too low for the complexity of the task.

✓ Fix

Increase the max_iterations parameter when creating the agent to allow more steps before stopping.

3

Agent tools or callbacks produce outputs that confuse the agent, causing repeated loops.

✓ Fix

Validate and sanitize tool outputs and intermediate agent steps to prevent loops caused by unexpected responses.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
tools = [Tool(name="Search", func=lambda x: "irrelevant output")]
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", max_iterations=3)

# This will raise MaxIterationsReached if the agent loops too long
agent.run("Find the capital of France")
Fixed - works correctly
python
import os
from langchain.agents import initialize_agent, Tool
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(temperature=0, model="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
tools = [Tool(name="Search", func=lambda x: "Paris is the capital of France")]

# Increased max_iterations to 10 to allow more steps
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", max_iterations=10)

result = agent.run("Find the capital of France")
print(result)  # Should complete without MaxIterationsReached
Increased max_iterations to allow more agent steps and used a clearer tool output so the agent can finish without looping infinitely.

Workaround

Catch MaxIterationsReached exception around agent.run(), log intermediate steps, and retry with a higher max_iterations or simplified prompt.

Prevention

Design agent prompts and tools to produce clear stopping signals and set max_iterations sufficiently high for task complexity to avoid infinite loops.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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