ReAct Pattern Deep Dive (Reasoning + Acting)
The Foundation of Every AI Agent - Reasoning + Acting
Master the ReAct (Reasoning + Acting) pattern - the core loop behind LangChain agents, AutoGen, and every modern AI agent framework.
What is ReAct?
Thought → Action → Observation - The Agent Loop
The Core Idea:
ReAct (Yao et al., 2022) combines reasoning (chain-of-thought) with acting (tool use) in an interleaved loop. Instead of just thinking OR just acting, the agent does both:
- Thought: The model reasons about what to do next
- Action: The model calls a tool (search, calculator, API, etc.)
- Observation: The model sees the tool's result
- Repeat: Based on the observation, think again, act again...
Why Not Just Chain-of-Thought?
Pure chain-of-thought (CoT) only reasons - it can't access real data. It hallucinates facts. ReAct fixes this by letting the model ground its reasoning in real observations from tools.
Pure acting (no reasoning) makes random tool calls without strategy. ReAct adds planning and reflection between actions.
ReAct in Practice - Step by Step
Example: "What is the population of the capital of France?"
Thought 1: I need to find the capital of France first.
Action 1: Search["capital of France"]
Observation 1: The capital of France is Paris.
Thought 2: Now I need the population of Paris.
Action 2: Search["population of Paris 2024"]
Observation 2: Paris has ~2.1 million (city) or ~12.3 million (metro).
Thought 3: I have the answer. The user likely wants the city population.
Action 3: Finish["The capital of France is Paris, with a population of approximately 2.1 million (city proper)."]
The ReAct Prompt Template:
Answer the following question using these tools:
- Search[query]: Search for information
- Calculator[expression]: Calculate math
- Finish[answer]: Return final answer
Use this format:
Thought: [your reasoning]
Action: [tool_name][input]
Observation: [tool result]
... (repeat as needed)
Thought: I have enough info
Action: Finish[final answer]
ReAct vs Other Agent Patterns
Comparison:
| Pattern | Approach | Best For |
|---|---|---|
| ReAct | Think → Act → Observe (loop) | General-purpose agent tasks |
| Plan-and-Execute | Plan all steps first, then execute | Complex multi-step tasks |
| Tree of Thoughts | Explore multiple reasoning paths | Creative/puzzle tasks |
| Reflexion | Act → Reflect → Improve | Self-improving agents |
Where ReAct Lives in Frameworks:
- LangChain:
create_react_agent()- the default agent type - LangGraph: ReAct is a node pattern in the graph
- AutoGen: ConversableAgent uses ReAct-style reasoning
- OpenAI: Function calling is essentially structured ReAct
- Claude: Tool use follows the same Thought → Tool → Result pattern
Implementing ReAct from Scratch
Python Implementation (No Framework):
import openai
def react_agent(question, tools, max_steps=5):
messages = [{
"role": "system",
"content": """You are a ReAct agent. For each step:
1. Thought: reason about what to do
2. Action: call a tool OR Finish with answer
Always think before acting."""
}, {"role": "user", "content": question}]
for step in range(max_steps):
response = openai.chat.completions.create(
model="gpt-4",
messages=messages,
tools=tools,
)
msg = response.choices[0].message
messages.append(msg)
if msg.tool_calls:
for call in msg.tool_calls:
result = execute_tool(call)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": result
})
else:
return msg.content # Final answer
return "Max steps reached"
Interview Questions
- Q: What is the ReAct pattern?
A: A prompting paradigm that interleaves reasoning (Thought) with acting (Action → Observation) in a loop, enabling LLMs to solve complex tasks by grounding reasoning in real-world tool outputs. - Q: Why is ReAct better than pure chain-of-thought?
A: Pure CoT can only reason from training data and hallucinate facts. ReAct grounds reasoning in real observations from tools, reducing hallucination and enabling access to live data. - Q: How does ReAct relate to function calling?
A: Function calling (OpenAI/Anthropic) is essentially a structured implementation of ReAct - the model reasons, decides to call a function, observes the result, and continues. - Q: When would you use Plan-and-Execute over ReAct?
A: When the task has many interdependent steps that benefit from upfront planning. ReAct is better for tasks where the next step depends on the result of the current step.
Frequently Asked Questions
What is ReAct Pattern Deep Dive?
Master the ReAct (Reasoning + Acting) pattern - the core loop behind LangChain agents, AutoGen, and every modern AI agent framework.
How does ReAct Pattern Deep Dive work?
Thought → Action → Observation - The Agent Loop The Core Idea: ReAct (Yao et al., 2022) combines reasoning (chain-of-thought) with acting (tool use) in an interleaved loop. Instead of just thinking OR just acting, the agent does both: Thought: The model reasons about what to do next Action: The model calls a tool…
Related topics
Practice this on DevInterviewMaster
Read the full ReAct Pattern Deep Dive (Reasoning + Acting) breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.