ReAct Pattern

Medium 28 min read

What is ReAct?

Why ReAct Matters

The Problem: Simple agents either reason without acting (just generating text) or act without reasoning (blindly calling tools). Both approaches fail on complex tasks.

The Solution: ReAct (Reasoning + Acting) interleaves thinking and doing -- the agent explicitly reasons about what to do next, takes an action, observes the result, then reasons again.

Real Impact: ReAct is the foundation of most modern agent frameworks and dramatically improves task completion accuracy.

Real-World Analogy

Think of ReAct like a detective solving a case:

  • Thought = "Based on the evidence, the suspect was at the office at 9pm"
  • Action = Check the security camera footage from 9pm
  • Observation = Camera shows suspect leaving at 8:45pm
  • Thought = "My hypothesis was wrong. Let me reconsider..."
  • Action = Interview the receptionist about who was there

ReAct Core Concepts

Thought

The agent explicitly reasons about the current situation, what information is needed, and what action to take next.

Action

The agent selects and executes a tool with specific parameters based on its reasoning.

Observation

The result of the action is fed back to the agent as new information to reason about.

Iteration

The loop continues until the agent has enough information to provide a final answer.

Thought-Action-Observation Loop

ReAct Loop: Thought - Action - Observation
Thought Reason about next step Action Execute a tool Observation Get tool result Loop until answer found Final Answer

Implementation

react_agent.py
class ReActAgent:
    def run(self, query, max_steps=10):
        messages = [
            {"role": "system", "content": "You are a ReAct agent. For each step: Thought, Action, then wait for Observation. When done: Final Answer."},
            {"role": "user", "content": query}
        ]

        for step in range(max_steps):
            response = self.llm.chat(messages=messages)
            text = response.content

            if "Final Answer:" in text:
                return text.split("Final Answer:")[1].strip()

            # Parse and execute the action
            action = self.parse_action(text)
            observation = self.execute(action)

            # Feed observation back into the loop
            messages.append({"role": "assistant", "content": text})
            messages.append({"role": "user",
                "content": f"Observation: {observation}"})
Output
Thought: I need to find the current weather in Tokyo.
Action: search("current weather Tokyo")
Observation: Tokyo is currently 22C with partly cloudy skies.
Thought: I now have the answer.
Final Answer: The current weather in Tokyo is 22C and partly cloudy.
Key Takeaway: ReAct combines reasoning (Thought) with acting (Action) in an interleaved loop. The Observation step grounds the agent's reasoning in real data, preventing hallucination.

Common Mistake

Wrong: Allowing unlimited reasoning loops without a maximum iteration count

Why it fails: The agent can enter infinite loops, cycling through the same thoughts and actions, burning tokens and never reaching a final answer.

Instead: Set a max_iterations limit (typically 5-10) and return a fallback response when exceeded.

ReAct vs Other Patterns

PatternReasoningActingBest For
ReActInterleaved with actionsAfter each thoughtGeneral-purpose tool use
Chain of ThoughtAll upfrontNonePure reasoning tasks
Plan-and-ExecutePlan first, then executeAfter full planComplex multi-step tasks
ReflexionIncludes self-evaluationWith retry loopTasks needing self-correction

Advanced ReAct

Advanced Techniques

  • ReAct + Reflection: Add a self-evaluation step after each observation
  • Parallel ReAct: Run multiple thought-action chains simultaneously
  • Bounded ReAct: Set strict limits on steps and cost per query
  • ReAct + Memory: Store successful strategies for similar future tasks
Deep Dive: ReAct vs Chain-of-Thought

Chain-of-Thought (CoT) prompting generates reasoning traces but cannot interact with the external world. ReAct extends CoT by interleaving reasoning with tool calls. Research shows ReAct outperforms CoT on tasks requiring factual knowledge (since it can look things up) while maintaining CoT's advantages for multi-step reasoning. The key insight is that actions ground the reasoning in reality, reducing hallucination rates by 30-50% compared to pure reasoning approaches.

Key Takeaway: ReAct is the foundational pattern for tool-using agents. Most modern agent frameworks (LangChain, AutoGen) implement variations of the ReAct loop under the hood.

Quick Reference

ComponentPurposeImplementation
ThoughtExplicit reasoning traceLLM generates reasoning text
ActionTool invocationParse tool name + args from output
ObservationTool result feedbackAppend result to message history
TerminationStop conditionMax steps or Final Answer token
Error RecoveryHandle tool failuresReturn error as observation