Fix LangChain "Agent stopped due to iteration limit" Error

intermediate🧠 AI Tools2026-05-17| Python 3.8+, LangChain 0.1.x / 0.2.x / 0.3.x, any OS

Error Message

Agent stopped due to iteration limit
#langchain#agent#iteration-limit

What happened

You ran a LangChain agent and it stopped mid-task, returning something like:

Agent stopped due to iteration limit or time limit.

No final answer. Just a partial result β€” or nothing at all. LangChain caps agents at 15 iterations by default. It's a guardrail, not a bug. Without it, a stuck agent could run indefinitely and rack up hundreds of API calls.

The cap triggers for a few reasons:

  • The task genuinely needs more tool calls than the default allows.
  • The agent is stuck in a loop β€” calling the same tool repeatedly without making progress.
  • The LLM keeps producing intermediate steps instead of committing to a final answer.
  • A tool keeps returning results the agent doesn't know how to handle.

Quick diagnosis

Before touching any config, turn on verbose mode to see what the agent is actually doing on each iteration:

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,   #  str:
    return f"Parsing failed: {str(error)[:100]}. Try a different approach."

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    max_iterations=25,
    handle_parsing_errors=handle_error
)

Fix 4: Fix the actual loop β€” improve your prompt

Raising the limit only delays the problem if the agent loops on a specific tool. Nine times out of ten, the root cause is a system prompt that doesn't tell the agent when to stop. Add explicit stopping rules:

from langchain.agents import create_react_agent
from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("""
You are a helpful assistant. Answer the question using the tools available.

IMPORTANT:
- If you've used a tool and have enough information to answer, stop and give the Final Answer immediately.
- Do NOT call the same tool twice with the same input.
- If a tool returns an error twice, stop and explain what you found so far.

{tools}

Question: {input}
{agent_scratchpad}
""")

agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)

Fix 5: LangGraph agents (0.3.x+)

Migrated to LangGraph? The iteration limit works differently there. Instead of max_iterations, you set recursion_limit in the invoke config:

from langgraph.prebuilt import create_react_agent

agent = create_react_agent(model=llm, tools=tools)

# Pass recursion_limit in config when invoking
result = agent.invoke(
    {"messages": [("user", "your question here")]},
    config={"recursion_limit": 50}
)

LangGraph also lets you stream graph state to debug loops step by step:

for step in agent.stream(
    {"messages": [("user", "your question here")]},
    config={"recursion_limit": 50}
):
    print(step)
    print("---")

Verify the fix worked

Run the agent with your fix applied and confirm it reaches a real final answer:

result = agent_executor.invoke({"input": "your test question"})

# Check you got a real output, not an empty/error result
print(result["output"])
assert "Agent stopped due to iteration limit" not in str(result)
assert len(result["output"]) > 0

With verbose=True, you should see Final Answer: appear before the iteration cap. Still looping? Re-examine the tool outputs and tighten the prompt instructions.

Summary of fixes

  • Legitimate long task: raise max_iterations, add max_execution_time.
  • Bad output at cutoff: set early_stopping_method="generate".
  • Parse errors causing retries: enable handle_parsing_errors=True.
  • Agent looping on same tool: fix the system prompt with explicit stopping rules.
  • LangGraph agents: use recursion_limit in the invoke config.

Related Error Notes