Module 3 Lesson 1: The ReAct Pattern
Mastering the fundamental agent loop. How to prompt for Thought, Action, and Observation.
The ReAct Pattern: Reasoning and Acting
We have briefly mentioned ReAct in Module 1, but now we are going to master it. ReAct is the most common pattern for simple, single-agent systems. It stands for Reasoning + Acting.
The core idea is that the agent shouldn't just act in a vacuum; it should describe Why it is taking an action before it takes it.
1. The 4 Stages of a ReAct Step
- Question: The user's goal.
- Thought: The model writes down what it is thinking.
- Action: The model chooses a tool to call.
- Observation: The result of that tool call is fed back to the model.
2. Why "Thought" is Mandatory
If you remove the "Thought" stage and force the model to jump straight to an "Action," it will often pick the wrong tool. Writing the "Thought" out loud gives the model a Logical Buffer. It's like "Thinking before you speak."
3. Visualizing ReAct in Action
graph TD
Q[User Question] --> T1[Thought: I need X]
T1 --> A1[Action: Call Tool X]
A1 --> O1[Observation: Result X]
O1 --> T2[Thought: Now I need Y]
T2 --> A2[Action: Call Tool Y]
A2 --> O2[Observation: Result Y]
O2 --> F[Final Answer]
4. The ReAct Prompt Structure
Here is a production-grade ReAct prompt. Note the explicit instructions for formatting.
Answer the following questions as best you can. You have access to the following tools:
Search: [Description of search tool]
Calculator: [Description of calculator tool]
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Search, Calculator]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: [User query here]
5. Failure Modes of ReAct
While powerful, ReAct has limits:
- Looping: The agent might keep trying the same failing tool.
- Complexity: It is hard for a single ReAct loop to solve a task with 20+ steps.
- Reliability: Sometimes the model "forgets" the format and stops outputting the
Action:keyword.
For these complex scenarios, we move to the next pattern: Planner–Executor.
Key Takeaways
- ReAct combines reasoning and action into a single iterative loop.
- The Thought stage reduces logic errors by roughly 40%.
- The system is Dynamic: it doesn't have a plan; it just knows the next step.
- Prompt structure must be extremely rigid to ensure the parser can extract the
Action.