ReAct Pattern
Combining Reasoning and Acting for autonomous tasks.
ReAct: Reason + Act
ReAct Loop: Thought → Action → Observation
The ReAct pattern interleaves reasoning (thinking about what to do) with acting (executing tools or API calls). This is the foundational pattern behind modern AI agents.
The Loop
- Thought: The model reasons about what it knows and what it needs
- Action: The model selects and calls a tool
- Observation: The tool returns a result
- Repeat until the task is complete
Why ReAct beats pure CoT
Pure Chain-of-Thought relies only on the model's parametric knowledge. ReAct allows the model to gather real-time information, verify facts, and take actions in the real world.
Code Example
The model decides which tool to call based on the user question. This is the foundation of tool-using AI agents.
python
1from openai import OpenAI
2import json
3
4client = OpenAI()
5
6tools = [
7 {
8 "type": "function",
9 "function": {
10 "name": "search_web",
11 "description": "Search the web for current information",
12 "parameters": {
13 "type": "object",
14 "properties": {
15 "query": {"type": "string", "description": "Search query"}
16 },
17 "required": ["query"]
18 }
19 }
20 }
21]
22
23# ReAct loop
24messages = [
25 {"role": "system", "content": "You are a research assistant. Use tools to find information."},
26 {"role": "user", "content": "What is the current stock price of NVIDIA?"}
27]
28
29response = client.chat.completions.create(
30 model="gpt-4o",
31 messages=messages,
32 tools=tools,
33 tool_choice="auto"
34)
35
36# The model will reason and decide to call search_web
37print(response.choices[0].message)Use Cases
Research assistants that search and synthesize information
Customer support agents that look up orders and process refunds
DevOps agents that diagnose and fix infrastructure issues
Data analysis agents that query databases and generate reports
Common Mistakes
Not implementing a maximum iteration limit — agents can loop forever
Giving agents too many tools at once, which confuses tool selection
Not providing clear tool descriptions — the model needs to know when to use each tool
Interview Insight
Relevance
High - Foundation of AI Agents