Lesson 46.1 · 1 of 3 · 5 min read · Reviewed 24 Nov 2026
Reason, act, observe
By the end, you can
- Describe the ReAct pattern.
- Trace a thought, action, observation cycle.
The cycle
Thought: The user asks why invoice 881 is overdue. I should look it up.
Action: get_invoice(id=881)
Observation: { status: "unpaid", due: "2026-10-01", reminder_sent: false }
Thought: It's unpaid and no reminder went out. Check the customer's payment method.
Action: get_payment_method(customer=42)
Observation: { card: "expired 2026-09" }
Thought: The card expired before the due date. That explains it.
Answer: Invoice 881 is overdue because the card on file expired in September ...
The name comes from Reason + Act. The 2022 paper showed that mixing the two beats reasoning alone (which can invent facts) and acting alone (which flails without a plan).
Why it works
- GroundingGrounding: Basing a model’s answer on supplied, verifiable sources rather than on what it learned in training.: each thought is based on real observations, not memory.
- Adaptivity: a surprising result changes the next step.
- Transparency: the traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. shows why each action was taken, which helps debugging (chapter 67).
When it fits
| Good fit | Poor fit |
|---|---|
| Investigations, debugging, research | A fixed pipeline you can write down in advance |
| Tasks where the next step depends on what you find | Very high-volume, latency-critical steps |
| Small tool sets with clear results | Tasks needing a long, coordinated plan across many parts |
The stop rule
Every ReAct loop needs an exit: a final answer, a maximum number of steps, a token or cost budget, or a human checkpoint. Without one, a confused agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. keeps going (46.3).
Key points
- ReAct interleaves reasoning and acting: think about what to do, take an action, read the result, repeat.
- Each observation feeds the next thought, so the agent adapts to what it finds.
- It suits open-ended tasks where you can't plan every step in advance.
- The loop needs a stop rule: an answer, a step limit, or a budget.
Trace a ReAct loop by hand
Build the thought, action, observation cycle and see each observation shape the next thought.
You will have: A ReAct loop that prints a full trace for the overdue invoice question.
Steps
- Run the starter: it prints one thought and stops.
- Handle the answer case: print it and the number of steps used.
- Otherwise run the tool, print the action and the observation, and keep it.
- Run it and read how the second thought depends on the first observation.
- Set MAX_STEPS to 2 and confirm the stop rule ends the run.
Done when
- The trace shows two actions, two observations and a final answer.
- The answer names the expired card, which came from an observation, not from memory.
- With MAX_STEPS = 2 the run prints 'Stopped: step limit reached'.
Open in the Playground No API key needed.
Starter code (Python)
# A ReAct loop: think, act, observe, repeat, with a step limit as the stop rule.
TOOLS = {
"get_invoice": lambda id: {"id": id, "status": "unpaid", "due": "2026-10-01", "customer": 42},
"get_payment_method": lambda customer: {"customer": customer, "card": "expired 2026-09"},
}
def think(question, observations):
"""Simulated model: picks the next step from what it has observed so far."""
if not observations:
return ("I should look up the invoice.", "get_invoice", {"id": 881})
last = observations[-1]
if last.get("status") == "unpaid":
return ("It is unpaid. Check the customer's payment method.", "get_payment_method", {"customer": last["customer"]})
if "card" in last:
return ("The card expired before the due date. That explains it.", "answer",
"Invoice 881 is overdue because the card on file expired in September.")
return ("I am stuck.", "answer", "I could not find the cause.")
MAX_STEPS = 5
def react(question):
observations = []
for step in range(1, MAX_STEPS + 1):
thought, action, arg = think(question, observations)
print(f"Thought: {thought}")
# TODO: if action is "answer", print "Answer: <arg>" and "steps used: <step>", then return arg
# TODO: otherwise run TOOLS[action](**arg), print "Action:" and "Observation:", and append the result
break
print("Stopped: step limit reached")
react("Why is invoice 881 overdue?")
Show a solution
# A ReAct loop: think, act, observe, repeat, with a step limit as the stop rule.
TOOLS = {
"get_invoice": lambda id: {"id": id, "status": "unpaid", "due": "2026-10-01", "customer": 42},
"get_payment_method": lambda customer: {"customer": customer, "card": "expired 2026-09"},
}
def think(question, observations):
"""Simulated model: picks the next step from what it has observed so far."""
if not observations:
return ("I should look up the invoice.", "get_invoice", {"id": 881})
last = observations[-1]
if last.get("status") == "unpaid":
return ("It is unpaid. Check the customer's payment method.", "get_payment_method", {"customer": last["customer"]})
if "card" in last:
return ("The card expired before the due date. That explains it.", "answer",
"Invoice 881 is overdue because the card on file expired in September.")
return ("I am stuck.", "answer", "I could not find the cause.")
MAX_STEPS = 5
def react(question):
observations = []
for step in range(1, MAX_STEPS + 1):
thought, action, arg = think(question, observations)
print(f"Thought: {thought}")
if action == "answer":
print(f"Answer: {arg}")
print(f"steps used: {step}")
return arg
result = TOOLS[action](**arg)
print(f"Action: {action}({arg})")
print(f"Observation: {result}")
observations.append(result)
print("Stopped: step limit reached")
react("Why is invoice 881 overdue?")