A model answering only from its internal knowledge will either compute wrong or invent facts. ReAct (reasoning + acting) alternates the two: the model writes a reasoning step, calls an external tool, and feeds what it finds back into the reasoning. This page shows what ReAct prompts look like, where they actually help, and how to assemble a ReAct agent that can search and calculate with LangChain.
What ReAct is
ReAct, introduced by Yao et al. (2022), is a framework in which LLMs generate reasoning traces and task-specific actions in an interleaved manner [1]. The reasoning traces let the model induce, track, and update action plans, and even handle exception cases; the actions let it interact with external sources such as knowledge bases or environments to gather information [1].
The relationship with chain-of-thought (CoT) is worth spelling out. CoT is reasoning only: the model has no contact with the external world and cannot update its knowledge, which leads to fact hallucination and error propagation [1]. ReAct combines reasoning with acting — reasoning decides what to look up next, and retrieved information supports the next reasoning step [1]. The authors found that the best way to combine ReAct with CoT is to use internal knowledge and retrieved external information together during reasoning [1].
What a ReAct prompt looks like
A ReAct trajectory consists of multiple thought-action-observation steps. Free-form thoughts decompose the question, extract information, perform commonsense or arithmetic reasoning, guide search formulation, and synthesize the final answer; actions call external tools; observations are what the tools return [1]. A HotpotQA example, shortened to one case:
问题 科罗拉多造山带东部区域延伸到的区域的海拔范围是多少?
思考 1 我需要搜索科罗拉多造山带,找到科罗拉多造山带东段延伸的区域,然后找到该区域的海拔范围。
操作 1 搜索 [科罗拉多造山带]
观察 1 科罗拉多造山带是科罗拉多及其周边地区造山运动的一段。
思考 2 它没有提到东部地区。所以我需要查一下东部地区。
操作 2 查找 [东部地区]
观察 2 (结果 1 / 1) 东部区域延伸至高平原,称为中原造山带。
思考 3 科罗拉多造山运动的东部区域延伸到高平原。所以我需要搜索高平原,找到它的海拔范围。
操作 3 搜索 [高平原]
观察 3 高平原指的是两个截然不同的陆地区域之一。
思考 4 我需要搜索高平原(美国)
操作 4 搜索 [高平原(美国)]
观察 4 高平原是大平原的一个分区。从东到西,高平原的海拔从 1800 到 7000 英尺 (550 到 2130 米) 不等。[3]
思考 5 高平原海拔高度从 1800 到 7000 英尺,所以答案是 1800 到 7000 英尺。
操作 5 结束 [1800 到 7000 英尺]
...The model first searches for the Colorado orogeny, notices the east is not mentioned, looks up the eastern zone, learns it extends to the High Plains, searches their elevation range, and finally answers "1800 to 7000 英尺" and stops [1]. Information flows in both directions: retrieval feeds reasoning, and reasoning determines what to retrieve next [1].
Prompt setups differ by task type: for reasoning-heavy tasks such as HotpotQA, trajectories use many thought-action-observation steps; for decision tasks with many actions, fewer thoughts are used [1]. These trajectories go into the prompt as few-shot examples.
How well does it work
On knowledge-intensive tasks, with PaLM-540B as the base model, prompting methods were compared on HotPotQA question answering and Fever fact verification [1]:
- ReAct generally outperforms Act, the actions-only variant;
- ReAct beats CoT on Fever but trails CoT on HotpotQA.
The error analysis gives three reasons: CoT suffers from fact hallucination; ReAct's structural constraint reduces its flexibility in devising reasoning steps; and ReAct depends heavily on the information it retrieves — uninformative search results derail the reasoning and are hard to recover from [1]. Prompting methods that combine and switch between ReAct and chain-of-thought plus self-consistency generally outperform all other methods [1].
On decision-making tasks, ReAct was evaluated on ALFWorld, a text-based game, and WebShop, an online shopping environment, and outperforms Act on both — without thoughts, Act cannot correctly decompose goals into subgoals [1]. Prompting-based methods still lag far behind human experts on these tasks, however [1].
Building a ReAct agent with LangChain
ReAct is not just a paper pattern; LangChain has built-in support for building agents around it. Below, an agent that can search and do arithmetic. Install and import the necessary libraries [1]:
%%capture
# 更新或安装必要的库
!pip install --upgrade openai
!pip install --upgrade langchain
!pip install --upgrade python-dotenv
!pip install google-search-results
# 引入库
import openai
import os
from langchain.llms import OpenAI
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from dotenv import load_dotenv
load_dotenv()
# 载入 API keys; 如果没有,你需要先获取。
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
Then configure the LLM, the tools, and the agent: a search API for external information, and the LLM itself as the math tool [1]:
llm = OpenAI(model_name="text-davinci-003" ,temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)After configuration, run it with a query; no few-shot examples are provided here [1]:
agent.run("奥利维亚·王尔德的男朋友是谁?他现在的年龄的0.23次方是多少?")The chain executes like this [1]:
> 正在输入新代理执行器链......
我得查出奥利维亚·王尔德的男友是谁然后计算出他的年龄的 0.23 次方。
操作: 搜索
操作输入: “奥利维亚·王尔德的男友”
观察: 奥利维亚·王尔德与杰森·苏代基斯在多年前订婚,在他们分手后,她开始与哈里·斯泰尔斯约会 — 参照他们的关系时间线。
思考: 我需要找出哈里·斯泰尔斯的年龄。
操作: 搜索
操作输入: “哈里·斯泰尔斯的年龄”
观察: 29 岁
思考: 我需要计算 29 的 0.23 次方。
操作: 计算器
操作输入: 29^0.23
观察: 答案: 2.169459462491557
思考: 现在我知道最终答案了。
最终答案: 哈里·斯泰尔斯, 奥利维亚·王尔德的男朋友, 29 岁。他年龄的 0.23 次方是 2.169459462491557。
> 结束链。The agent first searches and finds the boyfriend, then searches his age, calls the calculator for 29 to the power of 0.23, and synthesizes the final answer from both steps [1]. Note that every action is backed by an explicit thought — that is the ReAct structure in action.
When to use ReAct
A practical order: if the task can be done from internal knowledge, CoT is enough; if the answer depends on facts that need looking up, use ReAct; combined prompting that switches between the two works best but is the most complex to implement [1]. Accept its limitation too: agent quality tracks retrieval quality, and unreliable search results derail the whole chain [1].
Adapted from the ReAct framework section of DAIR.AI's *Prompt Engineering Guide*[1], released under the MIT License.
Sources
- 1ReAct 框架 | Prompt Engineering GuideDAIR.AI Prompt Engineering Guide (MIT)
Cite this page: ReAct prompting. TellWorld, https://www.tellworld.net/practice/practice-prompt-react. Updated 2026-11-14