RuntimeError
langgraph.agent.LangGraphAgentRuntimeError
Stack trace
Traceback (most recent call last):
File "main.py", line 42, in <module>
agent.run()
File "/usr/local/lib/python3.9/site-packages/langgraph/agent.py", line 128, in run
while True:
RuntimeError: LangGraph agent loop has no terminal condition and ran indefinitely Why it happens
LangGraph agents require a defined terminal condition or stopping criteria to end their execution loop. Without this, the agent continues running infinitely, causing a RuntimeError. This often happens when the developer forgets to specify a max iteration count or a success/failure condition in the agent configuration.
Detection
Monitor agent execution time and iteration counts; add logging inside the agent loop to detect if the loop exceeds expected iterations or time thresholds before crashing.
Causes & fixes
No max iteration or step limit set in the LangGraph agent configuration
Configure the agent with a max_steps or max_iterations parameter to enforce a hard stop on the loop.
Missing or improperly defined terminal condition in the agent's logic
Define a clear terminal condition function or callback that returns True when the agent should stop running.
Agent's output or state never reaches a condition that triggers loop termination
Ensure the prompt, tools, or environment produce outputs that can satisfy the terminal condition, or add fallback conditions.
Code: broken vs fixed
from langgraph import LangGraphAgent
agent = LangGraphAgent(model="gpt-4o-mini")
# Missing max_steps or terminal condition causes infinite loop
agent.run() # This line triggers RuntimeError due to no terminal condition import os
from langgraph import LangGraphAgent
os.environ["LANGGRAPH_API_KEY"] = os.environ["OPENAI_API_KEY"]
agent = LangGraphAgent(model="gpt-4o-mini", max_steps=10) # Added max_steps to prevent infinite loop
agent.run()
print("Agent run completed successfully.") Workaround
Wrap the agent.run() call in a try/except block catching RuntimeError, then forcibly stop the loop after a timeout or iteration count in your own code.
Prevention
Always define explicit terminal conditions or max iteration limits in LangGraph agents to guarantee loop termination and avoid infinite execution.