14 · ReAct: reason + act
ReAct is the pattern behind most agents: interleave a Thought, an Action, and an Observation each turn, until the model writes a Final Answer. The explicit thinking is what makes multi-step tool use reliable.
ReAct (Reason + Act) structures the agent loop so each turn produces a Thought (reasoning in words), then an Action (a tool call), then an Observation (the tool result) — looping until the model emits a Final Answer. Writing the reasoning out loud makes multi-step tool use far more reliable.
Without this:
Without the explicit Thought step, the model jumps straight to actions and skips steps on multi-hop questions; without the Observation step it can't react to what a tool actually returned.
ReAct ("Reason + Act") is the most influential agent pattern, and it's a tiny refinement of the loop you already built. The idea: make the model write its reasoning out loud before each action. Every turn has a fixed shape:
- Thought — the model says, in plain language, what it's trying to do next and why.
- Action — it calls a tool (name + arguments), chosen based on that thought.
- Observation — your code runs the tool and feeds the result back.
The loop repeats — Thought, Action, Observation, Thought, Action, Observation — until the model decides it has enough and writes a Final Answer instead of another action.
Why does the explicit Thought matter? Two reasons:
- Reliability. Forcing the model to reason in words before acting (the same effect as chain-of-thought) dramatically reduces skipped steps on multi-hop questions — ones that need several tool calls, where each step depends on the previous result.
- Debuggability. The Thought/Action/Observation trace is a complete, human-readable log of why the agent did what it did. When an agent goes wrong, this trace is the first thing you read.
Below is a mini ReAct agent answering a 2-step question: "What is the population of France times 3?" It must first look up the population, then multiply — it can't do both at once. The policy is rule-based (a stand-in for an LLM) so the whole trace runs deterministically offline. Watch the scratchpad (memory) carry the population from step 1 into the calculation in step 2.
Python (in browser)
A full ReAct trace: Thought→Action→Observation twice (look up population, then multiply) before the Final Answer. The scratchpad carries facts forward — that's how it solves a multi-step question.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
In a real ReAct agent the policy function is replaced by an LLM, and the Thought/Action/Observation format is taught to the model with a prompt (or enforced via function calling). Each turn you append the new Observation to the prompt and call the model again — the same loop, with a real reasoner. Read the LangChain-style version below; under the hood it runs the exact trace you just saw.
A real LangChain ReAct agent: an LLM as the reasoner, decorated functions as tools, a hub prompt for the Thought/Action/Observation format, and `max_iterations` as the step guard — the same loop, automated.
In ReAct, why is the explicit 'Thought' step (reasoning written out before each action) valuable?
- ReAct = interleave Thought (reason in words) → Action (tool call) → Observation (result), looping until a Final Answer.
- The explicit Thought (chain-of-thought) makes multi-step / multi-hop tool use far more reliable.
- The Thought/Action/Observation trace is a complete, human-readable log — your first stop when debugging an agent.
ReAct is the default agent pattern in LangChain, LlamaIndex, and most research agents; the Thought/Action/Observation format is what `verbose=True` prints and what observability tools (LangSmith) visualize.
If you remove it: Drop the Thought and the agent acts impulsively, skipping steps on multi-hop questions; drop the Observation loop and it can't adapt to what tools actually returned — both collapse it back to a brittle one-shot call.