Login Sign Up

Agent Planning Using LangChain

Introduction

Agents and assistants that lack planning capabilities are merely chatbots. We must integrate planning mechanisms to build autonomous, thinking agents capable of solving complex goals. 

This tutorial will explore agent planning using LangChain and demonstrate how raw Large Language Models (LLMs) differ from structured agentic approaches.

Understanding Agent Planning

Planning allows an agent to:

  • Break down a goal into sequential actions.
  • Execute those actions independently.
  • Return structured results to the user.

Many commercial AI platforms, including OpenAI Assistants and Claude, now incorporate built-in planning mechanisms. However, raw LLMs without planning struggle with sequential task execution.

Agent planning and reasoning process
Agent planning and reasoning process

Running an Agent Without Planning

To illustrate the limitations of raw LLMs, we use Nexus, a tool that facilitates AI agent development. Below is a basic demonstration of running Nexus with the Gradio interface.

Running Nexus

nexus run gradio

Gradio provides an intuitive web interface for interacting with agents.

Defining an Agent’s Goal

Search Wikipedia for pages on {topic} and download each page and save it 

to a file called Wikipedia_{topic}.txt

Actions Required for the Goal

import wikipediaapi

# Initialize Wikipedia API
wiki_wiki = wikipediaapi.Wikipedia('en')

def search_wikipedia(topic):
    """Searches Wikipedia and returns the first relevant page title."""
    page = wiki_wiki.page(topic)
    return page.title if page.exists() else "No page found"

def get_wikipedia_page(title):
    """Fetches Wikipedia page content given a title."""
    page = wiki_wiki.page(title)
    return page.text if page.exists() else "Page not found"

def save_file(content, filename):
    """Saves the retrieved content to a file."""
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(content)
    return f"File {filename} saved successfully."

Selecting Actions in Nexus

Once the agent is created, assign the following tools:

  • search_wikipedia(topic)
  • get_wikipedia_page(title)
  • save_file(content, filename)

Initially, we disable planning to observe how the agent behaves without structured execution.

Results Without Planning

Enter a search query: Calgary
Executing search_wikipedia
Found page: Calgary
Downloading content
Error: No sequential execution capability.

Since raw LLMs do not inherently plan actions, they fail to execute tasks that depend on prior steps.

Enabling Sequential Planning

To enable structured execution, we incorporate planning using OpenAI Assistants.

Setting Up a Planning Agent

from langchain.agents import initialize_agent
from langchain.tools import Tool

# Wrap functions into LangChain tools
search_tool = Tool(name="Wikipedia Search", func=search_wikipedia)
download_tool = Tool(name="Download Wikipedia Page", func=get_wikipedia_page)
save_tool = Tool(name="Save File", func=save_file)

# Initialize agent with planning
agent = initialize_agent(
    tools=[search_tool, download_tool, save_tool],
    agent="zero-shot-react-description",
    verbose=True
)

# Run agent
agent.run("Search Wikipedia for Calgary and save to file")

Output

Executing Wikipedia Search: Calgary…
Found page: Calgary
Downloading content…
Saving file: Wikipedia_Calgary.txt
Process completed successfully!

By integrating planning capabilities, the agent can now execute sequential tasks without failure.

Planning is essential for building intelligent AI assistants. Without it, raw LLMs struggle to complete multi-step tasks. By leveraging LangChain’s planning tools, we can create structured workflows that enable agents to function autonomously and efficiently.