ReAct is a few-shot prompting framework that interleaves a language model’s reasoning with external actions and observations. Instead of answering in one step, the model identifies an action, observes its result, reasons about what remains unknown, and chooses the next action. section 7.4.2
A typical ReAct loop contains:
- A question or task that establishes the goal. section 7.4.2
- A thought that identifies missing information or the next decision. section 7.4.2
- An action such as Search, Lookup, or Calculator. section 7.4.2
- An observation that feeds the next reasoning step, followed by a final answer when the task is complete. section 7.4.2
From lookup to calculation
A ReAct agent can search for Olivia Wilde’s boyfriend, search for Harry Styles’ age, and then use a calculator to raise 29 to the 0.23 power. Each result determines the next action rather than requiring every step in advance. section 7.4.2
Listing 7.5 Example ReAct with Langchain
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()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") #1
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
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
)
agent.run(
"Who is Olivia Wilde's boyfriend? \
What is his current age raised to the 0.23 power?"
)
A compact LangChain setup connects an OpenAI model to Google Serper search and an LLM-powered math tool through a zero-shot ReAct agent. section 7.4.2










