Skip to main content
In this tutorial, you’ll implement the ReAct (Reasoning and Acting) pattern, where agents alternate between reasoning about what to do and taking actions to accomplish goals.

What you’ll build

A ReAct agent that:
  • Reasons about the task at hand
  • Plans which tools to use
  • Executes actions iteratively
  • Reflects on results before responding

What is ReAct?

ReAct combines reasoning traces with action execution:
  1. Thought: Agent reasons about the current situation
  2. Action: Agent decides on and executes a tool
  3. Observation: Agent observes the result
  4. Repeat: Process continues until task is solved

Prerequisites

Install required packages:
Set your API key:

Tutorial

1

Define state with reasoning

Create state that tracks both messages and reasoning steps.
2

Create reasoning tools

Define tools for the agent to use during reasoning.
3

Create the ReAct agent node

Build the reasoning agent with explicit prompting.
4

Create tool execution node

Build the node that executes tool calls.
5

Add routing with iteration limit

Create a router that limits iterations to prevent infinite loops.
6

Build the ReAct graph

Assemble the complete ReAct agent.
7

Run the ReAct agent

Test the agent with reasoning-intensive queries.
8

Complete example with logging

Here’s the full code with execution logging:
Save as react_agent.py and run:

Expected output

When running the ReAct agent:

Key concepts

  • Reasoning Loop: Agent thinks before acting
  • Iteration Tracking: Monitor and limit reasoning steps
  • Tool Chaining: Use multiple tools in sequence
  • Explicit Reasoning: Agent verbalizes its thought process
  • Error Recovery: Iteration limits prevent infinite loops

Comparison: ReAct vs Basic Agent

Advanced patterns

Next steps

Multi-Agent

Build systems with multiple specialized agents

Simple Agent

Review the basics of agent building
The ReAct pattern is powerful for complex tasks requiring multi-step reasoning. The explicit reasoning traces make agent behavior more interpretable and debuggable.