🧠 Lesson 6.1: How Agents Plan (ReAct, Plan-and-Execute)

 

🧠 Lesson 6.1: How Agents Plan (ReAct, Plan-and-Execute)


🎯 Objective

By the end of this lesson, you will understand:

  • What it means for an AI agent to “plan”

  • How planning is implemented in LangChain using strategies like ReAct and Plan-and-Execute

  • The differences between reactive and deliberative agent behaviors

  • When and how to apply each method in building intelligent, multi-step agents


🔍 1. What Does It Mean for an Agent to “Plan”?

In LangChain and other LLM frameworks, planning refers to the agent’s ability to:

  • Break a user’s goal or question into multiple steps

  • Decide which tools or actions to use for each step

  • Execute each step in order, with logic and memory

  • Adapt if something changes or fails along the way

This is critical for complex tasks like:

  • Researching a topic

  • Performing data analysis

  • Executing business workflows

  • Coordinating between multiple tools or agents


🤖 2. ReAct (Reasoning and Acting)

🔧 Overview

ReAct is a prompt-based strategy where the agent reasons and acts interleaved — like a human thinking out loud.

It reasons about the next action, takes it, sees the result, reasons again, and continues.

🔁 Flow:

  1. Receive a user query

  2. Reason step-by-step in natural language

  3. Act: choose a tool or action

  4. Observe result

  5. Repeat until a final answer is ready

📋 Example:

User: “What’s the current weather in Istanbul and how does it compare to last week?”

Agent (ReAct):

Thought: I need to get the current weather in Istanbul.
Action: weather_tool.run("Istanbul")

Observation: 31°C, sunny

Thought: Now I need historical weather from last week.
Action: history_tool.run("Istanbul, last week")

Observation: 24°C, rainy

Thought: Now I can compare them.

Final Answer: This week is hotter and sunnier than last week in Istanbul (31°C vs 24°C).

✅ Best For:

  • Flexible, short-to-mid complexity tasks

  • Interactive querying, tool use, and iteration

  • Transparent step-by-step logic


🧭 3. Plan-and-Execute

🔧 Overview

Plan-and-Execute separates the agent’s thinking into two phases:

  1. Planner: Creates a full plan of actions upfront

  2. Executor: Carries out each step of the plan

This is especially useful for long, structured, multi-step tasks.


🧱 Structure:

  • Planner LLM: “Break this task into 3 steps.”

  • Executor LLM: “Carry out each step and report results.”

🧠 Example Workflow:

User: “Find the latest news about Tesla, summarize it, and post a summary to my Notion.”

Planner Output:

1. Search for the latest Tesla news.
2. Summarize the articles.
3. Post the summary to Notion database.

Executor Actions:

  1. Use web_search_tool to find articles

  2. Summarize with summarizer_tool

  3. Send to Notion using notion_tool

✅ Best For:

  • Long-form, multi-step, highly structured tasks

  • Automations where step order matters

  • Tasks where steps shouldn’t depend on mid-run LLM reasoning


⚖️ 4. ReAct vs. Plan-and-Execute: A Comparison

Feature ReAct Plan-and-Execute
Reasoning style Interleaved (step-by-step) Separate planning + execution
Transparency High (traces reasoning) Moderate
Tool usage Dynamic and flexible Sequential and pre-planned
Use case complexity Low–medium Medium–high
Debugging ease Easy to follow reasoning Easier to control overall flow
Suitable for Conversational agents, assistants Task runners, workflows

🔨 5. How to Implement in LangChain


🔁 ReAct Agent (LangChain)

from langchain.agents import initialize_agent, Tool, AgentType
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(model="gpt-4")
tools = [search_tool, weather_tool, notion_tool]

agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,  # ReAct agent type
    verbose=True
)

agent.run("What's the weather today and how does it compare to last week?")

🧭 Plan-and-Execute Agent

from langchain.experimental.plan_and_execute import PlanAndExecute, load_agent_executor
from langchain.experimental.plan_and_execute.planners import load_chat_planner

planner = load_chat_planner(llm)
executor = load_agent_executor(llm=llm, tools=tools)

agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

agent.run("Find and summarize the latest Tesla news, then post it to Notion.")

🧩 6. When to Use Each in Multi-Agent Systems

In multi-agent workflows:

  • Use ReAct agents for dynamic tasks where agents must adapt on the fly

  • Use Plan-and-Execute agents when:

    • One agent delegates work to others

    • A master planner coordinates sub-agents

    • You need reliable order of execution


✅ Summary

Concept Key Takeaway
ReAct Think-act-think loop with live reasoning
Plan-and-Execute Break plan first, then carry it out step-by-step
Agent Planning Needed for multi-step, tool-driven workflows
LangChain Support Built-in agent types for both methods

 

103