ReAct: Combining Reasoning and Acting
What if an LLM could not only think, but also do? The ReAct framework is a revolutionary step in this direction, transforming language models from passive text generators into active agents that can reason, act, and learn from their environment.
Introduction
So far, the reasoning techniques we've explored have been "internal." They take place entirely within the context of the LLM's own thought processes. But what if the model needs information it doesn't have? What if it needs to perform a calculation, search for a recent event, or interact with a piece of software?
ReAct, which stands for "Reason and Act," is a powerful framework that addresses this limitation. It provides a structured way for an LLM to interleave its reasoning process with actions that interact with the outside world. This is the foundational concept behind most modern AI agents and tool-using systems.
The Core Idea: Thought, Action, Observation
The ReAct framework is an iterative loop that mimics how humans often solve problems: we think about what to do, we take an action, and we observe the result to inform our next thought.
The prompt structure in ReAct is designed to make this loop explicit. At each step, the model is prompted to generate a response that contains three components:
- Thought: The model's internal reasoning. It analyzes the current situation, formulates a plan, and decides what to do next. This is a private "inner monologue."
- Action: A specific, executable command that the model wants to perform. This is often a call to an external tool, like a search engine, a calculator, or an API.
- Observation: After the action is executed by the system, the result (the "observation") is fed back into the prompt for the next iteration.
This loop continues until the model determines that it has enough information to provide a final answer.
A Practical Example: Answering a Question with a Search Engine
Problem: "What is the current weather in Paris, and what was the score of the latest Paris Saint-Germain soccer match?"
An LLM cannot answer this from its internal knowledge, as both pieces of information are real-time. A ReAct agent would tackle this as follows:
Iteration 1:
Prompt:
Question: What is the current weather in Paris, and what was the score of the latest Paris Saint-Germain soccer match?
LLM Output:
Thought: I need to find two pieces of information: the current weather in Paris and the result of the last PSG match. I will start by searching for the weather. Action:
search("current weather in Paris")
(The system executes the search action and gets a result.)
Iteration 2:
Prompt:
Question: What is the current weather in Paris, and what was the score of the latest Paris Saint-Germain soccer match?
Observation: The current weather in Paris is 18°C and sunny.
LLM Output:
Thought: I have the weather. Now I need to find the score of the last PSG match. Action:
search("latest Paris Saint-Germain match score")
(The system executes the search action.)
Iteration 3:
Prompt:
Question: What is the current weather in Paris, and what was the score of the latest Paris Saint-Germain soccer match?
Observation: Paris Saint-Germain drew 1-1 with Monaco in their last match.
LLM Output:
Thought: I have now found both pieces of information. I can provide the final answer. Final Answer: The current weather in Paris is 18°C and sunny, and the latest Paris Saint-Germain match ended in a 1-1 draw with Monaco.
Implementing ReAct
ReAct is not just a prompt; it's a system. A full implementation requires:
- An LLM capable of generating the "Thought, Action" format.
- A set of "tools" that the LLM can call (e.g., a search function, a calculator function).
- A parsing layer that can extract the
Actionfrom the LLM's output. - An orchestration loop that executes the action, gets the observation, and feeds it back into the prompt for the next turn.
Frameworks like LangChain and LlamaIndex provide robust implementations of the ReAct agent loop, making it much easier to build these kinds of systems.
Why ReAct is a Game-Changer
- Overcomes Knowledge Cutoffs: It allows LLMs to access and reason about real-time information.
- Improves Factual Grounding: By grounding its reasoning in observations from the real world, it can reduce hallucinations.
- Enables Complex Workflows: It allows models to interact with APIs, databases, and other software, turning them into true "agents" that can perform tasks, not just generate text.
- Enhances Transparency: The explicit "Thought" process provides a clear, auditable trail of the agent's reasoning.
Key Takeaways
- The ReAct framework combines reasoning with action, allowing LLMs to interact with external tools.
- The core loop is "Thought -> Action -> Observation."
- "Thought" is the model's internal reasoning, and "Action" is a call to an external tool.
- ReAct enables LLMs to overcome knowledge cutoffs and perform complex, multi-step tasks in the real world.
- Implementing ReAct requires an orchestration loop to manage the tools and the flow of information.
What's Next?
ReAct provides a powerful loop for reasoning and acting. But how does an agent learn from its mistakes? What if an action leads to a bad outcome? In the next article, we will explore Reflexion, a technique that builds upon ReAct by adding a "self-reflection" step, allowing agents to analyze their past actions and improve their future performance.
With ReAct, the LLM is no longer just an oracle; it is an actor on the world stage, capable of seeking out knowledge and taking action to achieve its goals.