LangGraph Tutorial: Understanding Nodes, Edges, and State Management

Analytics Vidhya · Intermediate ·🤖 AI Agents & Automation ·4mo ago

Key Takeaways

The video demonstrates the use of LangGraph, a framework for building complex, agentic LLM workflows, and covers the fundamental building blocks of nodes, edges, and state management. It provides a step-by-step guide on designing a LangGraph workflow, implementing state management, and creating a graph over a chat state.

Full Transcript

Hey there, it's good to see you. In the last class, we have seen a bird's eye view of what lang graph is and how it builds up on top of lang chain in order to provide you the framework to create end to end agentic workflows. Today we are going to understand some of the basics of langra terminologies and what each one of them means when designing this workflow. So without further delay let's get started. So to understand there are four core concepts in any langraph workflow or a lang graph graph to say the first key understanding is basically of the terminology called graphs. So a lang graph graph is basically the entire workflow that you create which comprises of nodes and edges. In the in the hands-on for today, we are going to design a very simple hello world workflow which will comprise of this graphical structure in which we are going to add some few nodes and edges to help you understand the same. So a graph is nothing but a pictorial representation of the entire end to- end workflow which you have. For example, imagine a graph would look something like this wherein you would have a particular starting node. Then based upon the input from the starting node, you would move to the second node. Then from that you would move to the third node and so on. So here you are basically designing a workflow wherein your data flows from one node to another and these nodes are connected via edges right and this entire workflow is basically your graph and these individual points are basically your nodes. So whenever building any workflow in langraph we are basically designing an end toend graphical workflow which helps us navigate the different functionalities which we are designing inside our work. So that's the first core concept which is of graph. We'll learn more about graphs as we go on and we'll try to implement more functionalities around the same. And graph basically includes nodes and edges. So nodes are nothing but the individual steps that you define. These steps could be basic functions or agent invocation or tool calls as well. Right? So you can design any particular node to have any either of these three things. A function is basically just a Python function which takes any input and returns you an output. Right? basic functions which we have. You can even invoke agents in between. So say for example after taking some user input you want to invoke an agent to answer the user queries. Right? So here the user might ask you okay plan me an itinary for for a particular country. Then you can invoke an agent to to give the response back which contains the itineraries. Right? So a a node can be containing an agent or it can even contain tools. As I mentioned, a lot of times your agent might not have the intrinsic capability of performing tasks that you have requested. In such cases, we can actually take it one step ahead, right? Wherein we can create tools which your root agent can invoke to get the responses back, right? So, it can contain any one of these three things. And as I mentioned previously, these nodes are connected together via edges which are rules that decide when the flow should move from node one to node two, from node two to node 3 and so on. And the last core concept is of a state. Now state is nothing but the core data which contains all the information which supposed to flow throughout your workflow. We'll see this in practice in just a second. But the core idea here is that the workflow always requires some shared data. A very common example it could be let's say if you're designing a chat assistant, you need to store all the messages that have come between the agent and the user, right? You need a way to store and let the following agent know what previous conversation is. So you can store everything inside a state or if you want certain variables say for example the user is requesting you to create an itinary for Indonesia and you need to store it in a variable so that the following tool calls can know which country you have requested the itinary for. So in that case you can create a country variable inside your state which will then pass on that information to any following nodes in that end toend workflow. That's basically the concept of a state which is a shared data that flows throughout the graph as long as the workflow is running. Now a typical workflow looks something like this. You start off by defining a state right? So as I mentioned state is basically that shared data which flows through your workflow. You always have to start off by defining that state. So in in langraph you typically use type dedict or data classes in Python to define these states. Right? So they these are the two primary ways of instantiating states inside lang graph. We'll see type date and how it is used in a second. And then after you have initiated your states you then write the node function. So these are basically the individual nodes that you contain in a graph. Then once all the nodes are ready then you wire all those nodes together with your edges right and finally once this entire structure is ready then you build the graph which puts or you which compacts everything together into a single data called graph. And then once everything is ready then we can invoke the agent with the initial input initial state or input and then the workflow will continue from there. So in the hands-on what are we going to do? So remember this is the first practice. So we'll keep things simple here. Here we are going to see how you can create a simple state right using a chat style interaction. So basically here we are defining this workflow for interacting with the agent right? So that's what the core goal is. We'll then implement the node functions right. So here we'll write the input node and llm node. You'll see that both of these will basically be uh like so we'll not actually invoke an LLM here. So all of this will be an abstraction right. So here we are going to hardcode some of the things just to keep it simple. In the coming hands on we will slowly keep adding complexities to these workflows and then once we everything is ready we are going to then graph it up right and then initiate the graph with a initial state and then we'll see what the output looks like after the execution and this is what the workflow will look like right so the user will have some question so the starting node will receive the user input in this particular case we are going to see how it will look like when you call an actual LM we are going to improve the state with the output from the LM and then we will see what the final response looks like. So these are three states that we will work through in order to achieve the final answer. Right? So this basically is typically how the execution looks like in any workflow. Right? So you have an entry node which starts the execution. Then all the intermediate nodes essentially are connected via the edges. Edges decide which nodes is supposed to run next. Right? So based on the edges you are going to decide which node is supposed to be invoked. Right? And each node will receive the current state and return the updated state. So typically every single node receives the current state of your shared state variable and then it will update it to return you the updated state back. Right? So this basically happens in every single node and the execution stops when there are no more nodes to be executed obviously. Right? So when you reach the end the execution will stop and a state is essentially as I said it's typically a dictionary like object and here we are going to use type dict from typing basically that is basically the very commonly used dictionary format wherein you can type the all the attributes and assign which all the attributes are mandatory which are not mandatory and things it's a very decent way to initiate any state of dictionary and then here we there are certain things that we have to remember the first thing is that nodes should always use states to pass along information. Never use global variables. That's a very bad practice. That's why states are there so that you can share information across different nodes in a workflow. And last but not the least, these states make flow easier to debug and test. So once you have updated the state, you can add logging information within each node to let you know what the current state looks like and what the update actually is in the current information that you have passed. Right? So that makes the debugging pretty much easier and streamlined as well. And in this entire workflow we have discussed previously is this lang graph is built on top of lang chain. So lang chain basically has the primitives like models tools and retrievers that basically is used to define the individual nodes in langraph. Right? So inside the nodes you can have chains or tools. So you can create a chain using invocation wherein you invoke a model get the response back or you can also create tools which are again defined using lang chain intake. So lang graph is nothing but your orchestration that that's the framework that supports orchestration and the lang chain. So this is sorry this is basically lang chain and lang chain supports integration right. So lang graph is for orchestration and lang chain is basically the internal which is uses for integrating all these different aspects together. So now it's time to jump into the hands. So in this notebook we are going to see how we can create a simple hello world graph using langraph. In this notebook, we are going to uh first install. So remember in my environment, I have already have all the dependencies installed. So I don't need to install again. But when running for the first time, remember to execute these install commands to to to let all the dependencies installed for right. So here we have lang graph, lang chain, open and tick token. And in this case, we are then going to define a simple state. We are going to create two nodes. One is an input node that will basically add a user message to a state. Meaning here we are going to instantiate a state based upon the initial input from a user and an LLM node which is going to basically mock the placeholder. It's it's like a placeholder that is mocking how an actual LLM would look like or they can simulate how we use an actual LLM when we do not have a simulation in place. Right? So that's basically the core idea here. [snorts] So to kick things off here we are going to import right. So here as I mentioned to initiate states always remember to use type dict right here we are going to also import certain other data types like lists and optional. So type dict is basically what we are going to use to initiate our chat state. So here we are creating a new class called chat state that that imports typed dictal is basically just to ensure that all the variables here are optional not it's not like you need every single variable at a time of instantiating your state. So it's typically you know recommended to set it to false when you want certain attributes of your dictionary to be optional. So here we are going to initialize our state variable. So let me just initiate this whole thing. So after this now our state variable has been initialized. Then it's time to define our nodes. So here you are going to see I'm going to define my input node and an LLM node. Both of these are basic Python functions and both of these are receiving the states as an input. Remember here we are going to pass the state to every single node so that the node can utilize the information available in the state and modify if and when it's required. Now the role of input node is to basically append the user message into your state. Whatever input the user is giving this node is supposed to add a new user message to the state so that the followon nodes can use that input to generate the output. Right? So in a real app this would come from UI. Right? So remember that here we are manually setting it. But when you are using it in an actual application, you are going to take this from the incoming API or the UI that is exposing the interaction. Right? So here we are going to simply hardcode a question for the demo. And in this case we are going to first check what the current message looks like. If the messages are not set obviously when you are initializing there will be no messages here. It will be an empty list. So therefore here we are going to create an empty list and then simply we are appending a new user message which is basically what is lang graph in simple terms. basic input user message that we are setting here and then what it is returning back is basically the original state along with the appended messages that we have created remember here that whatever the previous state was I'm overwriting all the information from the previous state with the new information that we have created here and then the second node is an LLM node which is simulating how you can call an LLM here right and append the response in the same state so here again we are taking the state as an input again we are checking the messages so obviously this time we have the messages variable available. So therefore in this particular case here we are going to use that message but this time we are not going to call an LLM rather we are just mocking an answer here which contains what langraph actually is and then I'm appending a new message here. Here you will see that here we are appending a new message from assistant which is basically containing the mock answer that we have received and this everything will be tied up together. So the previous state along with the updated messages and what the actual result looks like from this state, right? So everything is passed on together as the output from an NLM node. So as mentioned previously, a node will receive the state, update it, modify it and return the updated state back. So here we are going to define both of our nodes and then finally we are ready to build our graph. Here to define this graph we are using lang graph as always and the flow is going to be input node, llm node and ending. It's a simple basic hello world graph. So to import this we are going to use a state graph from lang graph's graph package. Right? So this exposes a different type of graphs. Here we are going to import the lang graph and an endpoint to indicate that this is an ending of a graph. We'll use both of them in a minute. So what we do here is basically we create a graph over the chat state. So this is basically letting the graph know which is the shared state that it's supposed to track. So over here end to end it's supposed to track all the information from your chat state and then we are adding up both of our nodes. So the first is your input node. This is just an identifier. This is like a text identifier for the node. This will contain your input node. Then we have a second node called LLM node which contains your LLM node. So these are the nodes that we have added. Now it's time to define your edges or the connections between these nodes. So obviously we are first setting up an entry point. This is basically telling the graph where you interact or when you when you start it which is the first node that you always call. So in this case we are starting off with the input node. Then we are adding a edge between an input node to an LLM node which tells the mod graph that you need to navigate from input to LLM whenever available. And then finally we add an edge to your LLM node which indicates the ending of your code. So after the response is received from the LLM you basically end your entire flow. And once this graph is ready, we then compile it to basically you know link everything together and you app you have your app ready. So let me print it out to see what it looks like in after you have initiated this entire graph. So you will notice that here this is what the graph looks like. So you have start which goes to the input then the input goes to your LLM and then LLM responds back and this is the end of our simple graph. And finally we are then ready to run our graph. So we initialize our state. So remember initially the state is just going to have an empty list of messages. I don't have any any new updates here. And then we are going to invoke our app. Invoking app means that you need to run your entire end to end workflow. Here you are passing your initial state because this is what the graph is expecting, right? So it's it's expecting your state as an input. And then after everything is done, we are then printing what the final state looks like. And then we can even iterate through every single message in our messages queue to see what different messages are available. Okay, let's run this and you will see that this is the final state. In the final state, you have a user query which is what is langraph and then an assistant response and then finally you have the result back and the conversation trace is nothing but what different information have gone through uh you know has has gone through your messages. So we are iterating through every single one and telling you and we are just logging what is the input and what is the assistant response. Remember this was just a demo to show you how you can create a basic graph using langra. In the coming videos, we'll make it bit more complicated and adding LLMs to it as well. So, I'll see you in the next lecture. Thank you very much and I'll see you around.

Original Description

In this video, we dive deep into the fundamental building blocks of LangGraph, the powerful framework for building complex, agentic LLM workflows. 🚀 If you want to move beyond simple linear chains and build advanced AI applications that can think, loop, and remember, understanding these four core concepts is essential. What you will learn in this lesson: The 4 Pillars of LangGraph: 1. Graphs: Pictorial representations of your end-to-end workflow. 2. Nodes: Individual steps like Python functions, agent invocations, or tool calls. 3. Edges: The rules and logic that connect nodes and define the flow. 4. State: The shared data object (using TypedDict) that persists across your graph. The Development Workflow: How to define state, write node functions, wire edges, and compile your graph. Hands-on Demo: Follow along as we build a "Hello World" LangGraph application that simulates a chat interaction from scratch. Prerequisites: Basic knowledge of Python and familiarity with LangChain. This is part of our series on the Foundations of LangGraph. Don't forget to like and subscribe to stay updated on future modules where we add LLMs and real-world tools to these workflows! #LangGraph #AIAgents #LangChain #Python #GenerativeAI #LLMWorkflows #machinelearning We will discuss the following- langgraph multi agent langgraph agents langgraph project langgraph playlist langgraph and langchain langgraph studio langgraph ai agents langgraph multi agent tutorial langgraph chatbot langgraph multi agent project langgraph memory langgraph js
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Analytics Vidhya · Analytics Vidhya · 0 of 60

← Previous Next →
1 The DataHour: Data Science in Retail
The DataHour: Data Science in Retail
Analytics Vidhya
2 The DataHour: Anomaly detection using NLP and Predictive Modeling
The DataHour: Anomaly detection using NLP and Predictive Modeling
Analytics Vidhya
3 The DataHour: Energy Data Science Project from Scratch
The DataHour: Energy Data Science Project from Scratch
Analytics Vidhya
4 The DataHour: Explainable AI Need and Implementation
The DataHour: Explainable AI Need and Implementation
Analytics Vidhya
5 The DataHour: Google Cloud AI/ML
The DataHour: Google Cloud AI/ML
Analytics Vidhya
6 Prediction to Production in Machine Learning #machinelearning #prediction
Prediction to Production in Machine Learning #machinelearning #prediction
Analytics Vidhya
7 Practical Applications of Data science in Ecommerce
Practical Applications of Data science in Ecommerce
Analytics Vidhya
8 How to tackle Overfitting?#machinelearning #overfitting
How to tackle Overfitting?#machinelearning #overfitting
Analytics Vidhya
9 Building Data Pipelines on GCP #googlecloud #datapipelines #data
Building Data Pipelines on GCP #googlecloud #datapipelines #data
Analytics Vidhya
10 Hands-on with A/B Testing #abtesting #datascience
Hands-on with A/B Testing #abtesting #datascience
Analytics Vidhya
11 Efficient Implementations of Transformers #transformers #cnn  #machinelearning
Efficient Implementations of Transformers #transformers #cnn #machinelearning
Analytics Vidhya
12 Modern Deep Learning Architecture #deeplearning  #architecture #deeplearningtutorial
Modern Deep Learning Architecture #deeplearning #architecture #deeplearningtutorial
Analytics Vidhya
13 Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Analytics Vidhya
14 5 things you should know about Azure SQL #azure #sql #datahour #datascience
5 things you should know about Azure SQL #azure #sql #datahour #datascience
Analytics Vidhya
15 AI & ML in the Automotive Industry #machinelearning #ai
AI & ML in the Automotive Industry #machinelearning #ai
Analytics Vidhya
16 Building Machine Learning Models in BigQuery
Building Machine Learning Models in BigQuery
Analytics Vidhya
17 NLP aspects in Telecommunication Industry
NLP aspects in Telecommunication Industry
Analytics Vidhya
18 Practical Time Series Analysis
Practical Time Series Analysis
Analytics Vidhya
19 Fundamentals of Quantum Computing
Fundamentals of Quantum Computing
Analytics Vidhya
20 A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
Analytics Vidhya
21 Classification Machine Learning Model from Scratch
Classification Machine Learning Model from Scratch
Analytics Vidhya
22 Knowledge Graph Solutions using Neo4j
Knowledge Graph Solutions using Neo4j
Analytics Vidhya
23 Model Guesstimation (MLOps)
Model Guesstimation (MLOps)
Analytics Vidhya
24 ETL Pipelines in Google Cloud Platform
ETL Pipelines in Google Cloud Platform
Analytics Vidhya
25 Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Analytics Vidhya
26 Getting Started with AWS EC2 #amazon #aws
Getting Started with AWS EC2 #amazon #aws
Analytics Vidhya
27 How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
Analytics Vidhya
28 Certified AI & ML BlackBelt Plus Program #shorts
Certified AI & ML BlackBelt Plus Program #shorts
Analytics Vidhya
29 Visualizing Data using Python #machinelearning #visualization #python
Visualizing Data using Python #machinelearning #visualization #python
Analytics Vidhya
30 DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
Analytics Vidhya
31 M in ML stands for Math & Magic
M in ML stands for Math & Magic
Analytics Vidhya
32 An Unsupervised ML approach using Clustering
An Unsupervised ML approach using Clustering
Analytics Vidhya
33 Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Analytics Vidhya
34 Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Analytics Vidhya
35 Practical MLOps #mlops #datascience
Practical MLOps #mlops #datascience
Analytics Vidhya
36 Data Engineering with Databricks #dataengineering #databricks
Data Engineering with Databricks #dataengineering #databricks
Analytics Vidhya
37 Multi-Objective Optimisation
Multi-Objective Optimisation
Analytics Vidhya
38 When Airflow Meets Kubernetes
When Airflow Meets Kubernetes
Analytics Vidhya
39 AI in Banking
AI in Banking
Analytics Vidhya
40 Learn Convolutional Neural Network for Image Recognition
Learn Convolutional Neural Network for Image Recognition
Analytics Vidhya
41 Extracting Value from Data
Extracting Value from Data
Analytics Vidhya
42 How to measure Marketing Channel Effectiveness
How to measure Marketing Channel Effectiveness
Analytics Vidhya
43 Transforming Lives | Data Science Immersive Bootcamp
Transforming Lives | Data Science Immersive Bootcamp
Analytics Vidhya
44 Stock Market Analysis - AI driven approach
Stock Market Analysis - AI driven approach
Analytics Vidhya
45 Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Analytics Vidhya
46 Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Analytics Vidhya
47 The Power of Visualization | Tableau Full Course | Analytics Vidhya
The Power of Visualization | Tableau Full Course | Analytics Vidhya
Analytics Vidhya
48 Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Analytics Vidhya
49 Data Visualization in Data Science | DataHour | Analytics Vidhya
Data Visualization in Data Science | DataHour | Analytics Vidhya
Analytics Vidhya
50 Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Analytics Vidhya
51 Solving any Machine Learning Problem | Approach and Steps Involved
Solving any Machine Learning Problem | Approach and Steps Involved
Analytics Vidhya
52 Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Analytics Vidhya
53 Data Engineering in E-Commerce | The Best Case Study
Data Engineering in E-Commerce | The Best Case Study
Analytics Vidhya
54 Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Analytics Vidhya
55 Introduction to Federated Learning | DataHour | Analytics Vidhya
Introduction to Federated Learning | DataHour | Analytics Vidhya
Analytics Vidhya
56 Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Analytics Vidhya
57 Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Analytics Vidhya
58 Learn Hypothesis Testing | DataHour | Analytics Vidhya
Learn Hypothesis Testing | DataHour | Analytics Vidhya
Analytics Vidhya
59 A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
Analytics Vidhya
60 Making AI work for Business | DataHour | Analytics Vidhya
Making AI work for Business | DataHour | Analytics Vidhya
Analytics Vidhya

This video teaches how to design and implement a LangGraph workflow, including nodes, edges, and state management, to build complex, agentic LLM workflows. It provides a step-by-step guide on creating a graph over a chat state and demonstrates the end-to-end workflow.

Key Takeaways
  1. Design a LangGraph workflow
  2. Add nodes and edges to the workflow
  3. Implement state management in the workflow
  4. Define a state using type dedict or data classes in Python
  5. Write node functions for the graph
  6. Wire nodes together with edges
  7. Build the graph
  8. Invoke the agent with the initial input and state
  9. Create a graph over the chat state
  10. Add nodes for input and LLM
💡 LangGraph is built on top of LangChain, which has primitives like models, tools, and retrievers, and is used for orchestration, while LangChain is used for integration.

Related Reads

📰
BizNode workflow chains: /cw to create, /rw to run. BZeUSD escrow enforced. If any step fails, full rollback — your funds...
Learn to automate complex business processes with BizNode workflow chains using /cw and /rw commands
Dev.to AI
📰
Every Telegram conversation becomes a qualified lead. BizNode captures name, email, and business details automatically while...
Use BizNode to automate lead capture from Telegram conversations, leveraging AI for smarter business operations
Dev.to AI
📰
Genkit Agents: Why the Whole Is Greater Than the Sum of Its Parts
Learn how Genkit's agent framework combines interrupts, middleware, sessions, and streaming to create production-grade AI agents
Dev.to · middleware.mind
📰
Run MiniMax models on Amazon Bedrock
Run MiniMax models on Amazon Bedrock for scalable and secure machine learning workloads
AWS Machine Learning
Up next
AI can support review workflows, but quality still needs human oversight | ARDEM Incorporated
ARDEM Incorporated
Watch →