LangChain Agents in 2025 | Full Tutorial for v0.3

James Briggs · Beginner ·🧠 Large Language Models ·1y ago

Key Takeaways

The video introduces LangChain Agents, a crucial component in AI that can perform tasks beyond normal language models using tools such as search and calculators, and demonstrates how to use them with OpenAI's gpt-4o-mini and LangChain's v0.3.

Full Transcript

In this chapter, we are going to introduce agents. Now, agents, I think, are one of the most important components in the world of AI and I don't see that going away anytime soon. I think the majority of AI applications, the intelligent part of those will be almost always an implementation of an AI agent or multiple AI agents. So in this chapter we are just going to introduce agents within the context of lang chain. We're going to keep it relatively simple. We're going to go into much more depth in agents in the next chapter where we'll do a bit of a deep dive but we'll focus on just introducing the core concepts and of course agents within line chain here. So, jumping straight into our notebook, let's run our prerequisites. You'll see that we do have an additional prerequisite here, which is Google search results. That's because we're going to be using the SER API to allow our LM as an agent to search the web, which is one of the great things about agents is that they can do all of these additional things and LM by itself obviously cannot. So we'll come down to here. We have our linesmith parameters again of course. So you enter your line chain API key if you have one. And now we're going to take a look at tools which is a very essential part of agents. So tools are a way for us to augment our LMS with essentially anything that we can write in code. So we mentioned that that we're going to have a Google search tool. That Google search tool is some code that gets executed by our LLM in order to search Google and get some results. So a tool can be thought of as any code logic or any function in the ca in the case of Python any function that has been formatted in a way so that our LM can understand how to use it and then actually use it. Although the the LM itself is not using the tool. is more our agent execution logic which uses the tool for the LM. So we're going to go ahead and actually create a few simple tools. We're going to be using what is called the tool decorator from Langchain. And there are a few things to keep in mind when we're building tools. So for optimal performance, our tool needs to be just very readable. And what I mean by readable is we need three main things. One is a dot string that is written in natural language and it is going to be used to explain to the LLM when and why and how it should use this tool. We should also have clear parameter names. Those parameter names should tell the LM okay what each one of these parameters are. They should be self-explanatory. If they are not self-explanatory, we should be including an explanation for those parameters within the dock string. Then finally, we should have type annotations for both our parameters and also what we're returning from the tool. So let's jump in and see how we would implement all of that. So we come down to here and we have lang chain core tools import tool. Okay, so these are just four incredibly simple tools. We have the addition or add tool, multiply, the exponentiate and the subtract tools. Okay, so a few calculatores tools. Now when we add this tool decorator, it is turning each of these tools into what we call a structured tool object. So we can see that here. We can see we have this structured tool. We have a name description. Okay. And then we have this old schema. We'll see this in a moment. And a function, right? So this function is literally just the original function. It's it's a mapping to the original function. So in this case, it it's the add function. Now the description we can see is coming from our dock string. And of course the name as well is just coming from the function name. Okay. And then we can also see let's just print the name and description. But then we can also see the AGS schema, right? We can so this thing here that we can't read at the moment. To read it, we're just going to look at the model JSON schema method and then we can see what that contains which is all of this information. So this actually contains everything includes properties. So we have the X it title for that and it also specifies the type. Okay. So the type that we defined is float. Float for OpenAI gets mapped to number rather than just being float. And then we also see that we have this required field. So this is telling our LM which parameters are required, which ones are optional. So we yeah in some cases you would we can even do that here. Let's do Z that is going to be float or none. Okay. And we're just going to say it is 0.3. Right? I'm going to remove this in a minute because it's kind of weird. But let's just see what that looks like. So you see that we now have X, Y, and Z. But then in Z, we have some additional information. Okay. So it can be any of it can be a number or it can just be nothing. The default value for that is 0.3. Okay. And then if we look here, we can see that the required field does not include Z. So it's just X and Y. So it's describing the full function schema for us. But let's remove that. Okay. And we can see that again with our exponentiate tool. Similar thing. Okay. So how how are we going to invoke our tool? So the LM the underlying LM is actually going to generate a string. Okay. So will look something like this. This is going to be our LM output. So it is it's a string that is some JSON. And of course to load a string into a dictionary format, we just use JSON loses. Okay, so let's see that. So this could be the alpha from LLM. We load it into a dictionary and then we get an actual dictionary. And then what we would do is we can take our exponentiate uh tool. We access the underlying function and then we pass it the keyword arguments from our dictionary here. Okay. And that will execute our tool. That is the tool execution logic line chain implements. And then later on in the next chapter we'll be implementing ourselves. Cool. So let's move on to creating an agent. Now we're going to be constructing a simple tool calling agent. We're going to be using linechain expression language to do this. Now we will be covering line chain expression language or L cell more in a upcoming chapter. But for now all we need to know is that our agent will be constructed using syntax and components that look like this. So we would start with our input parameters that is going to include our user query and of course the chat history because we need our agent to be conversational and remember previous interactions within the conversation. These input parameters will also include a placeholder for what we call the agent scratchpad. Now the agent stretch pad is essentially where we are storing the internal thoughts or the internal dialogue of the agent as it is using tools getting observations from those tools and working through those multiple internal steps. So in the case that we will see it will be using for example the addition tool getting the result using the multiply tool getting the result and then providing a final answer to us as a user. So let's jump in and see what that looks like. Okay. So, we'll just start with defining our prompt. So, our prompt is going to include the system message. That's nothing. We're not putting anything special in there. We're going to include the chat history, which is a messages placeholder. Then, we include our human message. And then we include a placeholder for the agent scratch pad. Now, the way that we implement this later is going to be slightly different. For the scratch pod, we would actually use this messages placeholder, but this is how we use it with the built-in create tool agent from Lang Train. Next, we'll define our LM. We do need our opening our API key for that. So, we'll enter that here like so. Okay, so come down. Okay, so we're going to be creating this agent. We need conversational memory and we are going to use the older conversation buffer memory class rather than the newer runnable with message history class. That's just because we're also using this older create tool calling agent and this is this is the older way of doing things. In the next chapter, we are going to be using the more recent basically what we already learned on chat history. We're going to be using all of that to implement our chat history. But for now, we're going to be using the older method uh which is deprecated just as a pre-warning. But again as I mentioned at the very start of the course we're starting abstract and then we're getting into the details. So we're going to initialize our agent for that we need these four things lm as we defined tools as we have defined prompt as we have defined and then the memory which is our old conversation buffer memory. So with all of that we are going to go ahead and we create a tool calling agent and then we just provide it with everything. Okay, there we go. Now, uh you'll see here I didn't pass in the the memory. I'm passing it in down here instead. So, we're going to start with this question, which is what is 10.7 multiplied by 7.68. Okay. So, given the precision of these numbers, our L normal LM would not be able to answer that or almost definitely would not be able to answer that correctly. we need a external tool to answer that accurately and we'll see that that is exactly what it's going to do. So we can see that the tool agent action message here we can see that it decided okay I'm going to use the multiply tool and here are the parameters that I want to use for that tool. Okay, we can see X is 10.7 and Y is 7.68. You can see here that this is already a dictionary and that is because lang chain has taken the string from our LM call and already converted it into a dictionary for us. Okay, so that's just it's happening behind the scenes there. And you can actually see if we go into the details a little bit, we can see that we have these arguments and this is the original string that was coming from LLM. Okay, which has already been of of course processed by lang chain. So we have that. Now, the one thing missing here is that, okay, we've got that the LM wants us to use multiply and we've got what the LM wants us to put into multiply, but where's the answer, right? There is no answer because the tool itself has not been executed because it can't be executed by the LM. But then, okay, didn't we already define our agent here? Yes, we define the part of our agent that is our LM has our tools and it is going to generate which tool to use but it actually doesn't include the agent execution part which is okay the agent executor is a broader thing. It's it's broader logic like just code logic which acts as a scaffolding within which we have the iteration through multiple steps of our LLM calls followed by the LLM outputting what tool to use followed by us actually executing that for the LLM and then providing the output back into the LM for another decision or another step. So the agent itself here is not the full agentic flow that we might expect. Instead for that we need to implement this agent executor class. This agent executor includes our agent from before. Then it also includes the tools. And one thing here is okay we we already passed the tools to our agent. Why do we need to pass them again? Well the tools being passed to our agent up here that is being used. So that is essentially extracting out those function schemers and passing it to our L so our LM knows how to use the tools. Then we're down here we're passing the tools again to our agent executor. And this is rather than looking at how to use those tools. This is just looking at okay I want the functions for those tools so that I can actually execute them for the LM or for the agent. Okay. So that's what is happening there. Now we can also pass in our memory directly. So you see if we scroll up a little bit here, I actually had to pass in the memory like this with our agent. That's just because we weren't using the agent executor. Now we have the agent executor. It's going to handle that for us. And another thing that it's going to handle for us is intermediate steps. So you'll see in a moment that when we invoke the agent executor, we don't include the intermediate steps. And that's because it that is already handled by the agent executor now. So we'll come down. We'll set verbose equal to true. So we can see what is happening. And then we can see here there's no intermediate steps anymore. And we we do still pass in the chat history like this. But then the addition of those new interactions to our memory is going to be handled by the executor. So fact let me actually show that very quickly before we jump in. Okay. So that's currently empty. We're going to execute this. Okay, we're entered that new agent execute chain. And let's just have a quick look at our messages again. And now you can see that agent executor automatically handled the addition of our human message and then the responding AI message for us. Okay, which is useful. Now what happened? So we can see that the multiply tool was invoked with these parameters and then this pink text here that we got that is the observation from the tool. So this is what the tool output back to us. Okay. Then this final message here is not formatted very nicely but this final message here is coming from our LM. So the green is our LLM output. The pink is our tool output. Okay. So the LM after seeing this output says 10.7 multiplied by 7.68 is approximately 82.18. Okay, cool. Useful. And then we can also see that the chat history which we we already just saw. Great. So that has been used correctly. We can just also confirm that that is correct. Okay. 82.1759 recurring which is exactly what we get here. Okay. And we the reason for that is obviously our multiply tool is just doing this exact operation. Cool. So let's try this with a bit of memory. So I'm going to ask or I'm going to say to the agent, hello, my name is James. We'll leave that as the it's not actually the first interaction because we already have these, but it's an early interaction with my name in there. Then we're going to try and perform multiple tool calls within a single execution loop. And what you'll see with when it is calling these tools is that it can actually use multiple tools in parallel. So for sure, I think two or three of these were used in parallel and then define or subtract had to wait for those previous results. So it would have been executed afterwards and we should actually be able to see this in lang. So if we go here yeah we can see that we have this initial call and then we have add and multiply and exponentiate all use in parallel. Then we have another call which you subtract and then we get the response. Okay, which is pretty cool. And then the final result there is1. Now when you look at whether the answer is accurate, I think the order here of calculations is not quite correct. So if we put the actual computation here, it gets it right. But otherwise, if I use an actual language, it's like I'm doing maybe I'm phrasing it in a in a poor way. Okay. So I suppose that is pretty important. So okay, if we put the computation in here, we get the -3. So it's something to be careful with and probably requires a little bit of prompting to prompting and maybe examples in order to get that smooth so that it does do things in the way that we might expect or maybe we as humans are just bad and misuse the systems. One or the other. Okay, so now we've gone through that a few times. Let's go and see if our agent can still recall our name. Okay, and it remembers my name is James. Good. So, it still has that memory in there as well. That's good. Let's move on to another quick example where we're just going to use Google search. So, we're going to be using the SER API. You can Okay, you can get the API key that you need from here. So, serapi.com/ users/sign and just enter that in here. So, you will get it's up to 100 searches per month for free. So, just be aware of that if you overuse it. I don't think they charge you because I I don't think you enter your card details straight away, but yeah, just be aware of that limit. Now, there are certain tools that Lang Chain have already built for us. So, they're pre-built tools and we can just load them using the load tools function. So, we do that like so. We have our load tools and we just pass in the set API tool only. We could pass in more there if we wanted to. And then we also pass in our lm. Now I'm going to one use that tool, but I'm also going to define my own tool which is to get the current location based on the IP address. Now this is we're in collab at the moment. So it's actually going to get the IP address for the collab instance that I'm currently on and we'll find out where that is. So that is going to get the IP address and then it's going to provide the data back to our LM in this format here. So we're going to latitude, longitude, city, and country. Okay, we're also going to get the current day and time. So now we're going to redefine our prompt. I'm not going to include chat history here. I just want this to be like a oneshot thing. I'm going to redefine our agent and agent executor using our new tools which just our sub API plus the get current date time and get location from IP. Then I'm going to invoke our agent executor with I have a few questions. What is the date and time right now? How is the weather where I am? And please give me degrees in Celsius. So when it gives me that weather. Okay. And let's see what we get. Okay. So apparently we're in Council Bluffs in the US. It is 13° Fahrenheit, which I think is absolutely freezing. Oh my gosh, it is. Yes. - 10. So it's super cold over there. And you can see that, okay, it did give us Fahrenheit. And that's that is because the tool that we were using provided us with Fahrenheit, which is fine, but it did translate that over into a estimate of Celsius for us, which is pretty cool. So, let's actually output that. So, we get this, which I is correct. We do US approximately this. And we also get an description of the conditions as well. It's partly cloudy with 0% precipitation, lucky for them, and humidity of 66%. Okay, all pretty cool. So, that is it for this introduction to Langchain agents. As I mentioned, next chapter, we're going to dive much deeper into agents and also implement that for chain version 0.3. So, we'll leave this chapter here and jump into the next one.

Original Description

In this chapter, we will introduce LangChain's Agents, adding the ability to use tools such as search and calculators to complete tasks that normal LLMs cannot fulfil. We will be using OpenAI's gpt-4o-mini. 🔗 Full Course: https://www.aurelio.ai/course/langchain 📌 Article and code: https://www.aurelio.ai/learn/langchain-agents-intro 💡 Subscribe for Latest Courses and Tutorials: https://www.aurelio.ai/subscribe 👾 Discord: https://discord.gg/c5QtDB9RAP Twitter: https://twitter.com/jamescalam LinkedIn: https://www.linkedin.com/in/jamescalam/ #ai #coding #aiagents #langchain 00:00 LangChain Agents 101 01:27 Introduction to Tools 07:05 Creating an Agent 11:27 Agent Executor 18:02 Web Search Agent
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from James Briggs · James Briggs · 0 of 60

← Previous Next →
1 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

This video introduces LangChain Agents and demonstrates how to use them with OpenAI's gpt-4o-mini and LangChain's v0.3 to perform tasks beyond normal language models. The video covers the basics of agents, tools, and agent execution logic, and provides a practical guide to building and using LangChain Agents.

Key Takeaways
  1. Run prerequisites
  2. Create simple tools using the tool decorator
  3. Implement tools with a clear description, parameter names, and type annotations
  4. Invoke tool
  5. Load string into dictionary
  6. Execute tool
  7. Construct agent
  8. Define prompt
  9. Pass tools to agent executor class
  10. Execute tool using multiply tool
💡 LangChain Agents can be used to perform tasks beyond normal language models by leveraging tools such as search and calculators, and can be implemented using LangChain's v0.3 and OpenAI's gpt-4o-mini.

Related Reads

📰
GPT-5.6 Just Became Microsoft 365’s “Preferred Model.” Here’s What That Word Is Doing.
Microsoft 365 Copilot now uses GPT-5.6 as its preferred model, enhancing productivity across Word, Excel, PowerPoint, and more
Medium · AI
📰
How College Students Can Use ChatGPT Deep Research for Better Assignments in 2026
Use ChatGPT for deep research to improve college assignments in 2026
Medium · Deep Learning
📰
How College Students Can Use ChatGPT Deep Research for Better Assignments in 2026
Learn how to leverage ChatGPT for deep research to improve college assignments
Medium · ChatGPT
📰
Kimi K3 Explained: The Giant AI Model That Can Build Compilers, Design Chips, and Work for Days
Learn about Kimi K3, a giant AI model that can build compilers, design chips, and work for days, and its potential impact on the tech industry
Medium · AI

Chapters (5)

LangChain Agents 101
1:27 Introduction to Tools
7:05 Creating an Agent
11:27 Agent Executor
18:02 Web Search Agent
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →