Transcript for:
Building AI Agents with N8N

If you can use catchb, you can build an AI agent. In this video, I'll show you how to build your first agent from scratch on the N8N platform in three relatively simple steps. Step one, give your agent a brain. Step two, give it access to tools like Slack, Google Sheets, and Notion. Step three, teach your agent when and how to use each tool. And that's it. It's that simple. This will be a slightly longer video, but by the end, you go from just using AI to actually building with it. Let's get started. First, a quick refresher. An AI agent needs three things to function properly. One, a brain that's made up of a chat model and memory, which I'll get into later. Two, tools like Slack, Google Sheets, and Notion, so the agent can actually do stuff. And three, a brain stem in the form of a system prompt. I apologize to any doctors watching this video, but if I remember correctly, in human anatomy, the brain stem relays information from the brain to the rest of the body. And the system prompt performs a similar function by helping the agent brain control the tools correctly. Once these three elements are in place, we can give the AI agent some sort of input. For example, by chatting with it directly or uploading a file and after receiving that input, the agent uses the tools to take action before providing some sort of output. Now, let's head on over to the NEM platform. And after signing up, you'll receive a 14-day free trial. To be clear, you can run N locally for free, but some technical setup is required. So, to keep things simple in this tutorial, I'm using N cloud, the paid web version most suited for beginners. And here's the agent we're building today. If you compare this to the diagram I shared earlier, you can see we have the OpenAI chat model and Simple Memory that make up the brain. We have a tool connected Google Sheets. And in order to find the brain stem, we click into the AI agent node and under system message. This is where we have the system prompt that controls the AI agent. So let's find out what this agent does. I'm going to first give it an input by opening chat and typing in natural language. I just subscribed to N for 20 bucks a month. And I press enter. The agent is going to process in real time. But you'll notice that, hey, the Google Sheets tool didn't turn green. What's going on, Jeff? You're terrible at this. And then we realize, oh, just like a human assistant, it's confirming the information before it does anything. It says, it seems like you're trying to add an expense, right? This is a charge date. This is the monthly cadence, and this is a cost. Do you want to confirm? I'm like, yep, looks good. And now the AI agent is going to process again in real time. And this time, we see that the Google Sheets tool has turned green. We're going to open up the corresponding Google sheet. And now we see the output of the AI agent in the form of a new row/ entry, right? Expense, charge date, cadence, cost, ed, and status. At this point, you might be like, "That's cute, but not very practical." You're 100% right. However, learning the basics enabled me to build genuinely useful AI agents that I use every day, like this one. As you can see, I can upload an image or type a message in Slack. The AI agent will extract information from the image or text message, look through my real expenses database in notion, which I can show you just to prove to you I'm not making it up, and then reply with a confirmation message after the changes have been made. None of this would have been possible had I not started with building this basic AI agent. Enough talk. Let's start building. We're going to create a new workflow here. And in this empty page, we're going to click this plus icon. And then we're going to search for chat and we're going to select the chat trigger so we can communicate or talk with the AI agent. Nothing else needs to be done here. So we can press escape or click out to exit back out. Now if I open up the chat by clicking here or down here and I type hello. Although this says the chat node has executed successfully, nothing has happened because this chat trigger is not connected to anything. Next, we can click the plus icon that's connected to the chat trigger and search for AI agent. Select the AI agent. And I know this looks very intimidating, so let's break it down. Think of it like a relay race from left to right. On the left hand side, under the input tab, you'll see what the AI agent receives. In this case, our hello message from the chat trigger. You can even see that by default the AI agent understands it needs to process our message chat input before doing anything else. Now we have two options. We can either connect the chat model memory and tool by clicking here or we can press escape or click out and click the connector here which I'll do now for the chat model. I'm going to search for OpenAI. I'm going to select the OpenAI chat model. And if this is your first time, this is a test account so you should be seeing what I'm seeing. You'll need to create a new credential. And in order to do this, we need an API key from OpenAI. You can click the docs link for step-by-step instructions, but basically you have to go to platform.opai.com, sign in or create an account if you haven't already, head on over to settings, go over to billing, use a payment method to add some credits. Uh, this is not related to your chatbt plus or pro account because API usage, as you can see, is pay as you go. Then head on over to API keys and we'll create a new secret key. I'm just going to name this YouTube v2. I'm going to select the this video project and I'm going to create a new secret key here. I'm just going to copy this. Head on back to our nan um web page. Paste it and click save. And in a few seconds, yes, the connection has tested successfully. And we can click to exit out. When it comes to model selection, I usually go with one of the latest mini models since they strike the best balance between cost and performance. I'm going to use 40 mini here, but it might be 50 mini by next month. Right, but that's it for the chat model. I'm going to click escape to exit out and very critically press command or controls to save our workflow. Now, when we say hello, we will actually receive a response because our AI agent has a chat model as a brain. But look what happens when I say, "Hey, there's a list called the e-list. Keep that in mind." The AI agent tells me, "Got it. Don't worry, I won't forget." So, let's ask, "Hey, what's that list called again?" And somehow, it's sort of like the list has never existed. Thank god something like this would never ever happen in real life. And this is why memory is so important. So let's go ahead and click the memory connector and add a simple memory. The context window length represents how many past interactions or messages the agent remembers. And I like to default to 14. So that assuming I send messages twice a day, by the end of the week on a Sunday, it will still remember what I said on Monday. And now I'm going to press escape, exit the workflow. Press command or controls S to save this workflow. All right, let's now clear the execution and clear the chat log and repeat the exercise. Hey, there's a list called the e-list. Keep that in mind. Enter. And the agent will tell me, cool, it's going to remember the e-list. Awesome. Hey, what's that list called again? And now, thanks to its memory, the AI agent remembers it's called the E-List. Excellent. To close the loop here, let's click back into our AI agent node. And under the input tab on the left, we see the latest output from our chat node. Hey, what's that list called again? The AI agent processed the chat input, as you can see right here. And on the right, we see the latest output. The list you mentioned is called the e-list. Speaking of forgetting important things, I never have to rely on my brain to remember everything thanks to this command center in Notion that basically runs my entire existence. It captures every idea, plans out my day, and keeps track of all aspects of my life. If you like to join over 5,500 students who are already using my exact system, check the link in the description. Now, before clicking the tools connector, we're going to type sheets new in our browser to create a new Google sheet. We're going to rename this Google sheet to subscription tracker. And we're going to add these columns, expense, charge date, cadence, cost, and status. We can bold these if we want. I'm going to rename this tab to tracker. And we're going to just keep this window open. Back in nan, we're going to click the tool connector here. We're going to search for Google Sheets. We're going to select Google Sheets tool. And just like with a chat model, if this is your first time, we're going to need to create a new credential that connects our Google account with nan. The process is a lot simpler for Google Sheets. We can just click sign into Google. Choose our Google account over here. And we're going to select all to give access. And we're going to select continue. And you'll see that the connection is successful. For the tool description, keep it as set automatically. We will teach the AI how to use this tool later with our system prompt. So we don't need separate instructions here. Now let's configure what this tool actually does. For resource, we keep this as is sheet within document. For operation, we're going to change this to append row since we need to add new entries, add new rows to our Google sheet, right? And for document, we can choose by URL. We're going to head back to our Google sheet, copy the URL here, and paste the URL in this field. Pro tip, we can click the expand icon here to double check we're in the right file. Under the sheet field from list, we're going to select the tracker tab. And although we want to map the columns manually to ensure accuracy, we want the values of those columns to be determined by AI. So, here's the fun part. We can click the sparkle icon for each one of these column values. And we see that now the inputs are all defined automatically by the AI model. Last but not least, we're going to click the title of this tool and rename this tool to add entry. Enter. And press escape to exit back out. Command or control S to save the workflow. Now let's go back to our initial diagram. We have a brain chat model and memory. We have access to a tool Google Sheets. And all we need to do now is to activate the brain stem via a system prompt so that the brain can send instructions to the tool. At this point, it should be clear why we left this step until last. Right? Without knowing the name of the tool, we wouldn't have been able to instruct the brain to use the add entry tool. Writing the system prompt is actually the easiest part of this tutorial. First, we're going to take a screenshot of this workflow like so. Then we're going to download this entire workflow as a JSON file by clicking the three dots here, download. Then we're going to head on over to chatbt. Select the latest reasoning model. We're going to paste the screenshot we just took. We're going to upload the JSON file. And then we're going to input this prompt. Your task is to output a clear, precise, and comprehensive system message for my AI agent designed to keep track of my subscriptions. The AI agent is connected to a Google sheet and has access to the ad entry tool. I'm not going to read the whole thing. I'll link this in the video description below. After thinking for a bit, it's going to spit out an entire system prompt for us. So, we're going to copy this entire thing like so. Head back into N. Click into the AI agent. Scroll down to here under options. And I'm going to add option system message. I'm going to click expression so I can expand this to full screen. I'm going to replace the default message with what ChachiPT gave us. And just for the sake of this tutorial, I'm going to exit out and run this as is without making any adjustments to the system prompt. Now, to test our AI agent, I cleared the previous execution and chat logs. And I'm just going to type I just subscribed to iCloud for $10 a month. Enter. And let's see what happens. I don't know if this is going to work, and you'll see why in a little bit. All right. So this first execution failed and succeeded at the same time. Why? Because it added the entry without confirming with me. And if I open the spreadsheet, the date is completely wrong. We're now in 2023. So clearly something is wrong with the system prompt and we need to fix it. Two ways we can troubleshoot this. One, we could take a screenshot, upload the screenshot in the chatbt and ask to troubleshoot. Or this is way more fun. We can click into the AI agent, expand the system prompt, find the part of instructions that control the conversation flow part between the AI agent and the user, right? And instead of saying, oh, if anything is missing or ambiguous, ask exactly one concise follow-up question, I can literally say always follow up with a confirmation message even if the information is complete. So that's number one, right? Number two, we need to fix the date. So if I look for the date part, you can either press command or control F to look for the date part or I just see right here charge date, ISO format, year, month, date. But this is hard-coded. We want a dynamic variable, right? Whenever the date of the message, the date in which the message was sent, we want that to be the charge date. So what can we do? On the left hand side, we see that hey, we have a chat input variable here. I just subscribed to iCloud. I wonder if there's a time stamp. We can actually click open the variables and context and we see there is a timestamp right here. I wonder if I could oh I can I can drag this dynamic date variable right here so that the AI agent understands the date should always depend on when the message was sent. I'm going to now exit out and press command and control S to save this workflow. Now let's clear the execution and reset the chat logs and try again. I just subscribed to iCloud for $20 US a month. We're going to press enter. And this time, yes, it's confirming with us. iCloud, $20 monthly starting today. Add the tracker. Please confirm. See, as you can see, the tool did not run, right? Yep. Go for it. This time, the tool should run and the entry should be added. Yep, there we go. To our spreadsheet. And we open up the spreadsheet. Boom. iCloud today's date monthly cadence cost active. At this point, the AI agent has been created. But let's go through a quick thought experiment. If you had a human assistant helping you maintain this subscription tracker spreadsheet, you probably want them to check for existing entries to avoid duplications, right? If there were an existing iCloud entry, you probably want them to tell you that. So, how might we edit this workflow to reflect that? Step one, we'd want to add another tool, Google sheet. And for this tool, we want the tool to read the existing rows first. So the operation here should be to get row so that the AI agent looks through all the rows first. I won't implement these steps because the video is long enough, but the important part is the logic. Step two, going back into the add entry tool. Instead of the operation being append or add a new row, we want it to be append or update row, right? So when iCloud goes from $10 or $20 to like $50, $100, I wouldn't put it past Apple. The AI agent could update the values in that row. Step three, the most important step, we'd have to go back into the AI agent node and update the system message to reflect that updated workflow. Hey, before making any changes or adding any rows, go through the existing entries first. I highly recommend you try making those changes yourself. And if you get stuck, take a screenshot, download the JSON file, upload those into Chat GPT and ask Chat GPT to help you troubleshoot. Final thought I want to leave you with. I know it's tempting to rely on pre-built AI tools like ChatG agents, but that's like trusting a black box with your most critical tasks. It might be hallucinating. It might be leaking private data. And you never know. So, understanding how AI agents work, learning the building blocks is like understanding basic math. It's essential even if you're not a mathematician. Besides, this is pretty fun, right? Just me? Okay. Anyways, if you enjoyed this, you might want to check out my tutorial on Google Gemini next. See you on the next video. In the meantime, have a great one.