ReAct: Reasoning + Acting
After this lesson, you will be able to:
- Explain why ReAct (interleaving Thought → Action → Observation) is a widely used agent pattern, and when it beats both pure chain-of-thought and pure tool-calling
- Write a ReAct loop in 30 lines that ingests a user query, lets the LLM think aloud, dispatches tool calls, and feeds observations back
- Tune the ReAct loop's stopping criteria (max iterations, confidence thresholds, fallback heuristics) for production reliability
- Recognize ReAct's failure modes (runaway loops, tool selection thrashing, hallucinated observations) and apply the standard fixes
Before you start
#The ReAct Loop
#The Original Format
Yao et al. 2022 used few-shot prompting with examples like:
Question: What's the population of the city where Albert Einstein was born?
Thought: I need to find Einstein's birth city, then look up its population.
Action: search[Albert Einstein birthplace]
Observation: Albert Einstein was born in Ulm, Germany.
Thought: Now I need Ulm's population.
Action: search[Ulm population]
Observation: Ulm has a population of approximately 126,000.
Thought: I have the answer.
Final Answer: Approximately 126,000.
Modern implementations replace the text-format Action with native tool calling (OpenAI tools, Anthropic tools), but the structure is identical: model emits thought + tool call, system runs tool, observation fed back as a tool message, repeat.
Here is a more detailed worked trace on a calculator-plus-search task, showing how thoughts shape successive actions:
Question: If a Tesla Model 3 starts at $39,990 and a Toyota Camry starts at $26,420, what's
the percentage difference relative to the Camry?
Thought 1: Both prices appear in the question, but I should verify them before doing math —
prices change frequently. I'll search for the current MSRP of each.
Action 1: search[Tesla Model 3 2026 starting MSRP]
Observation 1: Tesla Model 3 starts at $42,490 as of Q2 2026.
Thought 2: The question stated $39,990 but the current MSRP is $42,490. I'll use the
current MSRP because the user likely wants up-to-date info. Now Camry.
Action 2: search[Toyota Camry 2026 starting MSRP]
Observation 2: Toyota Camry starts at $28,400 as of Q2 2026.
Thought 3: Diff = (42490 - 28400) / 28400. I'll compute that.
Action 3: calculator[(42490 - 28400) / 28400 * 100]
Observation 3: 49.61
Thought 4: I have enough to answer, and I should note that I corrected the user's stale numbers.
Final Answer: Using current 2026 MSRPs ($42,490 vs $28,400), the Model 3 is about 49.6% more
expensive than the Camry. Note: the prices you cited are from a previous model year.
Three things to notice: (1) the thought before each action explicitly justifies WHY that tool is needed; (2) thoughts can override information in the prompt when observations contradict it; (3) the final answer surfaces the discrepancy rather than silently using stale numbers. Take away the thoughts and the agent would either trust the prompt verbatim (wrong) or chain blind tool calls (no error recovery).
In the trace above, the agent saw the prompt say '$39,990' but search returned '$42,490'. Which line in the ReAct loop made the agent correct the user instead of trusting the prompt?
#ReAct vs Chain-of-Thought: When Each Wins
ReAct is often confused with chain-of-thought (CoT) prompting because both interleave reasoning with the model's response. The crucial difference is grounding. Pure CoT writes plausible-sounding reasoning from training-time knowledge; ReAct's reasoning is grounded in tool observations from the live world.
| Setting | CoT | ReAct |
|---|---|---|
| Pure arithmetic ("what is 23 * 47 - 14?") | Strong, model can compute internally | Equivalent + slower (adds tool call) |
| Stale-knowledge facts ("current Bitcoin price") | Hallucinates last-seen value | Strong, search/calculator grounds answer |
| Multi-hop QA ("president of country with largest GDP in Africa") | Compounds errors across hops | Strong, each hop verifies via tool |
| Long arithmetic chains | Drifts after ~5 steps | Strong, calculator removes drift |
| Codegen with imports/APIs | Hallucinates function signatures | Strong, doc search grounds API surface |
| Pure creative writing | Strong | Equivalent, no tools to ground in |
| Latency < 500ms | Strong (one model call) | Weak, each tool adds 200-2000ms |
A user asks a finance copilot: 'What was Apple's revenue in their most recent quarter, and what's that as a multiple of their R&D spending?' Pure CoT or ReAct?
#Implementation: 30-Line Python ReAct Loop
Run this version here. A scripted stand-in plays the part of the model, so it needs no API key. The loop, the tools, and the stop rule are the real ones.
Tests: Verify the loop chains two tool calls (search Einstein, then search Ulm) before answering. Verify the final answer mentions about 127,000.
openai package. It needs an API key, so it does not run on this page. Copy it to Google Colab.# Copy to Colab: needs the openai package and an API key, so it cannot run on this page.
# Minimal ReAct loop with OpenAI tool calling
# Run: pip install openai
import os
import json
from openai import OpenAI
client = OpenAI()
# --- Define tools ---
def search_wikipedia(query: str) -> str:
"""Mocked Wikipedia search. In production: use the real API."""
db = {
"Albert Einstein": "Albert Einstein (1879-1955) was born in Ulm, Germany.",
"Ulm": "Ulm is a city in southern Germany with a population of about 127,000 (2024).",
"Marie Curie": "Marie Curie was born in Warsaw, Poland in 1867.",
"Warsaw": "Warsaw is the capital of Poland with a population of about 1.86 million.",
}
for key, value in db.items():
if key.lower() in query.lower():
return value
return f"No results for '{query}'."
def calculator(expression: str) -> str:
"""Safe arithmetic eval, production should use a sandboxed evaluator."""
try:
return str(eval(expression, {"__builtins__": {}}, {}))
except Exception as e:
return f"Error: {e}"
TOOLS = [
{
"type": "function",
"function": {
"name": "search_wikipedia",
"description": "Search Wikipedia for information about a person, place, or topic.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "calculator",
"description": "Evaluate a math expression like '127000 / 1860000'.",
"parameters": {
"type": "object",
"properties": {"expression": {"type": "string"}},
"required": ["expression"],
},
},
},
]
TOOL_MAP = {"search_wikipedia": search_wikipedia, "calculator": calculator}
# --- ReAct loop ---
def react(user_query: str, max_iters: int = 10) -> str:
messages = [
{"role": "system", "content": "You are a research assistant. Think step by step, use tools to look up facts, then answer."},
{"role": "user", "content": user_query},
]
for iteration in range(max_iters):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=TOOLS,
)
msg = response.choices[0].message
messages.append({"role": "assistant", "content": msg.content, "tool_calls": msg.tool_calls})
if not msg.tool_calls:
return msg.content # Final answer
# Run each tool call
for tc in msg.tool_calls:
args = json.loads(tc.function.arguments)
result = TOOL_MAP[tc.function.name](**args)
print(f" [TOOL] {tc.function.name}({args}) -> {result[:100]}")
messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
return "Max iterations reached."
# --- Test ---
print("Q: What's the population of the city where Albert Einstein was born?")
print("A:", react("What's the population of the city where Albert Einstein was born?"))
print("\nQ: What fraction of Warsaw's population is Ulm's population?")
print("A:", react("What fraction of Warsaw's population is Ulm's population?"))
print("\nCHALLENGE: Add a 'browse_url' tool. Test on a 3-hop question.")#Stopping Criteria
ReAct loops can run forever without proper stopping rules. Production must enforce:
- Max iterations (typically 10-20). Hard cap.
- Token budget (e.g., 50K tokens total). Stops runaway thoughts.
- No-progress detection: if the LLM emits the same tool call twice in a row, break out and force a final answer.
- Confidence thresholds: stop early if the LLM's logprob-based confidence is high.
- User cancellation: support graceful early termination.
#What a long loop costs
The iteration cap and the token budget exist because every step re-reads everything before it: the model API is stateless, so each call sends the whole message log again. Open the derivation to see how the total tokens processed grow with the number of steps T.
Two more loop facts are derived elsewhere. If each step independently succeeds with probability p, the chance that all T steps succeed is a product of per-step chances, derived in The RAG Pipeline. The number of steps until the first failure is a geometric waiting time, derived in The Distribution Zoo, Part 1: The Core Distributions.
#Failure Modes
#Simulate a ReAct Loop on a Toy Tool Registry
The best way to internalize ReAct's control flow is to run a tiny simulation. Below, we model the loop in plain Python with a deterministic policy stand-in instead of an LLM, so you can see how the message log grows and how stopping criteria fire.
Once that runs, try editing the policy function so it also handles the Curie query (search Curie -> search Warsaw -> answer). You will discover a subtle truth: in real ReAct, the LLM IS the policy. Every fix you make in code is a fix you would otherwise make by prompt-engineering the system message. The loop infrastructure stays identical; only the policy gets smarter.
#ReAct vs Plan-Then-Execute vs Reflexion
| Pattern | When | Tradeoff |
|---|---|---|
| ReAct | General-purpose, default | Best balance; simple to implement |
| Plan-then-Execute | Long, structured tasks | Better for non-mutable plans; adds latency |
| Reflexion | Tasks requiring iterative improvement | Adds a full retry per round |
| LATS / Tree Search | Hard puzzles, code, math | Adds many extra model calls for top-tier quality |
For most production agents, ReAct + good tool descriptions + sane stopping criteria is the right answer.
#ReAct Prompt Template
If you're hand-writing a ReAct system prompt (instead of relying on a framework's defaults), the canonical shape looks like the snippet below. The framing matters: it tells the model that observations come from a real environment, not the model's imagination.
You are a research assistant with access to the following tools.
Use them in a Thought/Action/Observation loop until you can answer the user's question.
Tools:
- search(query: str) -> string. Returns top result from Wikipedia.
- calculator(expression: str) -> string. Evaluates math expressions.
- finish(answer: str). Use this to return the final answer to the user.
Format every step exactly as:
Thought: <one sentence of reasoning about what to do next>
Action: <tool_name>(<json args>)
After each Action you will receive:
Observation: <tool output>
When you have enough information, emit:
Thought: I have enough to answer.
Action: finish({"answer": "..."})
Hard rules:
- Never invent Observation lines yourself; only the system emits them.
- If a search returns no useful result, try a different query before giving up.
- Stop after 10 Thought/Action pairs; emit finish() with your best partial answer.
In production with native tool-calling APIs, you drop the textual format and let the model emit tool calls natively, but the framing rules (no hallucinated observations, retry-then-give-up, hard iteration cap) belong in the system prompt either way.
Your ReAct prompt does NOT include the rule 'Never invent Observation lines yourself; only the system emits them.' What's the most likely failure mode?
#Key Takeaways
- ReAct interleaves Thought → Action → Observation. A widely used agent pattern, and a default in many frameworks
- Tool calling APIs structurally enforce ReAct. The model can emit thoughts (assistant content) interleaved with tool calls, and tool results come back as tool messages
- Production needs stopping criteria. Max iterations, token budget, no-progress detection, confidence thresholds; without them, loops can run forever
- Failure modes: runaway loops, tool thrashing, hallucinated observations, all have standard fixes
- ReAct is the right default. Switch to Plan-then-Execute / Reflexion / LATS only when you have evidence ReAct fails on your task
#Quick Check
Why does ReAct outperform pure chain-of-thought (no tools) on multi-hop questions like 'What's the population of Einstein's birthplace?'