React (reason + act)

ReAct (Reasoning + Acting) is one of the most powerful agent patterns. The agent thinks about what to do, takes an action (like calling a tool), observes the result, and repeats, interleaving reasoning and action until it reaches a final answer.

React: think → act → observe loop

The reasoning step. Before each action, the agent explains its thinking, which lets it course-correct based on observations. Without reasoning, the agent blindly chains tool calls. With it, the agent adapts dynamically to unexpected results.

patterns/34_react.py
python
class ReActAgent:
    def __init__(self, tools, max_iterations=5):
        self.tools = tools
        self.max_iterations = max_iterations
        self.llm = get_llm()

    def solve(self, question):
        context = []
        for i in range(self.max_iterations):
            # THINK
            prompt = self._build_prompt(question, context)
            response = self.llm.generate(prompt).content

            if self._is_final_answer(response):
                return self._extract_answer(response)

            # ACT
            thought = self._parse_thought(response)
            tool_name, args = self._parse_action(response)

            # OBSERVE
            observation = self._execute_action(tool_name, args)
            context.append({
                "thought": thought,
                "action": f"{tool_name}({args})",
                "observation": observation
            })
        return "Max iterations reached"

    def _execute_action(self, tool_name, args):
        if tool_name in self.tools:
            return self.tools[tool_name](args)
        return f"Unknown tool: {tool_name}"

ReActAgent with Think-Act-Observe loop and tool execution.

Planning creates a full plan upfront before executing. ReAct interleaves thinking and acting: the agent reasons about what to do next, takes one action, observes the result, then reasons again. ReAct is more adaptive because each step is informed by actual results.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Matching exercise: Match React concepts

Loading practice…

Ordering exercise: Order the ReAct loop

Loading practice…

Flashcards: Flashcards

Loading practice…

ReAct is one of the most widely used agent patterns in production systems. Next, we add a verification step to make plan execution more reliable with Plan-execute-verify.