ReAct Pattern

Medium 28 min read
ReAct pattern: 9 panels covering what ReAct is with a detective analogy, the 4 core concepts (thought, action, observation, iteration), the thought-action-observation loop diagram, a Python implementation that parses model output and feeds observations back, ReAct vs Chain of Thought vs Plan-and-Execute vs Reflexion, advanced ReAct techniques (reflection, parallel ReAct, bounded ReAct, ReAct + memory), an example trace, common pitfalls and best practices, a quick reference table, and the end-to-end ReAct workflow
ReAct pattern — interleaving reasoning and acting through the thought-action-observation loop.

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}"})

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

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