Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding the ReAct Framework

Hello! In our last lesson, we established the fundamental difference between a passive, prompt-responding LLM and a proactive, goal-oriented AI agent. We learned that an agent operates in a continuous loop of reasoning, acting, and observing to achieve a high-level objective.

Today, we'll dive into the mechanics of that loop. This lesson's learning outcome is to explain the ReAct (Reasoning and Acting) framework for agent behavior. ReAct is a foundational and powerful paradigm that provides a structured way for an LLM to "think out loud" and interact with the world to solve problems. It's the blueprint that turns the conceptual agent loop into a practical reality.

We will cover:

  • The evolution from simple reasoning (Chain of Thought) to ReAct.
  • The core components of the ReAct cycle: Thought, Action, and Observation.
  • How an "Agent Executor" orchestrates this cycle behind the scenes.

Let's begin by understanding why ReAct was a necessary innovation.

From Thinking to Doing: The Need for ReAct

In a previous module, you might have encountered Chain-of-Thought (CoT) prompting. This technique, often triggered by a simple phrase like "Let's think step by step," encourages an LLM to generate intermediate reasoning steps before giving a final answer. This dramatically improves performance on complex reasoning tasks.

However, CoT has a major limitation: the reasoning happens in a vacuum. The model uses only its internal, static knowledge. It can't look up current information, perform calculations, or verify facts against the real world. This can lead to factual errors, or "hallucinations," especially for questions about recent events.

On the other hand, you could imagine a system that is purely action-oriented. It can use tools but lacks a robust planning or reasoning capability. This is where ReAct comes in. It synergizes these two concepts: reasoning and acting.

The following image provides a perfect conceptual map for this. It shows "Reason Only" (like CoT), "Act Only," and how ReAct combines them.

Comparison of Reason Only, Act Only, and ReAct Frameworks
This diagram illustrates the core idea of ReAct. On the left, a 'Reason Only' model generates thoughts. In the middle, an 'Act Only' model interacts with an environment. On the right, ReAct integrates both: the model's reasoning informs its actions, and the observations from those actions feed back into its reasoning process.

To further ground these concepts, let's watch a video that explains the evolution from Chain of Thought to ReAct.

LLM Chronicles #6.4: LLM Agents with ReAct (Reason + Act)

The video 'LLM Agents with ReAct' from Donato Capitella provides excellent context on how ReAct builds upon earlier techniques.

Please watch the following two segments: Chain of Thought (01:28 - 03:25): This will be a good refresher on CoT and its limitations, setting the stage for ReAct. The ReAct Framework (04:49 - 06:01): This introduces the ReAct method, highlighting its iterative 'thought, action, observation' loop and its ability to use a flexible set of tools.

As the video explains, ReAct provides a general framework that allows an LLM to flexibly and iteratively call upon tools to solve a problem. Let's now examine that iterative loop in detail.

The ReAct Cycle: Thought, Action, Observation

The ReAct framework structures the agent's problem-solving process as a series of interleaved steps. The LLM is prompted to generate text that follows a specific format, which is then interpreted by the agent's code.

The cycle consists of:

  1. Thought: The LLM generates a reasoning trace. This is its internal monologue where it assesses the overall goal, what it knows so far, and what it needs to do next. For example: "I need to find the author of 'Sapiens' and then find their most recently published book."
  2. Action: Based on the thought, the LLM decides to use a specific tool. It generates the name of the tool and the input for it. For example: Action: Search("author of Sapiens").
  3. Observation: An external component (the "Agent Executor," which we'll discuss next) runs the action and passes the result back to the LLM as an observation. For example: Observation: Yuval Noah Harari.
  4. Repeat: This Thought -> Action -> Observation loop repeats. With the new observation, the LLM generates a new thought: "Okay, the author is Yuval Noah Harari. Now I need to search for his latest book." This leads to a new action, and so on.
  5. Finish: Once the LLM determines it has enough information to answer the original user query, it outputs a special "Finish" action with the final answer.

This next resource provides a clear text-based explanation and a classic example from the original ReAct paper.

ReAct - Prompt Engineering Guide

The 'ReAct - Prompt Engineering Guide' is a concise reference that clearly explains the ReAct paradigm.

Please read the sections 'How it Works?' and the example that follows it. Focus on how the model iteratively breaks down the question, uses the Search and Lookup tools, and uses the Observation from one step to inform the Thought of the next step.

The power of this approach is visually striking when you compare it to other methods. The image below, taken from the original ReAct paper, shows how ReAct successfully completes a task where other methods fail because they either cannot access external knowledge (CoT) or cannot reason effectively (Act-Only).

Comparison of Prompting Methods: Standard, CoT, Act-Only, and ReAct
This image compares different prompting methods. Notice how 'Standard' and 'Act-Only' fail to decompose the problem. 'CoT (Reason Only)' correctly decomposes the problem but hallucinates the answer because it can't access external information. Only 'ReAct' successfully reasons, acts (searches), observes, and uses that new information to reach the correct answer.

The Engine Room: The Agent Executor

As a software engineer, you're likely wondering how this loop is actually implemented. The LLM itself only generates text. How does an Action like Search("who is the US president") actually trigger an API call?

This is managed by a piece of code often called an Agent Executor or an agent runtime. This component acts as the orchestrator around the LLM.

Here is the process:

  1. Prompting: The Agent Executor wraps the user's query in a detailed prompt. This master prompt tells the LLM it's an agent, describes the tools it has available, and provides examples of the Thought-Action-Observation format.
  2. Generation & Parsing: The LLM generates its Thought and Action. The Agent Executor then parses this text output, looking for the Action: keyword.
  3. Tool Dispatch: When it finds an action, it pauses generation and calls the corresponding function in your code (e.g., it calls a Python function that connects to a search API).
  4. Observation & Continuation: The executor takes the return value from the tool, formats it as an Observation:, and appends it to the history of the conversation (often called a "scratchpad").
  5. Looping: The entire updated history (original query, previous thoughts, actions, and observations) is fed back to the LLM, which then generates the next Thought.
  6. Termination: The loop continues until the LLM generates a Final Answer, which the executor then returns to the user.

This video segment explains this crucial orchestrator role perfectly.

LLM Chronicles #6.4: LLM Agents with ReAct (Reason + Act)

Let's return to the 'LLM Agents with ReAct' video to understand the role of the Agent Executor.

Watch the segment from 07:48 to 09:09. It clearly explains why an Agent Executor is needed (since LLMs only process text) and how it intercepts actions, executes tools, and feeds observations back into the context.

To connect this directly to a software development mental model, the process can be broken down into distinct methods or functions within the agent's code, like think, decide, and act.

Building ReAct Agents from Scratch: A Hands-On Guide using Gemini

The article 'Building ReAct Agents from Scratch' provides a great conceptual breakdown of the agent's internal logic from a developer's perspective.

Please read the section 'Step 3: Implementing the Think-Act-Observe Loop'. You don't need to analyze the Python code line-by-line. Instead, focus on understanding the purpose of the think, decide, and act methods described. This maps directly to the orchestration logic we've been discussing.

Conclusion

You now have a solid conceptual and practical understanding of the ReAct framework. It is one of the most important concepts in the field of AI agents, providing a simple yet robust mechanism for combining an LLM's reasoning with real-world actions.

Key Takeaways:

  • ReAct = Reasoning + Acting: It combines the step-by-step reasoning of Chain-of-Thought with the ability to use external tools.
  • The Core Loop: The agent operates in an iterative Thought -> Action -> Observation cycle.
  • Structured Output: The LLM is prompted to produce its thinking in a structured format that an external program can parse.
  • The Agent Executor: This is the software component that orchestrates the loop, parsing the LLM's output, calling tools, and feeding observations back into the LLM's context or "scratchpad".

In our next lesson, we will focus entirely on the "Act" part of the cycle. You will learn about the role of tools (functions), how to define them for an LLM, and how they extend an agent's capabilities beyond simple text generation. This is the next logical step in your journey to building capable AI agents.

Can't find a good explanation? Sign up and we'll make it for you

Sign up