What is ReAct Prompting? A Complete Guide to Reasoning + Acting
ReAct prompting is a paradigm that interleaves reasoning traces (thoughts) with task-specific actions (tool calls) in a dynamic loop of Thought → Action → Observation steps until a final answer is reached.
Introduced by Yao et al. (2022), ReAct (Reasoning + Acting) is documented extensively in the dair-ai/Prompt-Engineering-Guide repository. This prompting technique enables large language models to solve complex tasks by alternating between internal reasoning and external tool usage, creating an interpretable trajectory of decisions and actions.
How ReAct Prompting Works
According to the source documentation in pages/techniques/react.en.mdx, ReAct operates through an iterative cycle that combines cognitive reasoning with environmental interaction. The architecture follows a strict pattern where the model generates a thought, decides on an action, receives an observation, and repeats until task completion.
The Six-Step Execution Flow
The implementation described in the guide follows this precise sequence:
-
Few-shot exemplars – Training examples are formatted as trajectories containing alternating
Thought,Action, andObservationlines (e.g.,Thought 1 …,Action 1 Search[…],Observation 1 …). -
Prompt construction – These exemplars are prepended to the prompt alongside the new user query.
-
LLM generation – The model emits the next
Thought(reasoning step) followed by anActiontoken specifying a tool such asSearch,Lookup, orCalculator. -
Tool execution – The system executes the requested tool, captures the result as an
Observation, and appends it to the prompt context. -
Iterative loop – Steps 3-4 repeat, allowing the model to refine its plan, fetch additional information, or backtrack when observations contradict previous assumptions.
-
Finish action – When the model outputs
Action n Finish[…], the loop terminates and the final answer is returned.
Visualizing the ReAct Trajectory
The repository includes a visual representation at img/react.png that illustrates this cycle for complex question-answering tasks. The diagram demonstrates how reasoning traces and action calls interleave to form a coherent problem-solving path.
Architectural Benefits of ReAct Prompting
The pages/techniques/react.en.mdx file outlines four primary advantages of this approach:
-
Reduced hallucination – External tool calls ground the reasoning in real data, preventing the model from generating unsupported speculations.
-
Interpretability – The interleaved
Thought/Actionlog creates an explicit reasoning trace that developers can inspect and debug. -
Flexibility – Any tool that can be wrapped (search engines, calculators, database queries, or API calls) integrates seamlessly, making the approach domain-agnostic.
-
Synergy with CoT – Combining Chain-of-Thought (deep internal reasoning) with ReAct (external grounding) yields superior performance on both knowledge-intensive and decision-making tasks.
Practical Implementation with LangChain
The repository provides executable examples in notebooks/react.ipynb demonstrating how to implement ReAct agents using LangChain. Below is a minimal, runnable example that instantiates a ReAct-style agent with search and calculation capabilities.
# Install required packages (run once)
# !pip install --quiet openai langchain python-dotenv google-search-results
import os
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, load_tools
from dotenv import load_dotenv
load_dotenv() # loads OPENAI_API_KEY and SERPER_API_KEY from .env
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
# Configure LLM and tools
llm = OpenAI(model_name="text-davinci-003", temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)
# Initialise a zero‑shot ReAct agent
agent = initialize_agent(
tools, llm,
agent="zero-shot-react-description", # selects the ReAct prompting style
verbose=True
)
# Run the agent
result = agent.run(
"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"
)
print(result)
Execution Trace Analysis
When the above code runs, the agent executes the following trajectory:
- Issues a
Searchaction for "Olivia Wilde boyfriend" - Receives an observation containing the name (e.g., Harry Styles)
- Performs a second
Searchto retrieve Harry Styles' age - Uses the
Calculatortool to computeage^0.23 - Returns the final synthesized answer
Key Source Files and Resources
The dair-ai/Prompt-Engineering-Guide repository contains several authoritative sources for understanding ReAct prompting:
-
pages/techniques/react.en.mdx– Comprehensive documentation covering theory, few-shot examples, and implementation details. -
notebooks/react.ipynb– Executable Jupyter notebook demonstrating the LangChain ReAct agent in practice. -
img/react.png– Visual illustration of the ReAct reasoning-action loop. -
img/react/table1.png– Performance comparison table showing ReAct versus other prompting strategies. -
img/react/alfworld.png– Screenshots demonstrating ReAct applied to decision-making tasks in ALFWorld.
Summary
- ReAct prompting combines reasoning traces (Thought) with tool execution (Action) to solve complex tasks through an iterative loop.
- The paradigm follows a strict Thought → Action → Observation pattern until the model emits a Finish action.
- Key benefits include reduced hallucination, improved interpretability, and seamless integration with external tools.
- Implementation is straightforward using frameworks like LangChain with the
zero-shot-react-descriptionagent type. - Official documentation and examples reside in the
pages/techniques/react.en.mdxfile andnotebooks/react.ipynbnotebook within the dair-ai/Prompt-Engineering-Guide repository.
Frequently Asked Questions
How does ReAct prompting differ from Chain-of-Thought?
While Chain-of-Thought (CoT) prompts the model to show its reasoning internally, ReAct prompting extends this by allowing the model to take actions in the environment. ReAct interleaves reasoning traces with external tool calls, whereas CoT relies solely on the model's parametric knowledge.
What tools can be used with ReAct agents?
Any tool that can be wrapped with a text-based interface works with ReAct. Common examples include web search APIs (Search), calculators (Calculator), database query engines, code interpreters, and custom APIs. The flexibility to integrate arbitrary tools makes ReAct adaptable to domain-specific applications.
When should I use ReAct prompting instead of standard prompting?
Use ReAct prompting when tasks require up-to-date information not in the model's training data, complex calculations, or multi-step reasoning that benefits from external verification. It is particularly effective for open-domain question answering, tool-augmented assistants, and interactive decision-making environments like ALFWorld or WebShop.
Where can I find the official ReAct documentation and examples?
The authoritative documentation lives in pages/techniques/react.en.mdx within the dair-ai/Prompt-Engineering-Guide repository. For executable examples, refer to notebooks/react.ipynb, which contains a complete LangChain implementation demonstrating the Thought-Action-Observation loop.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →