What is LangGraph?
Welcome to this beginner tutorial on LangGraph, where we're going to cover the theory and then a practical example of how you can get stuck in.
So LangGraph, what is it, why should you use it, and how do you use it? LangGraph is an AI agent building framework built by LangChain. It's highly flexible and it allows you to connect language models together in a way that the LLM can control what happens next. It's supported in two major programming languages: Python and JavaScript. And it was released last October, and since then it's been increasing in popularity as an alternative to other agent building frameworks like CrewAI and Microsoft AutoGen.
LangGraph Use Cases and Applications
Let's take a look at an example of how it might be used. Let's take a customer support agent who asks a question to an online website chatbot for a bank. The chatbot agent might then look up the customer information. It might then update the customer details based on the conversation, and it might also then want to make a transaction for the customer. But because this is a large transaction, we might want verification from a real person before it goes ahead. LangGraph supports this with human-in-the-loop execution, where the transaction can be authorized first before the conversation with the customer can continue.
What are some other applications where LangGraph could be highly useful? It could be useful for copywriting: gathering information from across the internet and putting it together into a single article or report. It could be useful for custom analytics for creating figures, dashboards, or extracting information from a database. It could be useful for customer service assistants, so whether that's answering questions via email, WhatsApp, SMS, or voice. It could be useful for personalized recommendation, as from previous interactions with the customer we might have an idea of what the customer prefers. It could be useful for doing research, to making sure that you stay up to date with the latest trends or any new articles that have been released. And finally, it could be useful for personal marketing: tailoring your communications to each customer based on their personal characteristics.
Key Features of LangGraph
So what are some features that make LangGraph particularly useful as an agentic framework?
It supports streaming straight out of the box with either tokens or messages. It also supports multiple execution at the same time, or async execution. It supports persistence with a database so that you don't need to persist your state between invocations. And it also supports fault-tolerant tool calls so that it can handle any failures with external APIs. As we've seen, it supports human-in-the-loop execution, and because of the way it handles state, it makes it quite easy to decide what the next action should be.
Core Concepts and Architectures
Let's take a look at the core building blocks of LangGraph that allow you to customize it for your use case.
Let's take a look at the core building blocks for LangGraph. Nodes contain the code you want to execute, and we connect them with edges, which determine which node should be executed next. We also have conditional edges, and these can decide based on what's happened so far which node should be executed next. And all of these components are tied together with the state, which stores our inputs, outputs, and any variables that are created to pass information between nodes.
Let's see how all these components interact together in another example. Let's take another customer service agent who asks a question to our agent. The input message is stored in our state, which is also storing some custom information about that shop: the opening hours. Our graph is then executed. The state goes into the start of the graph and it is then passed to the first node where a call to a language model is made and a new message is added to the graph state. After this, the flow of execution moves to the end node and the final state is returned, and we can return the output message to the user.
This is a very basic example, but LangGraph supports any architecture that you would like to build. It includes architectures like a router, where a conditional edge chooses which prompt should be executed based on the input to the graph. It supports a ReAct agent, where a language model can decide which tools should be executed. We can execute those tools as code locally, and the responses can be passed back into the language model for it to decide the next steps. It also supports the reflection pattern, which is where one language model is generating an output and another is reviewing that output and identifying any mistakes and passing feedback to the first to be able to correct them.
Practical Example: Setup and Dependencies
Now that we've covered the theory, let's take a look at how we can build these architectures in code. If you're liking this video so far, please like and subscribe as it'll motivate me to make more of this content.
First, we're going to need to install the requirements. If you've cloned the repository, you can do this with pip install -r requirements. If you haven't cloned the repository, you can install the two packages, langgraph and langchain-openai. If you're following along, you're also going to need an OpenAI API key for this tutorial, and you can get one of these by logging into your OpenAI account and going to platform.openai.com. We can then go to the API Keys tab and click on 'Create new secret key'.
First, we need to import all the classes and functions that we'll need for this tutorial. We're then going to create our graph state where we store all of the inputs, outputs, and variables we're going to need to access during the graph execution. We can then create our graph with this pre-built StateGraph class.
As this is a ReAct agent, we're going to need tools, and this agent will only be doing one thing, which is retrieving the weather at a particular location. So we can create a tool with the @tool decorator, which is provided by LangChain. You don't need to use LangChain, but we're going to use it here for convenience. I'm then creating a language model class with ChatOpenAI and putting in the API key that I retrieved earlier. I'm then binding the tools that I've created—the get_weather function—to this language model so the language model knows that this tool is available to use.
Defining Nodes and Conditional Edges
In order to provide these tools within the graph, we need to create a node, and we can do this with a pre-built node provided by LangGraph called ToolNode. So we create that, and then we're adding the node and naming it 'tool_node' to our existing graph.
Now that we've got our tool, we need the main entry node where we call our language model to see if we need to perform any tool calls. So I'm calling this 'prompt_node', and all that happens within this node is that we invoke our language model and we then add the response message into the graph state. When we respond with messages and a list, we then update the messages in the graph state with all of the new messages that have been returned. This happens automatically, and this is the way the state works in LangGraph.
To connect it all up together, we create a conditional edge which, depending on the output of the previous prompt node, if there are any tool calls then we will send the execution to the tool node. If there are no tool calls, we're going to end the execution then and return our response to the user. We're also setting the entry point to our graph which connects the start node to our prompt node.
Compiling and Running the Agent
Finally, I'm then compiling the graph and then invoking it with 'What's the weather in Yorkshire?'. And as expected, we're getting out that the weather in New Yorkshire is currently cold and wet.
If you're interested in learning more, I have another video on building a research agent. If you really like this video, I'd appreciate it if you liked and subscribed as it will motivate me to do more of this content. Thanks for watching, have a good day.