Skip to main content
Time to implement the ReAct pattern so your agent exposes its internal reasoning while calling tools. The ReAct loop makes the agent produce a “Thought” before acting and an “Observation” after each tool result, letting you follow its chain-of-thought: Reason → Act → Observe → Repeat.
A retro, pixel-art style splash screen that says "BUILD A REACT AGENT" with the subtitle "You built an agent that calls tools." A small "HANDS-ON LAB" label and buttons including "But you can't see its reasoning" and "REACT PATTERN" are shown below.
This guide walks through a minimal Python ReAct agent. The environment (Python, virtualenv, OpenAI SDK) is already set up and your working directory is /root/code. We’ll:
  • Start with a compact agent implementation that calls a single check_calendar tool.
  • Modify the system prompt to require Thought/Observation lines (the ReAct style).
  • Add a second tool (search_contacts) and show a multi-step interaction.
  • Summarize best practices and include a tool reference table.
Why this matters: ReAct helps debug and audit multi-step agent decisions by making intermediate reasoning visible.

1) Minimal agent that calls a single tool

Create react_agent.py with this simple agent loop that supports one tool: check_calendar(day). The model may call the tool, and the agent dispatches to the local function and appends the tool result back into the conversation. At this stage, the model does not reveal its chain-of-thought.
Run the script. The model will call check_calendar and return a final answer, but its internal reasoning will be hidden. Example console output (chain-of-thought not visible):

2) Make the model reveal its reasoning: add a ReAct system prompt

To force the model to expose its internal loop, replace the simple system prompt with explicit ReAct instructions that require the model to write a Thought: line before each tool call and an Observation: line after every tool result. Update the messages list as shown:
Run the agent again with the same check_calendar tool. The model will produce a visible Thought line, a tool call, and an Observation line before the final answer. Example illustrative output:
Now you can follow the agent’s reasoning and tool interaction.

3) Add a second tool for multi-step tasks

Next, implement search_contacts(name) and extend the tools list and dispatch logic so the agent can perform multi-step workflows that require both tools (e.g., check calendar then look up a contact). Add the contact-search function and extend tools:
Update the dispatch logic inside the main while True loop to handle both function names:
Change the user prompt to a multi-step request:
When you run the updated script with the ReAct system prompt, the model will produce a step-by-step loop: it will write Thoughts, call tools, show Observations, and then provide a combined final answer. Illustrative output:

4) Tool reference (quick lookup)

Note: When documenting parameter examples or small JSON objects in tables, keep them inline code as shown above so MDX doesn’t mis-parse them.

5) Best practices and tips

  • Always include clear tool descriptions (name, parameters, required fields) so the model understands available actions.
  • Append tool results as role: "tool" messages so the model can observe outcomes and continue reasoning.
  • Use the ReAct system prompt pattern for transparent multi-step reasoning:
    • Thought: state your reasoning
    • [tool_call] run the tool
    • Observation: report the tool’s output
    • Repeat until confident to answer
  • Use ReAct for debugging and audits: it makes decision steps explicit and easier to verify.

Summary

  • ReAct exposes model reasoning in the agent loop using Thought → Act → Observation → (repeat).
  • Add function/tool descriptions to the agent (so the model knows what it can call).
  • Append tool results as role: "tool" conversation messages so the model can observe and continue reasoning.
  • The ReAct pattern is especially helpful for debugging multi-step agent behavior.
References and further reading:
Use the ReAct system prompt whenever you want the model to make its intermediate reasoning and tool usage explicit. This is especially helpful for debugging multi-step agent behavior.

Watch Video