LangGraph Tutorial: Integrating External Tools and APIs into AI Agents

Analytics Vidhya · Intermediate ·🔧 Backend Engineering ·5mo ago

Key Takeaways

This video tutorial demonstrates how to integrate external tools and APIs into AI agents using LangGraph, enabling agents to interact with the real world and perform tasks beyond text generation. Tools such as calculator, file reader, web search, and database querying are integrated into LangGraph workflows to extend agent capabilities.

Full Transcript

Hi there, it's good to see you again. Let's continue our journey of understanding Langraph. In this video, we are going to talk about how we can integrate external tools within a langraph workflow. Let's begin by talking about what exactly is a tool. Now, we have described this previously as well. Tool is nothing but a function which an agent can use to get real world data or actions. Right? So this basically is a function which allows an agent to perform some actions which are beyond its intrinsic capabilities. Right? So these tools extend the agent beyond pure text generation. Right? Typically you expect a large language model to generate text. Right? Tasks like machine translation, text summarization, im information extraction, right? So all of these are something which are very much naive for a large language model. It is trained to do these. But if you wanted to perform tasks which are not that you know textori oriented that's where basically tools help you out a lot. Say for example you want to add web search capabilities to your agent or image generation or capabilities that allow your agent to query data from a database or read data from a file. Right? So all of these are basically functions which you can provide to your agent and the agent can then use those functions to perform these actions. So how are tools integrated in a langraph workflow? Right. So typically tools are designed as a dedicated node. So just like we have nodes that describe our agent, we have nodes to describe right. So the user input and such, we can even add nodes dedicated for tool calls and then agent will decide when it needs to invoke that particular tool or call that particular node. We have an edge connecting that and it is basically we it is it is going to decide whether or not this edge will decide if it wants to invoke that particular tool or not. Right? And the tool results are then written back into the state which can be used by the later nodes. And typically that's how a workflow within a tool is basically integrated. Now here we are going to integrate some basic tools. In this demo we are going to use a calculator tool and integrate this calculator tool in our workflow. But you can use similar example to integrate even a file reader or a web search tool over a vector store as well. Right? So here all of these are different examples that you can integrate and a calculator tool is a very primitive tool which when required the agent can pass expressions and then the calculator can then return the results based upon that expression. So here the tool nodes is basically designed as follows right. So obviously the first is input. Now input is basically the relevant fields from the state. Right? So this input will contain all the fields from the state. Say for example for our calculator tool we require the expression to be calculated. This is going to be our input for that particular tool. Similarly, for a file reader tool, we will require this file path. Right? So, this is going to be required for a file reader tool. Similarly, if you are basically designing a tool which can execute SQL queries, then you are going to require a SQL expression as an input. Right? So, therefore, every single tool will have will require its own inputs just like how Python functions operate. Then comes the core logic which is nothing but the underlying Python functionality which performs that tasks. the calculator meaning you need an eval expression that can evaluate the expression or a SQL engine that connects to the database executes the query and returns you the output right so that core logic is what defines the actual function logic and the final output is basically then returned back into the state under a clear key which can be used by the later nodes in the workflow so how these agent and tool interact with one another that's basically the next question that's supposed to be answered so what happens is that the agent is going to inspect the state and decide whether a tool is needed so basically there will be a decide a node which is going to decide if tool is needed or not and it can basically update the state and the updated state which can be read by the workflow will decide if this tool is supposed to be invoked or not right and the agent may set flags or write request into the state itself and then the graph will route to a tool node and then back to the agent or to the output node. So bas based upon how your flow has been designed. So once an tool invocation is complete that will redirect back to the agent or to an output node whichever basically has been designed in the workflow. So what are we going to code in this video? So in this particular demo we'll add a simple calculator tool node to the graph. Let the agent request a calculation based on the user input. So we'll see how we can let the agent decide if this tool call is supposed to be invoked and then we'll display the answer combine the tool output and the natural line. Let's dive into the notebook now. So now in this notebook we are going to be integrating an external tool within our LLM planner workflow. So this planner workflow is nothing but essentially an agent which will decide when it needs to invoke an agent. So the goal of this notebook is to have a node which can contain the user input. Then a planner agent which will decide whether a tool is required to be used and what are the arguments for that tool. What are the inputs for our tool? Then we'll have a dedicated tool node which is our calculator. In this case, this tool node will execute the operation which is provided in the expression and the agent will then answer back and it will explain the results in a natural language as well. So this is basically how the flow is going to look like. The user node will take the query. The planner node will then plan whether the tool is supposed to be executed and based on the conditional routing if the tool call is requested will then execute the tool call and then finally the result from the tool call will be displayed in natural language by the agent itself. Right? So as always we are going to first set up our open AI. Now here one thing that we have to do is we have to load our environment variables. So here we are going to import the env load.env first. So this is going to load our environment variable or the openi API key which is going to be required for our operations. Then we'll set up our openi client which will allow us to execute any particular operation using the openi API and run an agent in the back end. In the meantime, let's see what the state looks like. So this time the tool state that we are defining has messages that is going to contain a list of the interactions between the user and the assistant. User input which contains the initial user input the tool and now this is basically optional. Now this is going to be set by the planner agent if and when it requires us to use that tool. Right? So it is an optional. So this is basically going to set which which tool is supposed to be used. None will indicate no tool call is requested. And then if a tool call is requested then we will set the expression here. Right? So if this particular tool is calculator then it's going to evaluate this expression here. And then the calculation result is basically the final output that we have gotten from the result. And the result is the ultimate output. So this calculation result is the tool output. And this is the final response that we receive from the last node in our workflow. Let's run the state and let's continue there. After that it's time to define our user node. Now this is the first node and as always this will contain the initial user message right. So this will this is going to contain the so we we are going to read the messages first. The user text is basically the input that we have received and then we are simply going to append our user message using the user key and then we are going to update the state with the updated messages here. Next up we are going to define our calculator tool node. Now this is a very straightforward node. Now in this particular tool what we are doing is we are using the eval package from Python which the function is basically provided in Python that takes in an expression and simply returns the output for that evaluation. Here we are defining certain basic operations that are supported. So addition, subtraction, multiplication, division, power and and a and a negative subtraction. Right? So all of these are available and here what we are just doing is we are simply defining a function which is going to evaluate the different expressions. Right? So this is essentially what we are going to invoke at the time of evaluating an input expression string. Right? So all of these are expressions that can be evaluated and that's what we are doing here. And then over here this is the node tool node that we are writing. Now in this case what we are doing is we are basically getting the expression first right. So here from the state we are getting the calculation expression. Right? So this is the tool state and the messages we are getting the previous list of messages. We are then checking if the expression is existing or not. If there is no expression then obviously there is no expression provided but if it is there then we are passing the expression to our evaluation function which we wrote above right and then this will return us the value and then we are simply appending a message from the tool call saying that the tool has evaluated the expression to the corresponding value we'll then append the state by adding a new tool response right so just like we have response from the assistant we have a response from the tool here so hence we are appending that and then we are returning back the updated state like any other node does. Then we have a planner agent node which decide if a tool call is required or not. So here this particular agent will decide if it is required to use the calculator tool or nothing else. So if you look closely here there's a planner agent node and this planner agent node will take the user input and then we are getting a prompt like we are sending this prompt to the agent. We are saying to the agent that you need to decide if you need to use a tool based upon the user input. Right? It has two tools right? So in this case it has a calculator tool available. Now in this case this tool is supposed to be invoked when the user asks us to compute a numerical expression. Right? So whenever the user has given us to compute something we are going to invoke this calculator tool. Then we are giving what the user question is and after that what it needs to return back. So it needs to return a JSON object which contains the tool key that contains either calculator or none and the actual expression which is supposed to be you know executed when this particular tool is invoked. Right? So here you can see we are then asking the model to complete this response. We are setting up the system role right. So the input and everything prompt and all are going in right and then finally in this case we are letting the message to complete and then we are then loading the JSON which we have received as an output right and after that we are simply getting the two keys which we are getting the responses. So tool key to let us know which tool is used and the expression key to let us know what expression is supposed to be invoked. And then finally we are setting a response from the planner side here and letting the agent and letting the messages append itself in order to know what is the next plan of action. We'll execute this as well. And finally here this is the last and final tool answer. This is basically going to finally give us the answer back. Right? So here this is an open AI chat model to format the final response. So if the calculated result is none, it means that there is no response as such. Or else we are then going to you know create a user query and the we are use we are using another model to basically summarize what the output looks like and then an answer agent node will essentially answer us back based upon the user input. Right? So this answer agent will basically then pass this entire answer back to the function and then the model will then return back the answer and that is what we are going to append as an assistant response. That's pretty much it. So these are the different nodes that we have and now it's time to add these nodes together. So in the graph we are adding these nodes one by one. So each node gets added first. The entry point is set to be the user node and then we are adding an edge between user and planner. So every time we have an input we are going to plan what's supposed to be done next. So hence a definitive edge between these two and then after that we are deciding what to do after the planner. If the state contains a tool call then in that case it's returning the calculator else it is returning the answer agent and then we are adding a conditional edge between the planner right so from planner it is going to route to a calculator or an answer agent depending upon the output from route after planner and you can see here we are then adding an edge from calculator to the answer agent and after that we are also adding an edge after the answer agent to end meaning that it will complete the workflow we'll then compile everything off and we'll try out some a bunch of other queries here. Right? So let's complete it and see what the updated graph looks like. And now you can see clearly that we have basically the user input going to the planner. Planner decides whether to invoke the calculator or not. After invoking the calculator, the answer agent then summarizes the response. If this calculator is not required, the flow directly goes from the planner to the answer agent. Right? So that's basically the workflow that we have created. Then we have a couple of queries that we want to try out. So here the first query is an expression and the second query is just a chitchat right so it's not nothing specific to a query as such we'll iterate through both of them and see what the responses look like right so this is the first input that we gave to our agent and let's see what the sequence of flow looks like. So next up we are going to try a couple of queries to see how this workflow is working. So here we have two queries. One of which contains calculation and the second one which is basically asking the model to respond to a general query. Here we are going to iterate through both of these queries. See what the state looks like. Right? So here we are going to initiate the state, get the result back from the workflow and then iterate through every single message from the response. Print out those messages and see what the result tool actually looked like. Right? So we'll see what the tool call was and the final result which is after the tool call got finished. Let's execute both of these queries one by one. So here we start off with the first query. Right? So here the query is what's 23 into 7. So here you will see this is the input from the user. Then the planner actually chose the correct tool which is the calculator tool with the right expression of 23 into 7. So when evaluated the tool responded back with 161 which is actually the right answer and then the assistant then summarized the result here in natural language based upon the result from the tool tool call. The second query was just tell me about something. All right. So it's about lang graph wherein the planner then just did not choose any tool because obviously there is no calculation involved here and the assistant then simply summarized the result. Right? So assistant then told us that langraph is a tool designed to natural language processing that helps us create and and everything else. Right? So this is the final result which is generated and that's how typically tool calls work and how we can integrate tools within an existing workflow within langraph. As an exercise, we have options to add additional tools for you to practice, right? So, you can add more tools that can do vector search, convert temperatures, order lookup, right? So, these are some basic things that you can do to practice it more. That's all for this session. I'll see you in the next video. Thank you very much.

Original Description

In this lesson of our LangGraph Foundations series, we explore how to give your AI agents "superpowers" by integrating External Tools. 🚀 Large Language Models are excellent at text generation, but they need tools to interact with the real world—whether it's performing precise calculations, searching the web, or querying a database. In this video, we move beyond pure text and build a workflow where an agent can decide when to call a function to solve a problem. What you will learn in this video: 1. The Concept of Tools: Why agents need external functions for tasks like math, file reading, and web search. 2. Designing Tool Nodes: How to structure a node with specific inputs (file paths, expressions, SQL) and core logic. 3. The Planner Agent: Building a "brain" node that inspects the state and decides if a tool is needed. 4. Agent-Tool Interaction: How to route the workflow from the Planner to the Tool and back to the Agent for a final natural language response. 5. Hands-on Demo: A step-by-step walkthrough of building a Calculator Tool and integrating it into a LangGraph workflow with conditional logic. Key Highlights: 1. Using the eval logic in Python safely within a node. 2. Implementing a JSON-based Planner that outputs tool requests. 3. Visualizing a workflow that handles both chitchat and functional tool calls. Prerequisites: Basic Python knowledge and an OpenAI API key. Check out our previous videos in the series for context on LangGraph states and nodes! #LangGraph #AIAgents #LangChain #ToolCalling #PythonAI #GenerativeAI #OpenAI #MachineLearning #aitutorial 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 tutorial teaches how to integrate external tools and APIs into AI agents using LangGraph, enabling agents to interact with the real world and perform tasks beyond text generation. By following the steps and using the tools demonstrated, viewers can extend their agent capabilities and design more effective workflows.

Key Takeaways
  1. Define a tool as a function that an agent can use to get real-world data or actions
  2. Design a tool node in a LangGraph workflow
  3. Integrate a tool with LangGraph using a calculator tool as an example
  4. Pass inputs to a tool node
  5. Invoke a tool node to perform a task
  6. Set up environment variables
  7. Load environment variables
  8. Set up OpenI client
  9. Run agent in back end
  10. Update state
💡 Integrating external tools and APIs into AI agents using LangGraph enables agents to interact with the real world and perform tasks beyond text generation, extending their capabilities and improving workflow design.

Related Reads

📰
Beyond console.log: Advanced Debugging Workflows That Will Save You Hours
Improve your debugging workflow with advanced techniques beyond console.log to save time and increase productivity
Medium · JavaScript
📰
cgo Overhead Dropped 30%. When Should You Actually Care?
Learn when to care about the 30% drop in cgo overhead in Go programming and how it affects performance
Medium · Programming
📰
Why Everyone is Wrong About Website Development?
Web development requires a wide range of skills beyond just generating code with tools like ChatGPT
Medium · Programming
📰
The Silent Killer in Your Node.js APIs: Mass Assignment & How to Catch It Before Production
Learn to identify and prevent mass assignment vulnerabilities in Node.js APIs, which can bypass ESLint and SonarQube checks
Medium · Programming
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →