LangGraph Tutorial: Building Your First AI Agent Workflow with OpenAI

Analytics Vidhya · Intermediate ·🧠 Large Language Models ·5mo ago

Key Takeaways

The video demonstrates building a functional AI Agent Workflow using LangGraph and OpenAI, showcasing how to create an agent node, define reusable chat state, and invoke the LLM agent with conversation input. It covers the use of OpenAI's LLM model via APIs, environment variables, and the GPT-5 nano model.

Full Transcript

Hey there, it's good to see you. So let's continue our understanding of Lang graph. And in in this video, we are going to build a simple agent-based workflow using Langraph. So first let's understand what do we mean by an agent workflow. In our previous video, we have built a basic hello world kind of a workflow wherein we are mocking pretty much every single tool call or every single uh node that we were creating in order to understand what a basic workflow looks like. In this session however we are going to introduce our agent into one of our nodes. Now what exactly is an agent? Let's understand that. So the term agent basically is nothing but a large language model combined with some instruction and some tools together. Now as we know an LLM can or cannot have like the capabilities to perform the task that you are requesting it to. If the task it knows let's say you are doing something called as machine translation. Meaning you need the agent to trans to translate a document from language A to language B. Now such tasks the model already knows intrinsically. So you don't need an additional tool for the model to perform this. But on the other hand, if you want your model to able to book flights. Now this particular task the agent cannot perform on its own. It needs additional tools using which it can actually pass on the information from the user which is let's say what's the source, what's the destination, the dates and so on. using which it will call an API from any any any airlines let's say Qatar Airways or whichever right so any airways that you want to use using which it will book the flight based upon the user's behalf and this is what tools are nothing but the functionalities or the capabilities that you add on top of your LLM so that it can perform the tasks that are beyond its intrinsic capabilities and instructions are nothing but what are how that agent should perform right how are you instructing that agent to behave in any situation that's basically your instruction ction and agent is nothing but a collaboration of these three things. So what an agent does it the workflow is basically going to take a user input what are the reasons about it and decides what to do next. So that's basically what an agent does. It will read the input it will reason about it think what it needs to do and eventually take the decision. It might make a tool call or it might respond back based upon whatever question the user has asked. And in langraph the agent is basically a node in itself. So remember we have designed that workflow wherein you start off from uh initializing the state taking the user query and then calling an LLM agent here. So now we are going to replace this uh you know mock response with an actual LLM that is going to read the user input and respond back right and then we connect the nodes with others which are basically input node output node whatever postprocessing you want to add on top of your responses and whatever you want to do right so these basically is essentially a basic agent workflow in which the agent takes the instructions reasons and then decides to do next wherein it basically is a node in the entire workflow and the workflow then decides when to invoke that agent based upon on the precursor nodes outputs. So a single agent workflow will look something like this. So what a user will do a user will basically enter the graph as a part of the state. So whatever user message here we have we are going to incorporate that into our state and let that state flow into the successor node and the successor node will be an agent node which will read the message and the corresponding context of the message which includes the instructions and such and the agent node will then call the LLM and append its response into our state and the graph will then return the updated state including the answer from the node. That's basically how a simple agent workflow is going to behave. Now how do you design an agent node? So an agent node is supposed to have an input which is basically the current state that includes all the context and the messages from the user and then the core logic which is basically how that LLM is invoking. Now remember here we are going to use open AI's model. So here we are going to use the APIs provided by OpenAI to essentially start to interact with our agent. Here we are going to build a prompt which is basically the instruction that the model has to read and then internally the API will call the LLM and generate the responses for us and the output which is received from the API will be updated in the state with a new assistant message and the corresponding result from it. We'll see this in action in just a second and typically the prompt for agent follows these patterns right so first of all the agent is expected to have some system instructions consider these system instruction as basically the baseline behavior that you expect from your agent. So this sets up who the agent is. So it sets a persona for your agent and how it should behave when a user is interacting with it. It also needs a conversation history because remember it's a chat assistant. So here we will provide all the previous conversations so that the agent knows where it is right now and what it has already spoken to with the corresponding user. And lastly the latest user message which is the most recent input that you have received from your user. Right? That is basically the question to answer right now. And the agent workflow in Lang graph is going to include a list of chat messages. An agent node that converts state into an LLM call and new messages and edges which basically define what happens after the agent runs. So here after the agent run you can either response respond back to the user or you can call another node or you can do some sort of postprocessing into that particular response things like that. And the same pattern can be used for multiple agents later which we will take a look in just a second. So in order to observe and debug agent runs typically it is a good practice to print and log state before and after the agent node. So whenever you are invoking any agent always remember to add logging to know when an LLM has been invoked and what's the response from it. Also add exception handling to basically log any errors that have come while during the API calls. A lot of times you might have hit rate limiters or you might have exhausted your budget. So it's very important to have exception handling in place so that you can actually you know see and debug if any error has come up what's the reason for that and you can then read and debug your application accordingly and you also need to inspect the list of messages to see how the conversation evolves right so whenever you you are looking at the previous messages you can understand what are the previous you know inputs for the user and then how the agent has responded back to those questions as well and always remember use small and deterministic prompts while building your first graph do not over complicate Keep it simple because we are practicing. So hence we are going to keep these things for minimal as of now. So what are we going to code in this demo? So first of all we are going to extend our hello world graph into a simple chat style agent. Right? So we'll basically replace the mocked node where we are basically hard coding the response with an actual LLM agent call. Then we are going to define a reusable chat state with messages that we have done previously. We'll define this agent node that uses a real or you can even mock it but in this case we are going to use a real lf. And finally we'll run the graph with different user questions. So it's up to you which question you want to append there. Let's walk into the hands-on notebook next. So in this particular case since we are using the APIs from OpenAI the first thing that we need to have is basically you need to set your environment variable to contain the OpenAI API key. So always remember to have an OpenAI API key in your local environment. This is fairly straightforward. But you go to OpenAI's website, set up an environment variable uh create a new uh you know API key from there copy it and in in your local environment just paste it so that you can basically access it using this particular environment variable. Now to make things simple what you can do is basically you can create av file in your local working directory right so you can do something similar to this. Let me show you that real quick. So as you can see in my current working directory I have this env file created which when opened you will see that it contains the openi API key corresponding to my working directory. Now by the time you see this video this will be deleted but yeah so this is something you have to do similarly. So go to openai your project create a new API key and define an openi API key environment variable in this env file which is basically going to set it up for you. Once you have done that, once you have set your environment variable, then you can use the package envy which is basically a built-in package. You do not need to install it separately, you need to load this function load environment variables defined in that env file. If the output from this function is true, it means all the environment variables have been successfully loaded. Then you can import the corresponding packages. So here we are using OpenAI. So hence I'm going to use the class OpenAI from the package OpenAI create a client which will contain your API key and then we are going to define a simple function which is going to invoke my client every time I get a conversation input. So here call for LLM agent is basically the function that is going to be called every time I want to generate a response from this agent. So what the text means this is basically all the conversation that the corresponding agent will respond on top of and here we can use the chat API from this particular client here chat.comp completions.create and in this case you will define the model that you have to use. So for this demonstration I'm using GPT5 nano. The messages will contain all the messages that you need to send. So here you can see I have defined a role which is the system role for this agent and the content in this particular case is the basic instruction that we spoke about. This is the system instruction that we talk about. So system instruction you can clearly read it says you are a helpful assistant explaining langraph and LLM workflows to beginners in very simple clear language. Right? So this is basically an overview of what we expect the agent to do. And then we are adding another message in a conversation which is coming as a input from the user which contains the conversation text that is all the text that you want the agent to respond on top of. And then here you can see we have simply told the agent to respond to the user's question clearly and concisely and then we are returning back whatever response we are getting from the agent. So this is basically how you get the content back from the response which you have taken up. define this function and next up we are going to create our state. So this state is basically as I mentioned previously we typically initialize it as a type dictionary. Total false indicates that you can have basically all the inputs are optional. Messages is going to contain all the previous messages. The result is the final output and what's the user input which is the initial user message. We are going to define a state and then you can see we have created a user node. This is basically the input node that the user will start the interaction with. So we are going to pass the state. We are going to get the message whatever the user text is that is the initial user input. In this case we are setting up a default input but you can get any input based upon what the API is returning. Then we are going to append this message as a user message and then we'll update the state and return the updated state back. After that we are basically defining our agent node which is basically containing the agent itself. Again we are going to get all the messages. We are going to create a conversation history which is going to append all the messages that we have received till now right if not then we'll just create a default message which is basically to explain lang graph in simple terms and then we will call our LLM agent pass the input conversation that has been created and whatever response we have received we are going to append it in our conversation history as an assistant response which is the agent response and then we'll update our state return the result and the messages that we have received from the previous call and finally It's time to build up our graph. So here you will notice I'm again instantiating my state graph and here it is going to take in the state which is the object of chat state which we have defined above. We'll first add both of our nodes. One is for user, second is for agent. We'll set the entry point as a user node which is basically the how the interaction will begin. Then we'll add an edge between the user and the agent and then finally we'll add an edge between agent and the end result. So end is basically the end of conversation. We'll compile our graph and let's see what the final app look like. So now you can see this is the completed workflow. So we'll start the conversation. It will go to the user. User will forward it to the agent and then we'll end the conversation there. To initialize our state, we'll basically set a messages as an empty list and the user input is the initial instruction with the user has provided and then we'll invoke our app with the initial state. We'll let the model generate its output and we'll see what the final result looks like. Remember this this call can take some time depending upon the model and the the the complexity of the instruction right so since you are calling an external API so it can take some time but you will notice here this is what the conversation and output looks like so in our final state we have these three results so conversation is explain the lang graph and the assistant has responded back with the entire you know response to what lang graph is and what it does what it doesn't do right so here all of this is basically the response generated by the the model itself right so everything is basically basically the output generated by the model as an exercise. Here you can see we have an updated uh query as well which you can run later on and see how the overall response changes when you send a second user input here. And that's all for this particular hands-on. I'll see you in the next video. Thank you very much.

Original Description

In this lesson, we take the next step in our LangGraph journey by moving from "Hello World" mocks to building a functional AI Agent Workflow powered by OpenAI! 🚀 What is an AI Agent? As explained in the video, an agent is more than just an LLM. It is the combination of a Large Language Model, specific instructions (persona), and tools that allow it to perform tasks beyond its original training. What we cover in this tutorial: 1. The Anatomy of an Agent: Understanding the relationship between LLMs, instructions, and tools. 2. Agent Nodes: How to design a LangGraph node that reasons, processes user input, and generates a real response. 3. API Integration: Setting up your environment with .env files and connecting the OpenAI Python client. 4. State Management: Passing conversation history and context through the graph to ensure a seamless chat experience. 5. Workflow Orchestration: Defining the entry point, user nodes, agent nodes, and edges to complete the cycle. By the end of this video, you will have a working Python application that uses LangGraph to orchestrate a conversation between a user and a GPT-powered assistant. Prerequisites: Basic Python knowledge. An OpenAI API Key. Check out the previous "Foundations of LangGraph" videos if you're new! #LangGraph #AIAgents #OpenAI #LLM #LangChain #PythonTutorial #GenerativeAI #aiworkflows 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 build a functional AI Agent Workflow using LangGraph and OpenAI, covering the creation of an agent node, reusable chat state, and invocation of the LLM agent. It provides a hands-on approach to integrating LLMs with agent workflows.

Key Takeaways
  1. Extend hello world graph into a simple chat style agent
  2. Replace mocked node with actual LLM agent call
  3. Define reusable chat state with messages
  4. Define agent node
  5. Set environment variable with OpenAI API key
  6. Load environment variables with envy package
  7. Create OpenAI client with API key
  8. Define function to invoke LLM agent
  9. Use chat API with GPT-5 nano model
💡 The key to building a functional AI Agent Workflow is to integrate the LLM with the agent workflow, using conversation history and user input to craft effective prompts and invoke the LLM agent.

Related Reads

📰
AI Memory Is Not Just a Bigger Context Window
Learn why AI memory is more complex than just increasing the context window and how it impacts LLM performance
Dev.to · Paul Crinigan
📰
Enterprise AI Agency strategy
Learn how to implement a comprehensive Enterprise AI Agency strategy using cloud-native infrastructure and scalable data pipelines to drive AI-driven decision-making
Dev.to AI
📰
Fable 5 Just Shipped: What Anthropic's Newest Model Means for Developers
Learn about Anthropic's newest model, Fable 5, and its implications for developers
Dev.to · Pavel Espitia
📰
Apple Intelligence approved for launch in China with Alibaba’s Qwen AI
Apple's AI services are launching in China through a partnership with Alibaba, bringing Qwen AI models to Apple's operating systems
TechCrunch AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →