ReAct prompting

ReAct (Yao et al., ICLR 2023) interleaves Thought, Action and Observation lines: the model reasons, calls a tool, reads the real result and repeats until Finish[answer]; every step re-sends the whole trajectory, so cost grows quadratically.

8 min read708 wordscore0 viewsupdated 2026-10-21

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

the loopLLMfew-shot ReAct promptyour codestop at Observation:tool / envsearch · lookupappendobs → contextaction textparsed callresult textrepeat untilFinish[...] orthe step budgetone trajectory (HotpotQA; adapted from Yao et al. 2022, Fig. 1)QuestionAside from the Apple Remote, what other device can controlthe program Apple Remote was originally designed to interact with?Thought 1I need to search Apple Remote and find the program it wasoriginally designed to interact with.Action 1Search[Apple Remote]Obs 1The Apple Remote is a remote control … originally designed tocontrol the Front Row media center program …Thought 2It was designed to control Front Row. I need to search Front Row.Action 2Search[Front Row]Obs 2Could not find [Front Row]. Similar: ['Front Row Seat to Earth','Front Row Motorsports', 'Front Row (software)', …]Thought 3Front Row is not found. I need to search Front Row (software).Action 3Search[Front Row (software)]Obs 3Front Row is a discontinued media center software … controlledby an Apple Remote or the keyboard function keys.Thought 4So the answer is keyboard function keys.Action 4Finish[keyboard function keys]thoughts change only the context · actions hit the tool ·observations come from the tool, never from the modelObs 2 → Thought 3: a failed search is read and repaired
fig.Left: the control loop. The program calls the model with a stop sequence at "Observation", parses the action, runs the tool and appends the result. Right: a HotpotQA trajectory adapted from Yao et al. (2022), Figure 1, with observations shortened. Search[Front Row] fails and returns suggestions; Thought 3 reads that and repairs the query; the fourth step is Finish[keyboard function keys].

One iteration

  1. 1Send the prompt: instructions, the action space (the paper's Wikipedia tools were search[entity], lookup[string] and finish[answer]), a few exemplar trajectories, the question, and the trajectory so far.
  2. 2Generate with a stop sequence on Observation, so the model writes one Thought and one Action and cannot invent the tool's reply.
  3. 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.
  4. 4Run the tool, truncate long results (the paper used the first sentences of a Wikipedia page), and append Observation t: ….
  5. 5Stop on Finish[…] or when the step budget TT 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 pp, independently. The question needs hh useful observations, and Finish comes one call after the last of them. The run then finishes at call ss with a negative-binomial probability. Success is the sum up to the budget TT:

P(finish at s)=(s−2h−1)ph(1−p)s−1−h,P(success)=∑s=h+1TP(finish at s)P(\text{finish at }s)=\binom{s-2}{h-1}p^{h}(1-p)^{s-1-h},\qquad P(\text{success})=\sum_{s=h+1}^{T}P(\text{finish at }s)
pp
probability that one Thought → Action → Observation step yields useful evidence
hh
pieces of evidence the question needs (2 for a two-hop question)
TT
maximum number of model calls

Each call re-sends the prompt and the whole trajectory. With a fixed prompt of P0P_0 tokens and τ\tau tokens added per step (thought, action, observation), call tt reads P0+(t−1)τP_0+(t-1)\tau input tokens. The total over SS calls is:

Nin(S)=∑t=1S(P0+(t−1)τ)=S P0+τ S(S−1)2N_{\text{in}}(S)=\sum_{t=1}^{S}\big(P_0+(t-1)\tau\big)=S\,P_0+\tau\,\frac{S(S-1)}{2}
P0P_0
instructions + tool descriptions + exemplars + question
τ\tau
tokens per step; long observations dominate it

The fixed prefix is identical on every call, so Prompt caching removes most of the S P0S\,P_0 term. Truncating observations shrinks τ\tau, which controls the quadratic term.

BenchmarkBaselineReActNote
HotpotQACoT 29.427.4best was a hybrid: CoT-SC, then ReAct when votes disagree, 35.1
FEVERCoT 56.360.9retrieval matters more for fact verification
ALFWorldAct-only 4571thoughts track subgoals in long tasks
WebShopimitation learning 29.140one-shot ReAct against a trained baseline
Selected results from Yao et al. (PaLM-540B, few-shot prompting). HotpotQA: exact match; FEVER: accuracy; ALFWorld and WebShop: success rate. CoT-SC is CoT with self-consistency.

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

ReAct loop: each Thought/Action/Observation is useful w.p. 0.60; 2 facts neededFinish[answer] comes one call after the last useful observation; the loop stops after T = 7 callsP(finish at call s) and cumulative success00.51123456789101112131415cutoffmodel call s (bars scaled to the tallest)input tokens per call: 1500 prompt + 150 per earlier stepcall 1: 1500 tokens … call 7: 2400 tokensanswered within T95.9%cut off, no answer4.1%expected model calls4.26expected input tokens7,549worst case (T calls)13,650total over S calls =S·P₀ + τ·S(S−1)/2: quadratic in Sa model, not a benchmark: pstands for search quality andhow well thoughts recoverfrom a failed observation
fig.The model above with P₀ = 1,500 tokens. Default (p = 0.6, h = 2, T = 7, τ = 150): 95.9% of runs answer within the budget, 4.1% are cut off; the expected run takes 4.26 calls and 7,549 input tokens, while a run that hits the budget costs 13,650. With T = 3 only 36.0% finish. With p = 0.3 (weak search) success falls to 58.0% at T = 7 and expected tokens rise to 11,105.

Worked example

Call tInput tokens P₀ + (t−1)τCumulative
11,5001,500
21,6503,150
31,8004,950
41,9506,900
7 (budget)2,40013,650
Input tokens per call for P₀ = 1,500 and τ = 150. A two-hop question that succeeds at the first try of each search finishes at call 3.

The closed form agrees: Nin(4)=4⋅1500+150⋅4⋅32=6000+900=6900N_{\text{in}}(4)=4\cdot1500+150\cdot\frac{4\cdot3}{2}=6000+900=6900 and Nin(7)=10500+3150=13650N_{\text{in}}(7)=10500+3150=13650. Under the model, the probability of finishing at call 3 is p2=0.36p^2=0.36, at call 4 it is (21)p2(1−p)=0.288\binom{2}{1}p^2(1-p)=0.288, and at call 5 it is (31)p2(1−p)2=0.1728\binom{3}{1}p^2(1-p)^2=0.1728.

python
1import re
2
3DOCS = {"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."}
5
6def 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}"
11
12TOOLS = {"Search": search}
13ACTION = re.compile(r"Action \d+: (\w+)\[(.*)\]")
14
15def 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) observation
19 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), t
26 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 answer
32
33# scripted stand-in for an LLM, to exercise the loop
34script = 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), ""))
A minimal ReAct loop with a stop sequence, a strict action parser and a step budget. It was run with the scripted stand-in LLM shown and returns ('keyboard function keys', 4); swap `complete` for a real completion call.

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 TT steps follows from per-step usefulness pp. Input cost is SP0+τS(S−1)/2SP_0+\tau S(S-1)/2, 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.
← back to Generative AI roadmap