Definition
ReAct prompting is a few-shot prompting format in which a language model alternates free-text reasoning ("Thought"), a structured tool call ("Action"), and the tool's result ("Observation"), which the surrounding program inserts. The loop runs until the model emits a finishing action. Yao et al. introduced it in "ReAct: Synergizing Reasoning and Acting in Language Models" (ICLR 2023), using PaLM-540B with a Wikipedia API on HotpotQA and FEVER and text environments in ALFWorld and WebShop.
Chain-of-thought prompting reasons only over what the model already knows. When a fact is missing or out of date, the chain fills the gap with a plausible guess (see Hallucination). An action-only agent has the opposite problem. It can query tools, but it has no place to plan, track progress or notice that a search failed.
ReAct puts both in one trajectory. Thoughts only change the context. Actions change the world or fetch evidence. Observations always come from the tool, never from the model. Today this is the core of most agent loops (see The agent loop and ReAct in the Agentic AI branch). This article covers the prompting pattern itself.
The loop
One iteration
- 1Send the prompt: instructions, the action space (the paper's Wikipedia tools were
search[entity],lookup[string]andfinish[answer]), a few exemplar trajectories, the question, and the trajectory so far. - 2Generate with a stop sequence on
Observation, so the model writes one Thought and one Action and cannot invent the tool's reply. - 3Parse the Action with a strict pattern such as
(\w+)\[(.*)\]. If it is malformed or names an unknown tool, return an error string as the observation instead of crashing. - 4Run the tool, truncate long results (the paper used the first sentences of a Wikipedia page), and append
Observation t: …. - 5Stop on
Finish[…]or when the step budget runs out. In the second case, fall back (for example, answer with plain CoT) or return "unknown".
Success and cost of a bounded loop
A simple model makes the budget explicit. Suppose each step returns useful evidence with probability , independently. The question needs useful observations, and Finish comes one call after the last of them. The run then finishes at call with a negative-binomial probability. Success is the sum up to the budget :
- probability that one Thought → Action → Observation step yields useful evidence
- pieces of evidence the question needs (2 for a two-hop question)
- maximum number of model calls
Each call re-sends the prompt and the whole trajectory. With a fixed prompt of tokens and tokens added per step (thought, action, observation), call reads input tokens. The total over calls is:
- instructions + tool descriptions + exemplars + question
- tokens per step; long observations dominate it
The fixed prefix is identical on every call, so Prompt caching removes most of the term. Truncating observations shrinks , which controls the quadratic term.
| Benchmark | Baseline | ReAct | Note |
|---|---|---|---|
| HotpotQA | CoT 29.4 | 27.4 | best was a hybrid: CoT-SC, then ReAct when votes disagree, 35.1 |
| FEVER | CoT 56.3 | 60.9 | retrieval matters more for fact verification |
| ALFWorld | Act-only 45 | 71 | thoughts track subgoals in long tasks |
| WebShop | imitation learning 29.1 | 40 | one-shot ReAct against a trained baseline |
ReAct alone does not beat CoT on HotpotQA. Its trajectories are grounded and rarely contain made-up facts, but a bad search result can derail them. In the paper's error analysis, CoT's dominant failure was hallucinated facts and ReAct's was uninformative searches and repeated steps. Combining the two worked best.
Simulate the budget
Worked example
| Call t | Input tokens P₀ + (t−1)τ | Cumulative |
|---|---|---|
| 1 | 1,500 | 1,500 |
| 2 | 1,650 | 3,150 |
| 3 | 1,800 | 4,950 |
| 4 | 1,950 | 6,900 |
| 7 (budget) | 2,400 | 13,650 |
The closed form agrees: and . Under the model, the probability of finishing at call 3 is , at call 4 it is , and at call 5 it is .
1import re23DOCS = {"Apple Remote": "The Apple Remote ... originally designed to control the Front Row media center program.",4 "Front Row (software)": "Front Row is a discontinued media center software ... controlled by an Apple Remote or the keyboard function keys."}56def search(q):7 if q in DOCS:8 return DOCS[q]9 similar = [k for k in DOCS if q.lower() in k.lower()]10 return f"Could not find [{q}]. Similar: {similar}"1112TOOLS = {"Search": search}13ACTION = re.compile(r"Action \d+: (\w+)\[(.*)\]")1415def react(question, complete, fewshot, max_steps=7):16 prompt = f"{fewshot}\nQuestion: {question}\n"17 for t in range(1, max_steps + 1):18 # stop before the model writes its own (invented) observation19 step = complete(prompt + f"Thought {t}:", stop=[f"\nObservation {t}:"])20 prompt += f"Thought {t}:{step}\n"21 m = ACTION.search(step)22 if m is None:23 obs = "Invalid action. Use Search[entity] or Finish[answer]."24 elif m.group(1) == "Finish":25 return m.group(2), t26 elif m.group(1) in TOOLS:27 obs = TOOLS[m.group(1)](m.group(2))28 else:29 obs = f"Unknown tool {m.group(1)}."30 prompt += f"Observation {t}: {obs}\n"31 return None, max_steps # budget exhausted: no answer3233# scripted stand-in for an LLM, to exercise the loop34script = iter([" I need to search Apple Remote.\nAction 1: Search[Apple Remote]",35 " I need to search Front Row.\nAction 2: Search[Front Row]",36 " Not found. Search Front Row (software).\nAction 3: Search[Front Row (software)]",37 " So the answer is keyboard function keys.\nAction 4: Finish[keyboard function keys]"])38print(react("Aside from the Apple Remote, what else controls Front Row?", lambda p, stop: next(script), ""))Where it's used
Multi-hop question answering
search, read, search again with the entity just found; see Agentic RAG
Fact checking
retrieve evidence before the verdict, as on FEVER
Interactive tasks
text games, web shopping and form filling, where thoughts track subgoals across many steps
Coding agents
run tests or a linter as the action and read the output as the observation
Check yourself
Why does a ReAct implementation stop generation at the string "Observation"?
Check yourself
Prompt P₀ = 2,000 tokens, τ = 200 per step. How many input tokens do 5 calls consume in total?
Remember
- ✓ReAct interleaves Thought, Action and Observation. Thoughts plan and repair, actions fetch evidence, observations come only from tools.
- ✓A stop sequence at Observation, a strict action parser and a step budget are the three pieces every implementation needs.
- ✓Success within steps follows from per-step usefulness . Input cost is , quadratic in the number of steps.
- ✓On knowledge tasks ReAct is grounded but brittle to bad searches. Combining it with CoT and self-consistency gave the best HotpotQA and FEVER results.
- ✓Treat observations as untrusted text, since they are an injection channel.