ReAct Agent Stopping Conditions
What you'll learn: How to define and implement stopping criteria that reliably terminate a ReAct agent's loop without running forever or stopping prematurely.
The Core Problem
A ReAct agent cycles through reasoning and acting steps, but without proper stopping conditions, it could loop indefinitely, waste resources, or fail to recognize when it's done. You need clear, enforceable rules that tell the agent: "Stop here."
The Three Essential Stopping Criteria
1. Max Iterations
Set a hard limit on how many ReAct cycles the agent can perform (e.g., 10 loops). This prevents infinite loops when the agent gets stuck or confused. Think of it as a safety net—like a maximum number of turns in a board game.
2. Final Answer Signals
The agent explicitly indicates completion by outputting a "Final Answer" or similar marker (covered in the previous lesson). When you detect this signal during output parsing, stop immediately—the agent has solved the task.
3. Error Thresholds
Stop if the agent encounters too many consecutive errors (e.g., 3 tool failures in a row) or hits critical failures like timeouts or invalid tool calls. This prevents the agent from thrashing uselessly when something is broken.
Implementation Strategy
In your ReAct loop, check after each iteration:
- Has the iteration counter reached
max_iterations? - Did the parsed output contain a Final Answer?
- Has the error count exceeded your threshold?
If any condition is true, exit the loop gracefully, log the reason, and return whatever results you have.
Key Takeaway: ReAct agents need layered stopping conditions—max iterations for safety, final answer detection for success, and error thresholds for graceful failure—to ensure they always terminate appropriately without wasting resources or missing solutions.