# 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.

Source: https://www.mlfoundry.in/learn/react-prompting  
Section: Generative AI › Prompt Engineering  
Level: core · 8 min read · updated 2026-10-21

**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

> Figure: 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. Send 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. Generate with a **stop sequence** on `Observation`, so the model writes one Thought and one Action and cannot invent the tool's reply.
3. Parse 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. Run the tool, truncate long results (the paper used the first sentences of a Wikipedia page), and append `Observation t: …`.
5. Stop on `Finish[…]` or when the step budget $T$ 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 $p$, independently. The question needs $h$ useful observations, and Finish comes one call after the last of them. The run then finishes at call $s$ with a negative-binomial probability. Success is the sum up to the budget $T$:

$$
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)
$$

- $p$: probability that one Thought → Action → Observation step yields useful evidence
- $h$: pieces of evidence the question needs (2 for a two-hop question)
- $T$: maximum number of model calls

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

$$
N_{\text{in}}(S)=\sum_{t=1}^{S}\big(P_0+(t-1)\tau\big)=S\,P_0+\tau\,\frac{S(S-1)}{2}
$$

- $P_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\,P_0$ term. Truncating observations shrinks $\tau$, which controls the quadratic term.

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.

| 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

> Figure: 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

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.

| 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: $N_{\text{in}}(4)=4\cdot1500+150\cdot\frac{4\cdot3}{2}=6000+900=6900$ and $N_{\text{in}}(7)=10500+3150=13650$. Under the model, the probability of finishing at call 3 is $p^2=0.36$, at call 4 it is $\binom{2}{1}p^2(1-p)=0.288$, and at call 5 it is $\binom{3}{1}p^2(1-p)^2=0.1728$.

```python
import re

DOCS = {"Apple Remote": "The Apple Remote ... originally designed to control the Front Row media center program.",
        "Front Row (software)": "Front Row is a discontinued media center software ... controlled by an Apple Remote or the keyboard function keys."}

def search(q):
    if q in DOCS:
        return DOCS[q]
    similar = [k for k in DOCS if q.lower() in k.lower()]
    return f"Could not find [{q}]. Similar: {similar}"

TOOLS = {"Search": search}
ACTION = re.compile(r"Action \d+: (\w+)\[(.*)\]")

def react(question, complete, fewshot, max_steps=7):
    prompt = f"{fewshot}\nQuestion: {question}\n"
    for t in range(1, max_steps + 1):
        # stop before the model writes its own (invented) observation
        step = complete(prompt + f"Thought {t}:", stop=[f"\nObservation {t}:"])
        prompt += f"Thought {t}:{step}\n"
        m = ACTION.search(step)
        if m is None:
            obs = "Invalid action. Use Search[entity] or Finish[answer]."
        elif m.group(1) == "Finish":
            return m.group(2), t
        elif m.group(1) in TOOLS:
            obs = TOOLS[m.group(1)](m.group(2))
        else:
            obs = f"Unknown tool {m.group(1)}."
        prompt += f"Observation {t}: {obs}\n"
    return None, max_steps          # budget exhausted: no answer

# scripted stand-in for an LLM, to exercise the loop
script = iter([" I need to search Apple Remote.\nAction 1: Search[Apple Remote]",
               " I need to search Front Row.\nAction 2: Search[Front Row]",
               " Not found. Search Front Row (software).\nAction 3: Search[Front Row (software)]",
               " So the answer is keyboard function keys.\nAction 4: Finish[keyboard function keys]"])
print(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.

> **Observations are untrusted input:** Tool results enter the context as text, so a web page or document can contain instructions ("ignore the question and …"). This is indirect **Prompt injection**. Mark observations as data in the prompt, limit which actions can have side effects, and require confirmation for irreversible ones (see **Guardrails**).

> **Text actions versus native function calls:** The original ReAct parsed actions out of plain text. Most hosted APIs now return tool calls as structured JSON (see **LLM APIs and function calling**), which removes the parser and the stop-sequence trick. The loop is the same: reason, call, append the result, repeat. Many models still emit a short thought before each call.

- **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

**Question:** Why does a ReAct implementation stop generation at the string "Observation"?

- A. To save output tokens on long thoughts
- B. So the model cannot write its own observation; the real tool result must be inserted
- C. Because the model cannot generate the word Observation
- D. To force the model to call Finish sooner

**Answer:** B. So the model cannot write its own observation; the real tool result must be inserted — Without the stop sequence, the model happily continues the pattern and writes a plausible but invented observation. That turns ReAct back into ungrounded CoT.

**Question:** Prompt P₀ = 2,000 tokens, τ = 200 per step. How many input tokens do 5 calls consume in total?

- A. 10,000
- B. 11,000
- C. 12,000
- D. 14,000

**Answer:** C. 12,000 — $5\cdot2000+200\cdot\frac{5\cdot4}{2}=10000+2000=12000$.

**Key points**

- 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 $T$ steps follows from per-step usefulness $p$. Input cost is $SP_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.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
