chapter five

5 Introducing AI Agents with LangGraph

 

This chapter covers

  • Overview of LLM agents
  • LangGraph fundamentals and state management
  • Transition from LangChain chains
  • Practical case study conversion

In this chapter, I’ll show you how to move from basic LangChain applications to more advanced agent-based systems using LangGraph. We'll rebuild the web research assistant from chapter 4, shifting it from a simple, linear setup to a flexible agent framework for handling more complex tasks. By the end, you'll have a basic understanding of how to build applications with LangGraph that can manage multi-step tasks and make better decisions.

5.1 Understanding LLM-Based Workflows and Agents

LLM-powered AI agents follow two primary design patterns: workflows and agents. Both determine the application's flow, as shown in figure 5.1. It's crucial to understand these concepts—and what is meant by "agentic system" or "agent-based system"—since the terms are similar yet can be confusing.

Figure 5.1 Workflows and agents: workflows use the LLM to choose the next step from a fixed set of options, such as routing a request to a SQL database or a REST API and synthetizing the answer with the related results. Agents, however, dynamically select and combine tools to achieve their objectives.

5.1.1 Workflows

5.1.2 Agents

5.1.3 When to Use Agent-Based Architectures

5.1.4 Agent Frameworks in the LLM Ecosystem

5.2 LangGraph Basics

5.3 Moving from LangChain Chains to LangGraph

5.4 LangGraph Core Components

5.4.1 StateGraph Structure

5.4.2 State Management and Typing

5.4.3 Node Functions and Edge Definitions

5.4.4 Entry Points and End Conditions

5.5 Turning the Web Research Assistant into an AI Agent

5.5.1 Original LangChain Implementation Overview

5.5.2 Identifying Components for Conversion

5.5.3 Step-by-Step Transformation Process

5.5.4 Code Comparison and Benefits Realized

5.6 Summary