Chapter 6
15 min read
Section 34 of 175

Understanding ReAct

The ReAct Pattern

Introduction

ReAct (Reasoning + Acting) is one of the most important patterns in AI agents. It combines the reasoning capabilities of LLMs with the ability to take actions in the world. This pattern underlies virtually every successful agentic system.

The ReAct Insight: Don't just think, then act. Think AND act together. Let each action inform the next thought, and each thought guide the next action.

What is ReAct

ReAct is a prompting paradigm that interleaves reasoning traces with action execution. Instead of generating a complete plan first, the agent alternates between:

  1. Thought: Reasoning about the current situation
  2. Action: Taking an action based on the thought
  3. Observation: Observing the result of the action
📝react_flow.txt
1Task: "Find the population of France and compare it to Germany"
2
3Thought 1: I need to find the population of France first.
4Action 1: search("population of France 2024")
5Observation 1: France has a population of approximately 68 million.
6
7Thought 2: Now I need to find the population of Germany.
8Action 2: search("population of Germany 2024")
9Observation 2: Germany has a population of approximately 84 million.
10
11Thought 3: I have both populations. Germany (84M) > France (68M).
12Action 3: finish("Germany has about 84 million people, which is larger
13                  than France's approximately 68 million.")
14Observation 3: Task completed.

Origins

ReAct was introduced in the 2022 paper "ReAct: Synergizing Reasoning and Acting in Language Models" by Yao et al. The key finding was that interleaving reasoning and acting outperforms either approach alone.

ApproachDescriptionLimitation
Reason-onlyThink through the problem completelyCan't verify assumptions or get new info
Act-onlyTake actions without explicit reasoningLacks strategic planning, error-prone
ReActInterleave reasoning and actingBest of both worlds

The Core Insight

The fundamental insight of ReAct is that reasoning and acting are synergistic:

📝synergy.txt
1REASONING HELPS ACTING:
2- Thought guides which action to take next
3- Reasoning catches potential mistakes before acting
4- Complex multi-step plans become manageable
5
6ACTING HELPS REASONING:
7- Observations ground reasoning in reality
8- Failed actions reveal incorrect assumptions
9- New information updates the reasoning context
10
11THE SYNERGY:
12Each thought improves action quality.
13Each observation improves thought quality.
14The agent becomes more grounded and effective.

Contrast with Pure Reasoning

📝pure_reasoning.txt
1Pure Reasoning (Chain-of-Thought):
2
3Task: "What is the latest news about OpenAI?"
4
5Let me think about this...
6OpenAI is a leading AI company.
7They recently released GPT-4.
8There might be news about Sam Altman.
9I believe they have partnerships with Microsoft.
10
11Answer: OpenAI has been making advancements in AI...
12
13Problem: This is based on training data, not current information!
14         The answer could be months or years out of date.
15
16---
17
18ReAct Approach:
19
20Task: "What is the latest news about OpenAI?"
21
22Thought: I need to search for current OpenAI news.
23Action: search("OpenAI news today")
24Observation: [Current news articles...]
25
26Thought: I can now provide accurate, current information.
27Answer: Based on today's news, OpenAI has announced...
28
29Benefit: Grounded in real, current information.

Benefits of ReAct

1. Grounded Reasoning

🐍grounded_reasoning.py
1# Without grounding - reasoning can go astray
2def ungrounded_analysis(problem: str) -> str:
3    """Pure reasoning without verification."""
4    response = llm.generate(f"""
5Think through this problem:
6{problem}
7
8Provide your complete analysis and answer.
9""")
10    return response  # May contain hallucinations
11
12# With grounding - each step verified
13def grounded_analysis(problem: str) -> str:
14    """ReAct-style grounded reasoning."""
15    context = problem
16    max_steps = 10
17
18    for step in range(max_steps):
19        # Think about next action
20        thought = llm.generate(f"""
21Context: {context}
22
23What should I do next? Think step by step.
24If I have enough information, say "DONE".
25Otherwise, specify what action to take.
26""")
27
28        if "DONE" in thought:
29            return llm.generate(f"Based on: {context}\nProvide final answer.")
30
31        # Take action
32        action = extract_action(thought)
33        observation = execute_action(action)
34
35        # Update context with observation
36        context += f"\nThought: {thought}\nAction: {action}\nObservation: {observation}"
37
38    return "Could not complete within step limit"

2. Error Recovery

📝error_recovery.txt
1Task: "Read the config from config.yaml and update the port to 8080"
2
3Thought 1: I'll read the config file.
4Action 1: read_file("config.yaml")
5Observation 1: Error: File not found
6
7Thought 2: The file might be in a different location. Let me search.
8Action 2: find_file("config.yaml")
9Observation 2: Found at ./settings/config.yaml
10
11Thought 3: Now I'll read from the correct path.
12Action 3: read_file("./settings/config.yaml")
13Observation 3: port: 3000
14               host: localhost
15
16Thought 4: I'll update the port to 8080.
17Action 4: edit_file("./settings/config.yaml", "port: 3000", "port: 8080")
18Observation 4: File updated successfully.
19
20The agent recovered from the initial error by reasoning about alternatives.

3. Interpretability

ReAct traces are human-readable and debuggable:

  • See the reasoning: Understand why each action was taken
  • Identify issues: Spot where reasoning went wrong
  • Debug failures: Trace back to problematic thoughts
  • Trust the agent: Verify the logic before accepting results

4. Flexibility

📝flexibility.txt
1ReAct adapts to:
2
3CHANGING REQUIREMENTS:
4"Actually, also update the host" → Agent adjusts plan mid-execution
5
6UNEXPECTED SITUATIONS:
7"File is locked" → Agent tries alternative approach
8
9NEW INFORMATION:
10"There's a newer config format" → Agent adapts to use new format
11
12PARTIAL FAILURES:
13"Port update worked but host failed" → Agent retries just the failed part

When to Use ReAct

ScenarioUse ReAct?Why
Multi-step tasksYesNeed iterative progress with verification
Tasks requiring external infoYesGrounds reasoning in reality
Complex debuggingYesAdaptive problem-solving
Simple Q&AMaybeOften overkill for simple questions
Creative writingUsually notPure generation is often better
Real-time responsesCarefulEach step adds latency

Good Fit for ReAct

  • Coding tasks (read, edit, test, iterate)
  • Research tasks (search, read, synthesize)
  • Data analysis (query, analyze, visualize)
  • System administration (check, configure, verify)
  • Bug fixing (reproduce, diagnose, fix, test)

Less Ideal for ReAct

  • Simple factual questions (one-shot is faster)
  • Pure generation tasks (no actions needed)
  • Time-critical responses (latency matters)
  • Tasks with no tools available

Start with ReAct

When building agents, start with ReAct. It's the most reliable pattern for tasks requiring tools. Optimize to simpler patterns only when ReAct proves unnecessary.

Summary

Understanding ReAct:

  1. Definition: Interleaved reasoning and acting
  2. Pattern: Thought → Action → Observation → Repeat
  3. Core insight: Reasoning and acting are synergistic
  4. Benefits: Grounded, recoverable, interpretable, flexible
  5. Best for: Multi-step tasks requiring external tools
Next: Let's dive deeper into the Thought-Action-Observation cycle and how each component works.