LangGraph Tutorial: Build Stateful AI Agents in Python

LangGraph is a versatile Python framework designed for stateful, cyclic, and multi-actor Large Language Model (LLM) applications. LangGraph builds upon its parent library, LangChain, and allows you to build sophisticated workflows that can handle the complexities of real-world LLM applications. Throughout this LangGraph tutorial, you’ll build a complete email-processing agent from the ground up.

By the end of this tutorial, you’ll understand that:

  • You can use LangGraph to build LLM workflows by defining state graphs with nodes and edges.
  • LangGraph expands LangChain’s capabilities by providing tools to build complex LLM workflows with state, conditional edges, and cycles.
  • LLM agents in LangGraph autonomously process tasks using state graphs to make decisions and interact with tools or APIs.
  • You can use LangGraph independently of LangChain, although they’re often used together to complement each other.

Explore the full tutorial to gain hands-on experience with LangGraph, including setting up workflows and building a LangGraph agent that can autonomously parse emails, send emails, and interact with API services.

While you’ll get a brief primer on LangChain in this tutorial, you’ll benefit from having prior knowledge of LangChain fundamentals. You’ll also want to ensure you have intermediate Python knowledge, specifically in object-oriented programming concepts like classes and methods.

Take the Quiz: Test your knowledge with our interactive “LangGraph: Build Stateful AI Agents in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


{# position-relative bounds the stretched-link below: BS5 columns
are no longer positioned, so without it the click overlay
stretches to the whole article and swallows nearby links #}

Interactive Quiz

LangGraph: Build Stateful AI Agents in Python

Test your understanding of LangGraph, the Python library for building stateful, cyclic, and multi-actor LLM workflows and agents built on LangChain.

What Is LangGraph?

LangGraph is an open-source Python framework for building stateful, cyclic, and multi-agent applications powered by large language models (LLMs). It gives you a low-level orchestration layer that represents an LLM application as a graph: each step is a node, and the connections between steps are edges. Because the graph can branch and loop, you can model decision-making and repeated actions that a simple, linear pipeline can’t express.

LangGraph is part of the LangChain ecosystem and integrates closely with LangChain, but it’s a standalone framework that you can also use on its own.

What is LangGraph used for? It shines whenever an LLM application needs to do more than answer a single prompt, including tasks like:

  • Stateful workflows that carry information forward from one step to the next
  • Conditional logic that routes the application down different paths depending on the data
  • Cycles that repeat a step until some condition is met
  • AI agents that decide which tools to call and act autonomously
  • Multi-agent systems in which several agents coordinate to complete a larger task

Throughout this tutorial, you’ll build these capabilities from the ground up as you create an email-processing agent. Before you start coding, you’ll install LangGraph and set up a few example emails to test your work.

Install LangGraph

LangGraph is available on PyPI, and you can install it with pip. Open a terminal or command prompt, create a new virtual environment, and then run the following command:

Language: Shell

(venv) $ python -m pip install langgraph

This command will install the latest version of LangGraph from PyPI onto your machine. This tutorial was written for LangGraph 1.2, and the graph-building API that you’ll use here has been stable since the 1.0 long-term support release. To verify that the installation was successful, start a Python REPL and import LangGraph:

Language: Python

>>> import langgraph

If the import runs without error, then you’ve successfully installed LangGraph. You’ll also need a few more libraries for this tutorial:

Language: Shell

(venv) $ python -m pip install langchain-openai "pydantic[email]"

You’ll use langchain-openai to interact with OpenAI LLMs, but keep in mind that you can use any LLM provider you like with LangGraph and LangChain. You’ll use pydantic to validate the information your agent parses from emails.

Before moving forward, if you choose to use OpenAI, make sure you’re signed up for an OpenAI account and that you have a valid API key. You’ll need to set the following environment variable before running any examples in this tutorial:

Language: .env

OPENAI_API_KEY=<YOUR-OPENAI-API-KEY>

Note that while LangGraph was made by the creators of LangChain and the two libraries are highly compatible, it’s possible to use LangGraph without LangChain. However, it’s more common to use LangChain and LangGraph together, and you’ll see throughout this tutorial how they complement each other.

With that, you’ve installed all the dependencies you’ll need for this tutorial, and you’re ready to create your LangGraph email processor. Before diving in, you’ll take a brief detour to set up quick sanity tests for your app. Then, you’ll go through an overview of LangChain chains and explore LangGraph’s core concept, the state graph.

Read the full article at »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Similar Posts

Leave a Reply