Lesson 5 of 9 · Patterns
ReAct
The ReAct pattern: the model thinks, acts with a tool, observes and repeats. See Thought/Action/Observation in Python and how native tool calling replaced it.
- 5 min read
- Intermediate
- Reviewed
- Explain the ReAct pattern and why reasoning between actions helps
- Run and parse the classic Thought / Action / Observation format
- Relate ReAct to modern native tool calling
- Know ReAct's strengths and weaknesses
Reason, act, observe
Reasoning before each action helps the model keep track of what it knows, what's missing and why the next step makes sense. Observations ground that reasoning in real data instead of guesses.
The classic text format
Before APIs had built-in tool calling, ReAct was done in plain text: the model wrote labelled lines, and the program parsed them with patterns:
import re
# The classic ReAct text format: the model writes Thought / Action / Action Input,
# your code runs the action and appends an Observation, until a Final Answer.
SCRIPT = iter([
"Thought: I need the population of both cities.\nAction: lookup\nAction Input: Lisbon",
"Thought: Now Porto.\nAction: lookup\nAction Input: Porto",
"Thought: Lisbon has about 2.4 times as many people.\nFinal Answer: Lisbon (about 545,000) has roughly 2.4 times Porto's population (about 230,000).",
])
def model(transcript): # a scripted stand-in: a real LLM would read the transcript
return next(SCRIPT)
POPULATION = {"Lisbon": 545_000, "Porto": 230_000}
TOOLS = {"lookup": lambda city: f"{city}: {POPULATION.get(city, 'unknown'):,} people"}
transcript = "Question: How much bigger is Lisbon than Porto, by population?\n"
for _ in range(5):
output = model(transcript)
transcript += output + "\n"
final = re.search(r"Final Answer:\s*(.*)", output)
if final:
print(transcript)
break
action = re.search(r"Action:\s*(\w+)", output).group(1)
action_input = re.search(r"Action Input:\s*(.*)", output).group(1).strip()
observation = TOOLS[action](action_input)
transcript += f"Observation: {observation}\n"The program's job is small: find Action and Action Input in the model's text, run the tool, append an Observation line, and stop at Final Answer. (The model here is a scripted stand-in; a real model would write these lines itself, one step per request.)
ReAct today
Modern APIs replace the fragile text parsing with native tool calling: the model returns a structured tool call instead of an Action: line, and the result goes back as a tool-result message instead of an Observation: line. Thoughts appear as ordinary text before the tool call, or as the model's built-in reasoning in reasoning models.
| Classic ReAct | Modern equivalent |
|---|---|
Thought: … text | Text before a tool call, or built-in reasoning |
Action: and Action Input: lines | A structured tool call (name and JSON arguments) |
Observation: line | A tool result message |
Final Answer: line | A response with no tool calls |
The loop you built in the earlier courses, call the model, run tools, repeat until no tool is requested, is ReAct with native tool calling. It's the default architecture of most agents today.
Common mistakes
Parse a ReAct-style step: find which action the model chose and what input it gave.
import re output = "Thought: I should look this up.\nAction: search\nAction Input: MCP security best practices" # Pull out the action name and its input from the model's text, then print: # search | MCP security best practices
In modern APIs, what replaces ReAct's “Observation:” line?
Summary
- ReAct alternates thought, action and observation until the model can answer.
- The classic version used labelled text lines parsed by the program.
- Today, native tool calling implements the same loop with structured calls and results.
- ReAct suits tasks where each step depends on the last result, but can wander on long tasks.
Next: plan and execute.
Finished reading? Tick it off to track your progress.