The ReAct Pattern: How AI Agents Reason and Act in Alternating Steps
Understand the ReAct (Reasoning + Acting) pattern that powers most modern AI agents, with a step-by-step breakdown of observation loops and a working Python implementation.
What Is the ReAct Pattern?
ReAct stands for Reasoning + Acting. Introduced in a 2022 research paper by Yao et al., it is the foundational pattern behind nearly every modern AI agent. The core idea is simple: instead of having an LLM generate a final answer in one shot, you let it alternate between thinking (reasoning) and doing (acting), with observations from the environment feeding back into the next reasoning step.
This alternation is what gives agents their power. A single LLM call might hallucinate an answer. A ReAct loop grounds the LLM's reasoning in real data from real tools.
The ReAct Cycle
Each iteration of a ReAct loop has three phases:
- Thought — The LLM reasons about the current state and decides what to do next
- Action — The LLM calls a tool or function
- Observation — The tool returns a result, which feeds into the next thought
User: "What is the current stock price of AAPL and is it above its 50-day average?"
Thought 1: I need to look up the current AAPL stock price first.
Action 1: get_stock_price(symbol="AAPL")
Observation 1: {"price": 187.42, "currency": "USD", "timestamp": "2026-03-17T14:30:00Z"}
Thought 2: The current price is $187.42. Now I need the 50-day moving average.
Action 2: get_moving_average(symbol="AAPL", days=50)
Observation 2: {"average": 182.15, "period_days": 50}
Thought 3: AAPL is at $187.42, above its 50-day average of $182.15. I can answer now.
Action 3: respond("AAPL is currently at $187.42, which is above its 50-day
moving average of $182.15 by approximately $5.27 or 2.9%.")
Notice how each thought is informed by the previous observation. The agent does not guess — it retrieves real data and then reasons about it.
Why ReAct Beats Chain-of-Thought Alone
Chain-of-Thought (CoT) prompting asks the LLM to think step by step, but all reasoning happens internally without access to external information. This means CoT is limited by what the model already knows (its training data), and it can confidently produce wrong answers.
ReAct fixes this by interleaving actions between thoughts. The LLM can verify its assumptions, fetch current data, and correct course based on real observations.
See AI Voice Agents Handle Real Calls
Book a free demo or calculate how much you can save with AI voice automation.
| Approach | Grounded in real data? | Multi-step? | Can use tools? |
|---|---|---|---|
| Direct prompting | No | No | No |
| Chain-of-Thought | No | Yes (internal) | No |
| ReAct | Yes | Yes | Yes |
Implementing ReAct in Python
Here is a working ReAct agent using the OpenAI API with function calling:
import json
import openai
client = openai.OpenAI()
# Define the tools the agent can use
tools = [
{
"type": "function",
"function": {
"name": "search_web",
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Evaluate a mathematical expression",
"parameters": {
"type": "object",
"properties": {
"expression": {"type": "string", "description": "Math expression"}
},
"required": ["expression"],
},
},
},
]
def execute_tool(name: str, arguments: dict) -> str:
"""Execute a tool and return the result as a string."""
if name == "search_web":
# In production, call a real search API
return json.dumps({"results": [f"Result for: {arguments['query']}"]})
elif name == "calculate":
try:
result = eval(arguments["expression"]) # Use a safe evaluator in production
return json.dumps({"result": result})
except Exception as e:
return json.dumps({"error": str(e)})
return json.dumps({"error": f"Unknown tool: {name}"})
def react_agent(user_input: str, max_iterations: int = 10) -> str:
messages = [
{"role": "system", "content": "You are a helpful agent. Use tools to find "
"accurate information before answering. Think step by step."},
{"role": "user", "content": user_input},
]
for i in range(max_iterations):
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
choice = response.choices[0]
messages.append(choice.message)
# If no tool calls, the agent is done reasoning
if not choice.message.tool_calls:
return choice.message.content
# Execute each tool call (Action) and add results (Observation)
for tool_call in choice.message.tool_calls:
args = json.loads(tool_call.function.arguments)
result = execute_tool(tool_call.function.name, args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result, # This is the Observation
})
return "Agent reached maximum iterations without a final answer."
# Run the agent
answer = react_agent("What is 15% of the population of France?")
print(answer)
The key insight is on line where we check if not choice.message.tool_calls. When the LLM decides it has enough information, it responds with text instead of a tool call — that is how ReAct agents naturally terminate.
When ReAct Falls Short
ReAct is powerful but not perfect. It struggles with tasks that require long-horizon planning (more than 8-10 steps), because LLMs can lose track of their overall goal as context grows. For those cases, you layer planning patterns on top of ReAct — decompose the task into subtasks first, then use ReAct for each subtask.
ReAct also depends entirely on tool quality. If your tools return ambiguous or inconsistent results, the agent's reasoning degrades. Investing in clear tool descriptions and reliable tool implementations is just as important as prompt engineering.
FAQ
What is the difference between ReAct and function calling?
Function calling is the mechanism (how the LLM requests a tool execution). ReAct is the pattern (alternating reasoning and acting in a loop). ReAct uses function calling as its action mechanism, but adds the structured loop of thought-action-observation that continues until the task is complete.
How many iterations should a ReAct loop allow?
For most tasks, 5 to 15 iterations is sufficient. Set a maximum to prevent runaway loops and cost explosions. If your agent consistently hits the limit, the task is likely too complex for a single ReAct loop — consider decomposing it into subtasks instead.
Can ReAct work with open-source models?
Yes. Any LLM that supports function calling or structured output can run a ReAct loop. Models like Llama 3, Mistral, and Qwen all support tool use. The quality of reasoning depends on model capability, but the pattern itself is model-agnostic.
#ReAct #AIAgents #Reasoning #ToolUse #Python #AgenticAI #LearnAI #AIEngineering
CallSphere Team
Expert insights on AI voice agents and customer communication automation.
Try CallSphere AI Voice Agents
See how AI voice agents work for your industry. Live demo available -- no signup required.