Glossary · CRITICAL · OWASP LLM10:2025 Unbounded Consumption
LangChain max_iterations: Preventing Runaway Agents
max_iterations is a parameter on LangChain's AgentExecutor that limits how many reasoning steps the agent can take. It defaults to 15. Setting it to None removes the cap, and there is no time limit unless you set max_execution_time, so a slow tool can keep one run going for a long time. Agents built outside AgentExecutor need their own bound.
AgentExecutor Configuration
Vulnerable · python
from langchain.agents import AgentExecutor
# Cap removed and no time limit: dangerous in production
executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=True,
max_iterations=None,
)Safer · python
from langchain.agents import AgentExecutor
executor = AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
verbose=True,
max_iterations=10,
max_execution_time=60, # 60 second timeout
early_stopping_method="generate"
)Questions
- What happens if max_iterations is set to None?
- AgentExecutor keeps going through its think-act-observe cycle with no step limit. Each cycle is an LLM call, so cost grows until something else stops it: max_execution_time, a context-window error or a rate limit. If you leave the default of 15, there is still no time limit unless you set max_execution_time.
- What should I set max_iterations to?
- Depends on your use case. For simple Q&A agents: 5-10. For research agents with multiple tools: 15-25. For complex multi-step workflows: 25-50. Always pair with max_execution_time as a secondary safeguard. Start low and increase based on observed needs.
- What is max_execution_time in LangChain?
- max_execution_time (in seconds) is a time-based bound complementing max_iterations. It catches cases where individual iterations are slow (e.g., tools making external API calls). Set both: max_iterations=10, max_execution_time=60.
- What is early_stopping_method in LangChain?
- When max_iterations is reached, early_stopping_method controls behavior: "force" immediately stops and returns, "generate" makes one final LLM call to generate a summary response. Use "generate" for user-facing agents.
How Inkog detects it
Inkog identifies AgentExecutor instantiations that remove the iteration cap or set no max_execution_time. It also detects when these values are set unreasonably high (e.g., max_iterations=1000) which provides no practical protection.