
The ReAct Pattern: Reasoning and Acting in Unison
Master the fundamental logic loop of autonomous AI. Learn how the ReAct (Reason + Act) pattern allows agents to solve complex tasks through observation and self-correction.
The ReAct Pattern: Reasoning and Acting in Unison
How does an agent actually "think"? If you give an LLM access to a search engine and say "Find the current weather in Tokyo," how does it know to Stop thinking and Call the search engine?
The industry standard for this is the ReAct Pattern (Reason + Act). In this lesson, we will break down the mechanics of the ReAct loop and how it manages Planning, Memory, and Tool Use.
1. The ReAct Loop (Thought, Action, Observation)
Developed in 2022, the ReAct pattern forces an agent to verbalize its internal state before it performs an action. This verbalization "grounds" the model and prevents it from hallucinating a result before it has the facts.
The loop looks like this:
- Thought: "I need to find the weather in Tokyo. I should use the search_tool."
- Action:
search_tool("weather in Tokyo") - Observation: "Current temp: 15°C, Sunny."
- Thought: "I have the information. I can now answer the user."
- Final Answer: "The weather in Tokyo is 15°C and sunny."
graph TD
A[Question] --> B[Thought: Plan next step]
B --> C[Action: Call a Tool]
C --> D[Observation: Get Tool Result]
D --> E{Goal achieved?}
E -- No --> B
E -- Yes --> F[Final Answer]
2. Planning: Breaking Down the "Big Goal"
Agents struggle with long, complex tasks. If you say "Build me a marketing agency," the agent will freeze. As an LLM Engineer, you must implement Planning Strategies:
- Pre-computed Planning: The agent writes a 10-step list before starting.
- Dynamic Planning: The agent recalculates the plan after every observation.
Pro Tip: If a task is very complex, use a Manager Agent to create the plan and Worker Agents to execute individual steps.
3. Memory: Managing the Context
Memory is what allows an agent to stay "on track."
Short-term Memory (Chat History)
Every turn of the ReAct loop is appended to the chat history.
- The Risk: If the loop runs 10 times, the history gets huge, costing you money and potentially exceeding the context window.
- The Solution: Memory Summarization. Every 5 steps, ask the model to summarize the history into a condensed "State" to save space.
Long-term Memory (Persistent State)
Storing facts about the user across different sessions using a database like Redis or a Vector Store.
4. Tool Use (Function Calling)
"Tool Use" is the process where an LLM generates a JSON object representing a function call instead of a text response.
How it Works (The "Function Calling" Protocol):
- You provide the model with a Schema of your tool (Name, Description, Parameters).
- The model sees the user request.
- Instead of answering, it outputs:
{"name": "get_weather", "arguments": {"city": "Tokyo"}}. - Your Python code parses this, runs the actual
get_weather()function, and sends the result back to the model as an Observation.
Code Example: Defining a Tool in LangChain
from langchain.tools import tool
@tool
def calculate_tax(income: float) -> str:
"""Calculates the 20% flat tax for a given income amount."""
tax = income * 0.2
return f"The calculated tax is ${tax:.2f}"
# How the model sees the tool (Auto-generated by LangChain)
# Name: calculate_tax
# Description: Calculates the 20% flat tax for a given income amount.
# Args: income (float)
Summary
- ReAct is the loop of Thought $\rightarrow$ Action $\rightarrow$ Observation $\rightarrow$ Repeat.
- Thinking (Reasoning) prevents the model from acting blindly.
- Tool Use is powered by structured "Function Calling" schemas.
- Memory must be managed to prevent "Context Overflow."
In the next lesson, we will move from single-agent loops to Multi-Agent Systems, exploring how to coordinate "Swarms" of agents using LangGraph and CrewAI.
Exercise: The Loop Hunt
Look at the following agent log:
- "Thought: I should find the director of the movie Inception."
- "Action: search(Inception director)"
- "Observation: Christopher Nolan"
- "Thought: Now I need to find his most recent movie."
Question:
- Is this agent following the ReAct pattern?
- What should the next Action be?
Answer: Yes, it follows Thought $\rightarrow$ Action $\rightarrow$ Observation. The next action should be search("Christopher Nolan most recent movie").