Module 4 · Lesson 3 · 16 min read
The ReAct pattern: think, then act, then observe
The ReAct Pattern
Introduction
The ReAct pattern is one of the most influential patterns in building agents. It combines reasoning and acting in a single loop: the agent thinks, then carries out an action, then reads the observation and thinks again. In this lesson we understand the idea, compare it with the alternatives and build a complete ReAct loop in Python.
Learning objectives
- Explain the components of the ReAct pattern: thought, action and observation
- Compare ReAct with reasoning alone and with acting alone
- Implement a simplified ReAct loop with a maximum number of steps and stop conditions
- Identify common mistakes in applying ReAct and ways to avoid them
Where did ReAct come from?
The ReAct paper (Yao and colleagues, 2022) introduced a simple, powerful idea: instead of the model reasoning only in its head as in chain-of-thought, or carrying out actions without stated thinking, we make it alternate between the two. The name itself combines the words Reasoning and Acting. The paper showed that this alternation helps in tasks that require gathering information from external sources, because real observations correct the reasoning, and the reasoning guides the next actions.
The three-part loop
At each step the agent goes through three stages:
- Thought: the agent writes what it knows, what it is missing and what it will do now.
- Action: it calls a tool with specific arguments, or declares the final answer.
- Observation: the tool's result returns and is added to the context.
Then a new cycle begins with a thought that takes the observation into account.
Comparison with the alternatives
| Pattern | Strength | Weakness |
|---|---|---|
| Reasoning only | Good for closed problems such as mathematics | Does not reach new information and may hallucinate facts |
| Acting only | Fast and direct | Repeats calls without direction, and its decisions are hard to understand |
| ReAct | Links every action to a reason and corrects the course from observations | Slower and more costly, and may go round in loops |
An important extra advantage: written thoughts make the agent's behavior traceable; when it makes a mistake we read the log and know where it went off course.
A practical example: a question that needs two steps
A student asked: "Does the total of the hours for the artificial intelligence and databases courses exceed my allowed limit this semester, which is 9 extra hours?" The agent needs to look up the hours of each course and then compute. We will write a ReAct loop in plain Python, replacing the model with a deterministic rule function policy that imitates what a real model might decide:
KB = {"Artificial Intelligence": 3, "Databases": 4}
def knowledge_search(course):
hours = KB.get(course)
return f"{course}: {hours} hours" if hours else "Not found"
def calculator(expr):
a, b = expr.split("+")
return str(int(a) + int(b))
TOOLS = {"knowledge_search": knowledge_search, "calculator": calculator}
def policy(question, history):
# A deterministic stand-in for the model: decides the next thought and action from the observations
found = {k: v for k, v in KB.items() if any(k in h for h in history)}
for course in KB:
if course not in found:
return (f"I need the hours of the {course} course", "knowledge_search", course)
if not any(h.startswith("Total") for h in history):
expr = "+".join(str(v) for v in found.values())
return ("I have the hours, I will add them up", "calculator", expr)
total = int(history[-1].split("=")[1])
verdict = "does not exceed" if total <= 9 else "exceeds"
return ("I have everything I need", "finish", f"The total is {total} hours and {verdict} the limit")
def react(question, max_steps=6):
history = []
for step in range(1, max_steps + 1):
thought, action, arg = policy(question, history)
print(f"[{step}] Thought: {thought}")
if action == "finish":
return arg
obs = TOOLS[action](arg)
if action == "calculator":
obs = f"Total={obs}"
print(f" Action: {action}({arg}) -> Observation: {obs}")
history.append(obs)
return "Stopped: reached the maximum number of steps without an answer"
print(react("Does the total of the hours exceed 9?"))The agent goes through four steps: a search for the first course, then the second, then the addition, then the final answer "The total is 7 hours and does not exceed the limit". In a real system a call to the model replaces policy, and the structure stays the same.
Stop conditions and common mistakes
A ReAct loop needs more than one way to stop:
- A final answer declared by the agent.
- A maximum number of steps that prevents endless looping.
- Repetition detection: if the agent requests the same tool with the same arguments twice in a row, it is probably stuck.
- A budget for time or cost.
Common mistakes include: not adding the observation to the context so the agent repeats the same request, very long observations that fill the context window, and general thoughts that do not say exactly what is missing.
Summary
ReAct is a pattern in which the agent alternates between thought, action and observation in a loop. Real observations correct the reasoning, the reasoning guides the actions, and the resulting log makes the behavior traceable. It outperforms reasoning alone in tasks that need external information, but it is slower and more costly. The loop needs several stop conditions: a final answer, a step limit, repetition detection and a budget.
Terms
| Term | English | Definition |
|---|---|---|
| Reasoning and acting | ReAct | A pattern in which the agent alternates between thinking, carrying out actions and reading observations |
| Thought | Thought | Reasoning text that states what the agent knows and what it will do |
| Action | Action | Calling a tool or declaring the final answer |
| Observation | Observation | The result of an action that is added to the context |
| Stop condition | Stop Condition | A criterion that ends the loop, such as a final answer or a step limit |
| Trace | Trace | The sequence of thoughts, actions and observations for reviewing the agent's behavior |
Review questions
- What are the three stages in each step of ReAct? And what is the role of each?
- Why does ReAct outperform reasoning alone on questions that need recent data?
- Name three stop conditions and explain why the loop needs more than one condition.
- How do you detect that a ReAct agent is stuck in a loop?
References
- ReAct: Synergizing Reasoning and Acting in Language Models arxiv.org Reviewed on 2026-11-18
- Chain-of-Thought Prompting Elicits Reasoning in Large Language Models arxiv.org Reviewed on 2026-11-18
- OpenAI Agents SDK: Running agents openai.github.io Reviewed on 2026-11-18
