Module 8.1 — What is an AI Agent & The ReAct Pattern

How agents think, plan, and act — the foundation of everything in Phase 8


The Core Difference — LLM vs Agent

You have been using LLMs in two ways so far:

Chain (what you built in Phase 6 and 7):
User question → fixed steps → answer
Steps decided at BUILD time by you

Agent (what Phase 8 is about):
User question → LLM decides steps → answer
Steps decided at RUNTIME by the LLM itself

A chain is like a train on fixed tracks. An agent is like a car — it decides where to go based on what it sees.


What an Agent Actually Is

An agent is just an LLM in a loop with access to tools.

┌─────────────────────────────────────┐
│            AGENT LOOP               │
│                                     │
│  Input                              │
│    ↓                                │
│  LLM thinks:                        │
│  "What should I do next?"           │
│    ↓                                │
│  Option A → Call a tool             │
│    ↓                                │
│  Tool runs → result comes back      │
│    ↓                                │
│  LLM thinks again:                  │
│  "Do I have enough info now?"       │
│    ↓                                │
│  Option B → Give final answer       │
│    ↓                                │
│  Output                             │
└─────────────────────────────────────┘

The LLM keeps looping — think, act, observe — until it decides it has enough information to give a final answer.


The ReAct Pattern

ReAct = Reasoning + Acting

Introduced in a 2023 research paper at ICLR. It is the most widely used agent pattern today. Every major agent framework — LangChain, LangGraph, AutoGen — implements ReAct under the hood.

The pattern has three steps that repeat:

THOUGHT  → LLM reasons about what to do next
ACTION   → LLM calls a specific tool with specific inputs
OBSERVATION → Tool result comes back to the LLM

Repeat until:
THOUGHT  → "I now have enough information"
ANSWER   → LLM gives the final response

A Real Example — Step by Step

User asks: "What is the weather in Delhi today and should I carry an umbrella?"

Watch how a ReAct agent handles this:

THOUGHT 1:
"The user wants weather info for Delhi.
I need to call the weather tool first."

ACTION 1:
Tool: get_weather
Input: { city: "Delhi" }

OBSERVATION 1:
"Delhi: 32°C, humidity 85%, rain expected at 4pm"

THOUGHT 2:
"I have the weather data. It will rain.
The user also asked about an umbrella.
I have enough info to answer now."

ANSWER:
"Today in Delhi it's 32°C with high humidity
and rain expected around 4 PM. Yes, you should
definitely carry an umbrella."

The LLM went: Think → Act → Observe → Think → Answer.


Why ReAct is Better Than a Simple Chain

Simple chain approach:
You pre-decide: "always call weather tool, then answer"
Works for: simple, predictable tasks
Breaks when: task requires different steps based on input

ReAct approach:
LLM decides: "what tool do I need right now?"
Works for: complex, unpredictable tasks
Handles: multi-step tasks, conditional logic, different paths

Real example of why this matters:

User 1: "What is 2+2?"
Simple chain: calls weather tool (wrong!) then answers
ReAct agent: realizes no tool needed, answers directly ✅

User 2: "What is the weather in Delhi and what is 25% of 4000?"
Simple chain: can only follow its fixed path
ReAct agent: calls weather tool, then calculator tool ✅

The Three Types of Agent Memory

Agents have different kinds of memory, just like humans:

1. IN-CONTEXT MEMORY (short-term)
   → The current conversation in the context window
   → Lost when conversation ends
   → Example: "earlier you told me your name is Sofia"

2. EXTERNAL MEMORY (long-term)
   → Stored in a database, vector store, or file
   → Persists across conversations
   → Example: Pinecone storing user preferences

3. PROCEDURAL MEMORY (how-to knowledge)
   → Baked into the system prompt
   → Instructions for how the agent behaves
   → Example: "always search before answering"

You have already used all three:

In-context  → MemorySaver with thread_id (Phase 7)
External    → Pinecone vector store (Phase 7.4)
Procedural  → systemPrompt in createAgent (Phase 7)

Agent vs Chain — When to Use Which

Use a CHAIN when:
→ Steps are always the same
→ Task is predictable and simple
→ You need maximum reliability
→ Examples: PDF chunking, text classification,
            data extraction

Use an AGENT when:
→ Steps depend on the input
→ Task requires reasoning and decisions
→ Multiple tools might be needed
→ Order of steps is not fixed
→ Examples: research assistant, customer support,
            coding helper, resume analyzer

What LangGraph Is — And Why It Matters Now

From the research — LangGraph is now recommended for production agents with its stateful graph architecture, human-in-the-loop interrupts, and time-travel debugging capabilities for 2025.

You have been using createAgent from langchain — which runs on LangGraph internally. LangGraph is the actual engine underneath.

createAgent (from "langchain")
       ↓
       uses LangGraph internally
       ↓
LangGraph (from "@langchain/langgraph")
       ↓
       stateful graph where each node = one agent step
       each edge = connection between steps

In Phase 8 we will use LangGraph directly for:

→ Building custom agent loops
→ Multi-agent systems (multiple agents collaborating)
→ Human-in-the-loop (agent asks human before acting)
→ Agent with persistent memory across sessions

Phase 8 — What You Will Build

Module 8.1 → What is an Agent + ReAct (this module)
Module 8.2 → Tool Calling + Function Calling deep dive
Module 8.3 → Agent Memory + Planning
Module 8.4 → Multi-Agent Systems with LangGraph
Module 8.5 → Project: Resume Analyzer Agent
Module 8.6 → Project: Research Agent

3-Line Summary

  1. An agent is an LLM in a loop — it thinks, calls a tool, observes the result, thinks again, and keeps looping until it has enough information to give a final answer — unlike a chain which always follows fixed steps you define at build time.
  2. The ReAct pattern is Reasoning + Acting — the LLM alternates between internal thought ("what do I need to do?") and external action (calling a tool) until it reaches a final answer — this is the foundation of every major agent framework in 2025.
  3. LangGraph is the production-grade engine underneath createAgent — it models agent workflows as a stateful graph where nodes are agent steps and edges define routing — in Phase 8 we use it directly to build multi-agent systems and custom agent loops.

Module 8.1 — Complete ✅

No comments:

Post a Comment

Module 8.1 — What is an AI Agent & The ReAct Pattern

How agents think, plan, and act — the foundation of everything in Phase 8 The Core Difference — LLM vs Agent You have been using LLMs in two...