Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 78

Introduction to ReAct: LLM Reasoning and Tool Interaction

Recommended resources

Introduction

Welcome to our new module on Agentic AI Systems! In the previous module, we mastered Retrieval-Augmented Generation (RAG), a technique to provide LLMs with external knowledge. RAG systems are powerful for question-answering, but they are fundamentally passive; they retrieve information and synthesize an answer. What if we wanted the LLM to do things—to take actions in the world?

This lesson kicks off our journey into agents, which are systems that can reason, plan, and use tools to accomplish complex goals. We'll start with the foundational framework that enables this capability, directly addressing the learning outcome: Explain the ReAct (Reason, Act) framework for enabling LLMs to reason and interact with external tools.

You'll learn how ReAct synergizes the reasoning capabilities we saw in techniques like Chain-of-Thought with the ability to take actions, transforming the LLM from a passive "brain in a vat" into an active agent that can interact with its environment.


From Pure Reason to Action

Before ReAct, two main paradigms existed for leveraging LLMs for complex tasks:

  1. Reason-Only (e.g., Chain-of-Thought): The LLM generates a step-by-step reasoning trace to arrive at an answer. This is powerful but has significant limitations. Since the model operates entirely from its internal knowledge, it's prone to factual hallucination, cannot access real-time data, and can't verify information.

  2. Act-Only: The LLM is prompted to directly output actions to be taken in an environment. This allows interaction with the outside world, but it often lacks high-level planning. The model might struggle to adapt to unexpected results or get stuck in repetitive loops because it isn't explicitly reasoning about its overall strategy.

The ReAct framework was introduced to combine the strengths of both approaches. To get a high-level sense of this evolution, let's start with a short introduction from the original academic paper that proposed the ReAct framework.

ReAct: Synergizing Reasoning and Acting in Language Models

The paper 'ReAct: Synergizing Reasoning and Acting in Language Models' introduces the core idea. We'll focus on the introduction, which frames the problem and the solution.

Read the introduction (Section 1). Pay close attention to the analogy of cooking a dish, which beautifully illustrates how humans seamlessly blend reasoning and acting. Also, examine Figure 1 in the paper, which visually compares Standard, Chain-of-Thought (Reason-Only), Act-Only, and ReAct prompting. This figure is one of the clearest illustrations of what ReAct brings to the table.

As the paper highlights, ReAct allows the model to generate both verbal reasoning traces and actions in an interleaved manner. This creates a powerful synergy:

  • Reasoning helps acting: The model can create, maintain, and adjust a plan.
  • Acting helps reasoning: The model can query external tools (like a search engine or calculator) to gather new information and ground its reasoning in facts, reducing hallucination.

This diagram provides a great conceptual model of how ReAct fits into the landscape of LLM capabilities.

Evolution of LLM Agentic Capabilities: Reason Only, Act Only, and ReAct
This image from the ReAct project page shows how the framework integrates 'Reason Only' (internal thought processes) and 'Act Only' (environmental interaction) paradigms into a unified, synergistic loop.

The ReAct Loop: Thought, Action, Observation

The core of the ReAct framework is a simple, iterative loop. Given a task, the LLM repeatedly cycles through three steps until it can provide a final answer.

  1. Thought: The LLM thinks about the problem, assesses its current state, and devises a plan for what to do next. This is a reasoning trace, similar to Chain-of-Thought, that is not shown to the user but is kept in the context for the model itself.
  2. Action: Based on its thought, the LLM decides to take an action. This action consists of specifying a tool to use (e.g., WikipediaSearch, Calculator) and the input for that tool (e.g., "age of Joe Biden", 342 + 843).
  3. Observation: An external component, often called an "agent executor" or "orchestrator," executes the action. It calls the specified tool with the given input and captures the output. This output is the observation, which is then fed back to the LLM.

The LLM then starts the next cycle, incorporating the new observation into its context to generate the next Thought.

This process is visualized well in the following diagram.

ReAct Framework for LLM Tool Interaction
This diagram shows the ReAct cycle: The LLM receives a task, reasons about it, calls a tool (Action), the tool interacts with the environment, and the result is returned to the LLM (Observation), which informs the next step of reasoning.

The following video provides an excellent walkthrough of this cycle, comparing ReAct to its predecessors and showing a multi-step example.

Understanding ReACT with LangChain

The video 'Understanding ReACT with LangChain' by Sam Witteveen gives a clear, practical explanation of the ReAct loop in action.

Please watch the following segments: What is ReAct? (00:00 - 01:03): A quick introduction to what ReAct stands for and its purpose. ReAct vs. Other Paradigms (01:03 - 05:03): This part compares Chain-of-Thought (Reason-Only), Action-Only, and ReAct, using a concrete question-answering example. Focus on the Thought, Action, Observation structure in the ReAct example. Multi-Hop Question Example (15:38 - 19:25): This demonstrates how the agent uses the loop multiple times to solve a more complex question that requires finding and combining several pieces of information.


How It Works: Prompt Engineering and Stop Sequences

As an engineer, you know there's no magic. The ReAct loop is enabled by a clever combination of prompt engineering and a simple execution mechanism.

  1. The ReAct Prompt Template: The LLM is given a detailed prompt that instructs it to follow the Thought, Action, Action Input, Observation format. The prompt includes descriptions of the available tools and a few examples (few-shot learning) of the pattern.

  2. The Stop Sequence: Here is the critical mechanical trick. The agent executor configures the LLM call to stop generating text as soon as it produces the token Observation:.

This is crucial because it prevents the LLM from hallucinating the result of the action. Instead, the process looks like this:

  • The LLM generates a Thought and an Action.
  • The LLM call pauses at Observation:.
  • The agent executor parses the Action and Action Input from the generated text.
  • The executor runs the actual tool (e.g., calls the Wikipedia API).
  • The executor takes the real result from the tool, appends it to the prompt history after Observation:, and invokes the LLM again.

The history of thoughts, actions, and observations is maintained in a "scratchpad," which serves as the agent's working memory.

This video and blog post provide a fantastic, from-scratch explanation of this mechanism, which will resonate with your engineering background. The video even includes a manual simulation of the stop-and-resume process.

Getting Started with ReAct AI agents work using langchain

The video 'Getting Started with ReAct AI agents' by Chris Hay breaks down how to build a ReAct agent from scratch, with a key focus on the stop sequence mechanic.

Watch the following parts: Setting up the Agent (14:24 - 19:47): Pay attention to how the prompt is pulled from LangChain Hub and how the tools are defined. The initial run without a tool shows the LLM hallucinating what to do. Creating a Tool (19:47 - 22:32): This shows how a simple Python function is decorated to become a tool the agent can use. Notice how the function's docstring becomes its description for the LLM. The Stop Sequence Explained (31:17 - 43:33): This is the most important section. It manually reconstructs the agent's behavior, showing how stopping on the Observation: token allows the program to execute the tool and re-inject the real observation, preventing hallucination. This is a perfect demonstration of the underlying algorithm.

The accompanying article provides a text-based version of the same concepts, which can be a useful reference.

Agent 101: Create ReACT Agent From Scratch

The article 'Agent 101: Create ReACT Agent From Scratch' complements the video by providing the code and a clear textual breakdown of each component.

Quickly scan these sections to reinforce your understanding: Prompt: ReAct Base prompt: Look at the structure of the prompt template. Stop Sequence: Role and Importance: This section succinctly explains why stopping at Observation: is critical. Lights Camera & Action: Everything Stitched: This shows the final loop that orchestrates the entire process.

Test your understanding!

Why is the Observation: stop sequence so critical to the functionality of a ReAct agent? What would happen if you removed it and just let the LLM generate a complete response in one go?

Show answer

Without the stop sequence, the LLM would simply generate the entire Thought -> Action -> Observation -> ... -> Final Answer transcript in a single pass. The "Observation" would be a hallucination—the LLM would guess what the result of the tool should be, rather than getting the actual result from the real world.

For example, if the action is to call a stock price API, the LLM would invent a stock price. The stop sequence is the mechanism that allows the agent framework to pause the LLM, execute the real API call, and inject the real stock price back into the context. This grounds the agent's reasoning in factual, up-to-date information from its environment.


Conclusion

In this lesson, you've unpacked the ReAct framework, a cornerstone of modern agentic AI systems. You've moved beyond systems that just retrieve knowledge to understanding how an LLM can be prompted to reason, plan, and interact with the outside world.

Key Takeaways:

  • ReAct = Reason + Act: It synergizes the internal reasoning of Chain-of-Thought with the external interaction of tool use.
  • The Core Loop: The agent operates in an iterative Thought -> Action -> Observation cycle, using the observation from one step to inform the thought of the next.
  • Engineering over Magic: The ReAct mechanism is not magical. It's an algorithmic loop powered by a carefully structured prompt and the critical use of a stop sequence (like Observation:) to allow an agent executor to run real tools and feed their results back to the LLM.
  • Benefits: This framework enables LLMs to solve more complex problems, access real-time data, reduce factual hallucinations, and provide an interpretable trace of their "thinking" process.

What's Next?

Today, we've focused on the conceptual framework of ReAct and the high-level idea of using "tools." In our next lesson, we will dive into the practical engineering of this: Implement tool use (function calling) for an LLM using a standardized API schema. You'll learn how modern LLMs from OpenAI, Anthropic, and Google have built-in "function calling" or "tool use" APIs that standardize the Action and Action Input steps, making the implementation of ReAct agents more robust and straightforward than the manual parsing shown today.