Chapter 6
12 min read
Section 37 of 175

ReAct vs Chain-of-Thought

The ReAct Pattern

Introduction

ReAct and Chain-of-Thought (CoT) are both prompting techniques that encourage LLMs to reason explicitly. Understanding their differences helps you choose the right approach for each task.

The Key Difference: Chain-of-Thought reasons about what it knows. ReAct reasons about what to do to learn more. CoT is introspective; ReAct is interactive.

Chain-of-Thought Explained

Chain-of-Thought prompting encourages step-by-step reasoning before giving a final answer:

📝cot_example.txt
1Question: If I have 3 apples and buy 2 more, then give half
2to my friend, how many do I have left?
3
4Chain-of-Thought Response:
5
6Let me work through this step by step:
7
81. I start with 3 apples
92. I buy 2 more apples: 3 + 2 = 5 apples
103. I give half to my friend: 5 / 2 = 2.5 apples
114. Since I can't give half an apple, I round down: 2 apples
12
13Wait, let me reconsider. "Half" likely means half of the total.
145 / 2 = 2.5, and I give away 2.5, keeping 2.5.
15
16If we're talking about whole apples, I'd give 2 and keep 3,
17or give 3 and keep 2.
18
19Answer: I have 2 or 3 apples left, depending on how we
20handle the half.

CoT Prompting Techniques

🐍cot_prompting.py
1# Zero-shot CoT
2zero_shot_prompt = """
3Question: {question}
4
5Let's think step by step.
6"""
7
8# Few-shot CoT
9few_shot_prompt = """
10Question: What is 15% of 80?
11
12Let's think step by step:
131. 15% means 15/100 = 0.15
142. 0.15 × 80 = 12
15
16Answer: 12
17
18Question: {question}
19
20Let's think step by step:
21"""
22
23# Self-consistency CoT (generate multiple, take majority)
24def self_consistent_cot(question: str, n_samples: int = 5) -> str:
25    answers = []
26    for _ in range(n_samples):
27        response = llm.generate(f"{question}\nLet's think step by step:")
28        answer = extract_final_answer(response)
29        answers.append(answer)
30
31    # Return most common answer
32    from collections import Counter
33    return Counter(answers).most_common(1)[0][0]

ReAct vs CoT Comparison

AspectChain-of-ThoughtReAct
Reasoning typeInternal onlyInternal + external
ActionsNoneTool use, search, execute
KnowledgeTraining data onlyCan acquire new information
FeedbackSelf-check onlyReal-world observation
Hallucination riskHigherLower (grounded)
LatencySingle LLM callMultiple LLM calls
CostLowerHigher (more calls + tools)

Same Task, Different Approaches

📝comparison_example.txt
1Task: "What is the current stock price of Apple?"
2
3CHAIN-OF-THOUGHT:
4"Let me think about this...
5Apple is a major tech company traded as AAPL.
6Stock prices fluctuate constantly.
7Based on recent trends, it might be around $180-190.
8
9Answer: Approximately $185 (note: this is an estimate)"
10
11Problem: The answer is a guess from training data!
12
13---
14
15REACT:
16Thought: I need to find the current stock price.
17         This requires up-to-date information.
18Action: search(query="AAPL stock price today")
19Observation: AAPL is trading at $193.42, up 1.2% today.
20
21Thought: I have the current price from a reliable source.
22Action: finish(answer="Apple (AAPL) is currently at $193.42")
23
24Benefit: Actual current information, not a guess.
📝reasoning_vs_grounding.txt
1CHAIN-OF-THOUGHT EXCELS:
2- Mathematical reasoning
3- Logical deduction
4- Problems with complete information
5- Analysis of given text
6
7Example: "Given that all dogs are mammals and all mammals
8         are warm-blooded, are dogs warm-blooded?"
9
10CoT: "Let me reason through this:
111. All dogs are mammals (given)
122. All mammals are warm-blooded (given)
133. Therefore, since dogs are mammals, dogs are warm-blooded.
14
15Answer: Yes, dogs are warm-blooded."
16
17(No external information needed - pure deduction)
18
19---
20
21REACT EXCELS:
22- Tasks requiring current information
23- Multi-step research
24- Code execution and verification
25- Interacting with systems
26
27Example: "Is the website example.com currently online?"
28
29ReAct:
30Thought: I need to check if the website is responding.
31Action: check_website(url="https://example.com")
32Observation: Status 200 OK, response time 45ms
33
34Answer: Yes, example.com is online and responding normally.
35
36(Required actual verification)

When to Use Each

Use Chain-of-Thought When:

  • The problem can be solved with available information
  • Mathematical or logical reasoning is primary
  • Speed matters and single-call is preferable
  • No external tools are available
  • You need interpretable reasoning traces

Use ReAct When:

  • External information or verification is needed
  • The task involves multiple steps with real actions
  • Results must be grounded in reality
  • Error recovery is important
  • Tools and APIs are available
Task TypeRecommendedWhy
Math word problemCoTPure reasoning, no external info needed
Current events Q&AReActNeeds up-to-date information
Code reviewCoT or ReActDepends on if execution is needed
Bug fixingReActNeed to run code, check output
Essay analysisCoTAll information in the text
Research taskReActNeed to gather information

Combining Approaches

The best agents often combine CoT and ReAct:

🐍hybrid_approach.py
1class HybridAgent:
2    """Combines CoT for reasoning, ReAct for action."""
3
4    def __init__(self, llm, tools):
5        self.llm = llm
6        self.tools = tools
7
8    def solve(self, task: str) -> str:
9        # Step 1: CoT analysis to understand the task
10        analysis = self.analyze_with_cot(task)
11
12        # Step 2: Determine if external action needed
13        needs_action = self.needs_external_action(analysis)
14
15        if not needs_action:
16            # Pure reasoning is sufficient
17            return self.solve_with_cot(task)
18        else:
19            # Use ReAct for grounded execution
20            return self.solve_with_react(task, analysis)
21
22    def analyze_with_cot(self, task: str) -> str:
23        """Use CoT to analyze the task."""
24        prompt = f"""
25Analyze this task step by step:
26
27Task: {task}
28
29Consider:
301. What type of problem is this?
312. What information do I need?
323. Is all needed information available, or do I need to gather more?
334. What's my approach?
34
35Analysis:
36"""
37        return self.llm.generate(prompt)
38
39    def needs_external_action(self, analysis: str) -> bool:
40        """Determine if external action is needed."""
41        external_indicators = [
42            "need to search",
43            "need to check",
44            "need to verify",
45            "current information",
46            "real-time",
47            "execute",
48            "run",
49            "file",
50        ]
51
52        analysis_lower = analysis.lower()
53        return any(ind in analysis_lower for ind in external_indicators)
54
55    def solve_with_cot(self, task: str) -> str:
56        """Solve using pure Chain-of-Thought."""
57        prompt = f"""
58{task}
59
60Let me think through this step by step:
61"""
62        response = self.llm.generate(prompt)
63        return self.extract_answer(response)
64
65    def solve_with_react(self, task: str, analysis: str) -> str:
66        """Solve using ReAct with tools."""
67        # Initialize ReAct agent
68        react = ReActAgent(self.llm, self.tools)
69        # Include analysis as context
70        enhanced_task = f"{task}\n\nInitial analysis:\n{analysis}"
71        return react.run(enhanced_task)

CoT Within ReAct

📝cot_in_react.txt
1Task: "Calculate the total cost of items in cart.json and
2apply a 15% discount if over $100"
3
4Thought: I need to first read the cart to see the items and prices.
5Action: read_file(path="cart.json")
6Observation: {"items": [{"name": "Book", "price": 29.99},
7             {"name": "Pen", "price": 4.99},
8             {"name": "Notebook", "price": 12.99}]}
9
10Thought: Let me calculate the total using chain-of-thought reasoning:
11- Book: $29.99
12- Pen: $4.99
13- Notebook: $12.99
14- Total: 29.99 + 4.99 + 12.99 = $47.97
15
16Since $47.97 < $100, no discount applies.
17
18Action: finish(answer="Total: $47.97. No discount applied (under $100)")
19
20The agent used CoT reasoning WITHIN the ReAct thought phase!

Best of Both Worlds

Don't think of CoT and ReAct as mutually exclusive. Use CoT for complex reasoning within ReAct thoughts. Use ReAct to ground CoT reasoning with real-world verification.

Summary

ReAct vs Chain-of-Thought:

  1. CoT: Internal reasoning, no actions, fast but ungrounded
  2. ReAct: Reasoning + actions, grounded but slower
  3. CoT for: Logic, math, analysis of given information
  4. ReAct for: Tasks needing external info or verification
  5. Combine: Use CoT within ReAct for best results
Next: Let's build a complete ReAct agent with advanced features like memory and error recovery.