n8n AI Agent Tutorial | Building Multi Agent Workflows β
n8nRecentπ
2025-02-23
Build Process β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BUILD CHATBOT WORKFLOW β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β PLANNING β
β ββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Define βββββΊβ Choose βββββΊβ Setup β β
β β Scope β β Stack β β Project β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEVELOPMENT β
β βββββββββββ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Build βββββΊβ Add AI βββββΊβ Test & β β
β β Features β β Logic β β Debug β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β DEPLOYMENT β
β ββββββββββ β
β βββββββββββββββ βββββββββββββββ β
β β Deploy βββββΊβ Chatbot β βββ LIVE! β
β β to Cloud β β Running β β
β βββββββββββββββ βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββKey Components β
- Project architecture
- Core features implementation
- Best practices
Transcript β
[00:00] Welcome to this beginner course on building AI agents with N8N. In this tutorial, I will assume that you're new to N8N or perhaps this is your very first AI agent. So we will go through the entire process step by step. From setting up N8N to building our very first workflow and building a supervisor agent that has access to tools like web search and it will also be able to delegate tasks between different worker agents as well. And our agent will
[00:30] also have access to a custom knowledge base which we can maintain. And each of these worker agents will be able to execute multiple actions. Like the email agent will be able to send emails, read emails and even reply to emails. The calendar agent will be able to create new calendar events and retrieve our current calendar entries. Our contact agent will retrieve contact details like email addresses from Google contacts. It will also be able to create
[01:00] new contacts and update existing contacts. Then a content creator agent will be responsible for writing things like research papers, blog posts, social media posts, etc. If you are familiar with using N8N and building AI agents, you will still find value in this video as well as we will look at best practices for implementing these agents. For this tutorial, I do recommend following along in your own N8N instance. You will also find time
[01:30] codes to the different sections in the description of this video. And in the description, you will also find a link where you can download all of these workflows absolutely free. So what exactly is N8N? Well simply put, it's a workflow automation tool. It's similar to platforms like Make and Zapier, but with more often emphasis on developers. Now if you're not a developer, this is still the perfect tool for you. It simply means that it is very simple for developers to add custom functionality to these workflows.
[02:03] But if you're a non-developer, this is a super simple tool to use. N8N is also open source, which means you can download and sell photos to platform if you want. Or if you don't want to deal with the infrastructure yourself, you can definitely sign up for the paid service as well. And in this video, we will have a look at running it locally on our own machines. And I'll also show you how to sign up for the cloud service. Let's also briefly discuss workflows and the different notes you will see throughout this video. Workflows like this might seem
[02:34] overwhelming, but it all breaks down to three different types of nodes. These include trigger nodes, processing nodes, and finally action nodes. Now let's have a look at these nodes in a bit more detail. Triggers are, well, anything that triggers the workflow. These could be things like forms, file system changes. For example, a file might be created on the file system, or our workflows could also run on a schedule. Our workflows could also be exposed as webhooks or API endpoints, or
[03:07] changes in external applications could trigger these workflows as well. As an example, a database change, or if we receive an email. In terms of building AI agents, our trigger could be a simple chat message. Processing nodes can be used to work with the data within the workflow. These could include things like sort, filter, if then conditions, transforming data, enriching data, AI agents, summarizing text, transcribing audio, and much,
[03:38] much more. And finally, we have actions. Actions are typically things that have an effect on external systems. So actions could be something like sending an email, posting on social media, creating and saving files, creating tickets, making API calls, updating databases, retrieving data from calendars, generating AI images, voice, or video. Let's have a look at the super simple workflow example. We could have a trigger like when we receive an
[04:09] email, this workflow should be triggered. Then using the contents of that email, we might want to then process the contents of that email. For example, we could summarize the text or we could extract certain information, perhaps lead information from that email. We could then perform an action like taking that lead information and installing it in an external system, like maybe a CRM system or a database. And we could have another action that replies to that initial email. Let's
[04:40] have a look at another example that includes an AI agent. We could trigger the workflow when we receive a chat message. That chat message will then be passed to our AI agent. The agent has access to a few actions. It could respond to our chat message, but it could also decide to call tools. And each of these tools are actions as well. For example, it could send an email or it could perform a Google search. So fundamentally, this is how N8n works. Just keep in mind, you would
[05:11] always have triggers, you would then process the data and perform some action. Now enough about the theory, let's now jump into N8n. You can access N8n by clicking on the card on the screen right now or using the link in the description of this video. That link will tell N8n that you sent me and that will also greatly help my channel. As I mentioned earlier, N8n is open source and you can self-host it if you want. That means you can install N8n on your local machine for
[05:42] absolutely free. And that is what we'll use in this video. You could deploy it yourself to a VPN, which is usually a very affordable option. But if you don't want to deal with infrastructure and you don't want to run it locally, you can definitely go for their paid cloud service. You can see the pricing on their pricing page over a year. So if you simply wanted to follow along, you could simply click on get started and then sign up for this 14-day trial period. And that will take you to the N8n dashboard. For everyone
[06:12] else who wants to run it locally for free, I'm going to show you that process right now. The only prerequisite is that you need to have node.js installed. So go to node.js.org and download and install node.js for your operating system. Then open up your command prompt or terminal and enter npx n8n start. If it's your first time running N8n, you will be asked whether you want to install N8n. So simply press Y and enter. After installation
[06:42] completes, you will get this URL, which you can copy. And when you open it in the browser, you will be asked to create an account. And afterwards you will see this dashboard. So whether you're running N8n locally or in the cloud, you're now ready to move on to the next step. From the dashboard, we can view our workflows, any credentials that we've set up, as well as past executions. On the left, we can also access the N8n marketplace by clicking on templates. And from here, you can view
[07:14] and download thousands of pre-created templates for pretty much any use case. Scrolling down to the bottom, we can also find our settings. And if you're anything like me, you'd want to go to personal, scroll down to the bottom and change your theme to dark mode and save. Awesome. Now let's go back to our dashboard. And I'm guessing you're eager to build your very first AI agent. So let's get into this. Let's click on start from scratch or create workflow. We can rename
[07:46] a workflow by clicking on the name on the top left corner. Let's call this my first agent. Looking at the canvas, we can zoom out and in, and we can also click on this button to center all the notes in the center of the screen. You can move the canvas around by holding space bar and dragging the left mouse button. On the trackpad, you do have to press down two fingers and then move it left or right, up or down. On the top menu, we can switch between editor and past
[08:18] executions. We haven't executed this flow yet, so we don't see any past executions yet. Let's go back to our editor. We can also change our flow from inactive to active, which will put it into production mode. More on that later. If you are on the paid plan, you can share your workflow with team members. We can also access this workflow settings by clicking on these three dots. And from here, we can duplicate this workflow, download it, or import an existing workflow. What I do
[08:49] recommend is clicking on settings and from here, set your time zone. I'll set mine to my local time zone in Johannesburg. Let's save these settings. And now we can add our very first trigger node. When we click on add first step, we can see all the different triggers that are available. We can trigger a workflow manually, which is ideal for testing. Or on AppEvent, we can see all these integrations with third-party platforms. For example, we could search for something like Gmail,
[09:20] and within this Gmail node, we have this on message received trigger. This means that when we receive an email, that will trigger this workflow. We can also run workflows on a schedule, which means we could set the intervals to days, minutes, hours, seconds, weeks, etc. So we could decide to run this workflow on a regular schedule. We can also expose this workflow as an API endpoint using the webhook call. We can also create a custom form where we can specify
[09:51] individual fields. And when a user submits that form, it will trigger this workflow as well. Workflows can also be triggered by other workflows, which we will do in this tutorial. And of course, we can also trigger our workflow when we receive a chat message. And since we are focusing on AI agents in this tutorial, I'll select this on chat message node. This node doesn't give us too many options. We can basically decide to make it public, which will give us this URL, which we can use to access
[10:21] this chat flow using a chat interface from outside of N8N. I'm simply going to disable this toggle for now. Let's go back to the canvas. And now we get this open chat button. After clicking this button, we now have this chat interface. They send a message like, "Hello." We get this pop up saying this workflow executed successfully. And we can see our chat message all the way on the right over here. But we didn't receive any response. That is because we haven't attached an AI agent
[10:52] yet. Now we can decide to add processing or action nodes to this workflow by clicking on this add button. Now we have a whole bunch of different options. Under data transformation, we have values like edit field, which allows us to manipulate values. We can filter on data. We can limit the amount of entries. We can split the entries out or aggregate different results, et cetera. Also under flow, we can do things like if statements, filter, merge.
[11:23] We can loop over items and more. What we're interested in is this advanced AI menu. And within here, we have access to all sorts of AI related nodes. Let's add the AI agent node. Whenever we add nodes, whether these are processing or action nodes, we can see the input from the node before it, as well as the output generated by this node on the right hand side. This means we can see the input from the chat interface on the left over
[11:54] here. And we can change this view from a schema view, a table view, and a JSON view. Let's go back to schema. We can see the chat window provided a session ID, which is a unique identifier for this conversation. We also have an action called send message, and of course our chat message. Within this agent node, we can set all sorts of parameters. We can select the type of agent from this dropdown, and here we have access to tool agents, conversational agents, and many others.
[12:25] For 99.9% of all use cases, the tool agent is sufficient. For the source prompt node, the default behavior is to try and grab the user's message from the chat trigger node. Therefore, we don't have to manually specify the prompt. Of course, if we wanted to, we could change this to define below, and now we can simply type a message like hello or how are you doing? Now, obviously, you don't want to hard code those values. We want to grab them from the chat window or from another
[12:57] node in the flow. So what we could do is grab this chat input property on the left and drag and drop it into the stix field. And now we are passing the value from the chat window in to your AI agent. But as you can see, the value is coming from JSON.chat input, which is identical to this very first option, which also looks for JSON.chat input. So let's leave it on this option for now. Under options, we can click on add option. And let's add a
[13:27] system message. It's a common practice with AI agents to set a system message. Here we can define the role, the rules, and any other information that should affect the agent's behavior. To make this window a bit larger, let's switch over to expression and let's click on this button to expand this view. Let's change this. I'm going to remove this default text and I'm going to add this hashtag, which in Markdown is simply a heading with the text role. Here we can give our agent
[13:58] the name and set its behavior. Let's say you are an AI agent called Max. We could also set additional stuff like additional information. And in here, let's add something like you are talking to Leon. This will tell our agent who they're talking to. What I also like to do is to tell our agent what the current date and time is. So if we ask you the question like what is the current date and time, it will have that answer. So we can simply do that by saying the
[14:28] current date and time is, and now we have to dynamically populate the date and time. So how do we do that? We get this little tip saying we can use these double curly braces to inject some JavaScript. But don't worry, we won't be writing any JavaScript during this tutorial. This is the only time where we will inject a dynamic value. So simply enter double curly braces, then dollar sign, and here we get a list of suggested values. Let's select the now value. On the right hand side, we
[15:00] see a preview of what that value would look like. And this is injecting a JavaScript object into this text. We can make this look slightly better by adding dot and to string. So I'll just select this second option. And now we're injecting the date and the time and the time zone into this prompt. We will come back to the system prompt a few times during this tutorial. But for now, this is good enough. Let's close this pop up. And let's go back to the canvas. Now our AI agent has this red
[15:31] outline. That is because we're missing certain inputs. And we can see this chat model is a required input. The chat model is the large language model, which is effectively the brain behind this agent is click on Add. And here we can see a list of all the large language providers offered by in it in now I am going to mention that almost all of these nodes are paid services. And we will be using open AI during the course of this video. And there's a very good reason for that. AI
[16:01] agents require powerful models in order to work effectively and trying to run a powerful model on their own machine using a llama is not practical. If you want a free service, then I do recommend using Grog. But this does offer certain shortcomings as well as the models offered by Grog are not as powerful as the models we need to get this agent to work effectively. But if you really want to follow along for free and with a caveat that you might run into frustrating issues with the agent is not
[16:33] doing what you're expecting it to do. Afterwards, we will set up open AI instead. So let's add the Grog node. And now we need to create our credentials. So on the credentials, click on Create New Credential. And now we have to enter an API key, go to grok.com. And under developers, click on Free API key, then click on Create API key, and give it a name like n8n masterclass, copy the key and add it to n8n.
[17:05] Click on Save. And if everything was set up correctly, you should get this green message. It's close to pop up. And now we can select from the different models at the time of recording, the best open source model to use for agents would be llama 3.3 and the 70 billion parameter model. So simply select that model, go back to the canvas. And now this error will be resolved. We can chat to this model by clicking on open chat. And let's say, hello. So this will run the agent. And afterwards, we do get our response
[17:37] back. Now I will not be using Grog for this tutorial, as there are limitations down the line. So I'm going to delete this node, then on the chat model, I'm going to add open AI, then on the credentials, let's create a new credential. And now we have to provide an API key to get the key, go to platform.openai.com slash API keys, you will find a link in the description of this video to create a key, they go on create new secret key, give it a name
[18:07] like n8n masterclass. And let's create this key is copy the key and add it to n8n. We don't have to change any of these other fields, they simply save the connection was successful. So we can close this pop up. And now we can select our AI model. I'm going to select GPT 40 mini. This is a super affordable and intelligent model. I do want to remind you that open AI is a paid service, which means you do have to load some credit onto your account. And trust me, a few dollars goes a
[18:39] long way on this platform to load credit, click on your profile, click on billing, and then click on add to credit balance. And here you can add as little as $5. And trust me, that's going to last you a long time. And in my opinion, it's absolutely worth it. As I personally hate dealing with the frustration of working with models that don't do what I expect him to do. So back in n8n, let's go back to the canvas. And let's see if this is working. Let's open
[19:10] chat. And let's start a new conversation by clicking on this refresh button. Yeah, we'll just reset this conversation. And let's say hi there. And we get our response back. This chat window also provides other valuable information as well. On the right hand side, we can see that the agent that was called and within this agent node, the open AI chat model node was called. And we can see the response took about 900 milliseconds, along with the date and time that this was run, and
[19:40] the amount of tokens that were used. We can also see the full input. And this includes our system message along with the human message that we just passed over here. And we also see the agent's output. Our agent is working, but it is lacking some basic functionality. First, let me show you this silly issue. Let's say, my dog's name is Ruby. Our agent says, oh, what a lovely name. How old is Ruby? And what breed is she? But watch what happens when I ask it, what is my dog's name?
[20:12] And now it's saying it doesn't have access to that information. So our agent is clearly suffering from some kind of memory loss. Now, why is this? Although we can see the conversation history in this chat window, each message that we send to the agent is technically a separate invocation. So the agent has no view of the previous conversations. And we can see that on the right hand side as well, the input simply contains a system message. And then our latest message is got no view of the
[20:42] conversation history. So how can we add that history or the previous messages in our chat, we can do that by adding memory. So let's click on memory. And let's select this window buffer memory node. This node will be used to store our previous messages. And here we can also specify the amount of previous messages that should be stored and retrieved. Let's say we want to pull in the 20 last messages. Let's go back to the canvas and watch what happens when we chat to this agent. Now I'm going to reset this
[21:12] chat. And let's say my dog's name is Ruby. And in the chat window, we can already see a different behavior. First, our agent reaches out to the buffer memory node to retrieve our conversation history, it then sends our message to open AI. And finally, it stores the new messages in our memory database. Let's ask it now, what is my dog's name? And this time our agent did remember my dog's name. And on the right hand side, if we go to our chat model,
[21:42] we can see the system message, then we can see the conversation history. And finally, we can see the new message that we sent. Our agent is coming along nicely, but we can greatly improve it even still. AI agents have the capability to call tools. Tools allow it to interact with the environment around it. Let me show you an example of this. Let's ask it, what is the latest news on grok3? And our agent is telling us that its last update was October
[22:13] 2023. So it actually doesn't know what grok3 is. This simply means the cutoff date for the training data of the specific model was October 2023. So our agent doesn't have access to real time data. How can we fix that? Well, thankfully, that's easy. We can assign a tool within tools, we can call other workflows, we can access vector stores, and under other tools, there's plenty of different integrations. For example, on this screen, I can already see a calculator.
[22:45] And since these models are generally very bad at doing math, I'll actually add the calculator tool in the meantime, like so, right. But we also want to give our agent the ability to go online and perform a web search. That way it can retrieve up to date information. The tool that we can use for that is called Serp API, the Google search tool. So let's add this to the canvas. Now we also have to connect credentials to this tool. So let's select create new credential. And of course, we have to add an API key. To get
[23:16] your free API key, simply go to serpapi.com and register for an account. After signing in, copy your API key and add it to N8N. Hopefully you're starting to see a bit of a pattern here. Integrating with external systems is really simple. Now let's go back to the canvas. And let's just move these tools around. So this looks nice and neat. We now have a calculator tool and a Serp API tool. You can rename nodes by right clicking a node and clicking
[23:46] rename. I'll just call this Google search and let's click on rename. You can also rename a node by double clicking on it and clicking on the name over here. Right now let's test this out. Let's go to the chat window. I'm actually going to copy my question, start a new conversation and let's ask it what is the latest news on Croc 3. We can see the search tool was called and in the response we can see that Croc 3 is available to members of X who have
[24:17] this subscription plan and users who subscribe directly to Croc's platform. So we could ask other questions like what the current weather is etc. And our agent will now be able to use our web search tool to retrieve up to date information. Now let's step things up by adding the ability for our agent to read and send emails. Now we could simply click on add tools and within tools we could search for Gmail and within the Gmail node we have different operations. Like we've
[24:48] got the ability to send emails, we could reply to emails, we could mark emails as read, we could search emails etc. So this means we could add one node for sending an email like so and then we could search for Gmail again and under operation we could click on get many and then add it to this and we could add additional nodes that will now tag emails, read specific emails etc. But
[25:18] during the course of this video we will also be interacting with calendars and fetching contact information. So you can imagine we will have quite a few tools that we are adding to this agent and a general rule of thumb is to keep the amount of tools to 10 tools or less. Otherwise you'll start to confuse the agent. So a better approach is to create specialized worker agents that each have a very specific function. So what we'll do now is we will create an email agent that will
[25:50] be responsible for doing anything email related and then this agent will simply call that agent as a tool. So effectively this agent becomes a kind of a supervisor that will delegate tasks between different agent nodes. In fact while we're here let's already set that up. I'm going to rename this agent to supervisor and I'm also going to rename it on the top left to supervisor agent and I like adding these little emojis just to
[26:21] indicate that this workflow is an agent. So let's save this workflow, let's go back to home and let's create a new workflow and let's call this one email agent. Let's save this and let's add a trigger. Now this agent will be triggered by another workflow. So the type of trigger that we want to add is this when executed by another workflow. Under input data mode we can change this from accept all data. This simply means we
[26:53] will grab whatever data is being passed from our supervisor and work with that data. Then let's click on set mock data. This will simply allow us to manually specify data so that we can test this. So we don't have to run the supervisor workflow every time we just want to test this workflow in isolation. So I'll just actually remove line four and let's rename this property from name to query and this must be called query and for the value let's say
[27:24] send an email to leon.fundsale at gmail.com asking for a status update. Let's save this, let's go back to the canvas and let's click on add and let's add our AI agent. Let's change the source for the prompt to the find below and let's add the query property into the prompt. Let's go back to the canvas and let's add our chat model and of course we will be using open AI and we can leave it on gpt 4.0 mini and again I like
[27:54] to go to settings let's change the time zone to joe anisberg let's save this. For these worker agents we don't have to assign memory as these agents are kind of performing a once-off task. They don't have to remember the conversation history so they will receive an instruction from the supervisor execute on that instruction and provide some response. We do however want to assign a tool so under the list of tools let's search for gmail and here we do have to assign credentials. Let's
[28:26] create a new credential and for these google nodes we do have to provide the client id and a client secret. So to get the client secret and key go to cloud.google.com and click on console. Then let's create a new project by clicking on this drop down. Let's click on new project and I'm going to rename this to nitin masterclass and let's create this project. Once this is done click on select project or you could simply click on this drop down and select your project from
[28:56] there. Now we have to enable access to the gmail api so let's click on this navigation menu click on apis and services then click on library and search for gmail open up the gmail api and enable access to the gmail api. Now we have to set up our oauth consent screen thankfully this is something we only have to do once. Click on oauth consent screen click on get started. Now let's give our app a name. I'm
[29:27] simply going to call this nitin masterclass let's select a support email and let's click on next. For the audience select external click on next enter contact information so I'll enter my email address next under finish just click on agree and continue then create. Now click on audience and click on publish app and confirm then back in the menu let's go back to api and services and let's
[29:58] click on credentials. Now we can finally create our oauth credentials by clicking on create credentials oauth client id for the application type select web interface for the name just enter nitin and under authorized redirect uris click on add uri and back in nitin we can simply copy this oauth redirect url and paste it into this field and let's click on create. The good news is we only had to do this process once we will reuse
[30:30] this client id and secret throughout the course of this video so simply copy your client id and add it to nitin and let's do the same thing for the client secret and let's add that to nitin as well then click on sign in with google select your email address if you see this screen simply click on advanced and click on go to nitin masterclass there is a process that you can follow to verify these apps but for the tutorial's sake that's completely unnecessary let's click on continue to nitin
[31:01] masterclass let's select all of these permissions and continue and our connection was successful let's close this pop-up and now we can finally continue with this note first i'm going to rename this note to send and for the operation we'll select send now for the two email address and the subject we want our ai agent to intelligently populate these fields on our behalf for example the user would say something like send an email to this email address asking for an
[31:31] update but we want our agent to intelligently extract the email address and then populate it into the two field and generate a subject line and an email body based on this context so in order to tell nitin that the agent should populate these fields you can simply click on this button titled let the model define this parameter i'll do it for the two field for the subject field and for the message field we can also click on options and let's select a paint nitin attribution
[32:03] and disable this this will simply remove the text at the bottom of the email which says something like email sent using nitin let's go back to the canvas and let's test this out by running this workflow so our agent is running it's calling the send tool and looking at my email i received this email requesting a status update with this text that was generated by our ai agent so i'm actually going to reply to this email to say we're slightly behind schedule but it should be done by wednesday it's
[32:33] sent this and if i refresh this inbox and this is now the inbox that our agent is linked to we can see that response from the other leon the other email address so i'm not going to open this mail as i actually want to add the ability for our agent to read emails on our behalf so what we can do is click on tool let's go to gmail select the gmail tool and under operation let's select get many we don't want to return all of the emails so let's just say we want the last five emails and
[33:06] under filters let's select red status and let's select unread emails only i'm also going to rename this note to unread and let's rename this well let's go back to this trigger node and let's edit this value to something like what are my current unread emails let's save this let's go back to the canvas and let's run this workflow we can now see that the unread note was called and if we open up our agent node we can see on the right hand side that
[33:36] it says you currently have one unread email here are the details the email was sent from leon with the subject line of request for status update along with we're slightly behind schedule and we should be done by wednesday great so our agent is now able to retrieve our emails we can also get our agent to reply to emails and i think that will be the final function that we add to this email agent so again let's go to gmail let's add a gmail tool under operation let's select reply
[34:09] and for the message id we'll simply let the agent populate this and we'll also let the agent generate the message let's also rename this to reply and let's go back to the canvas and let's test this out i'm going to change this trigger node so for this message let's say reply to leon asking him to complete the work by tuesday like so it saved us let's go back to the canvas and it's execute this
[34:40] workflow so we can see it's searching the unread emails and now it's replying to the email and if i pull up my emails again we can see that response from my dev account saying thank you for the update but could you please complete the work by tuesday great we can actually now rename this agent to let's just call this email agent not that it matters but i like to keep things organized we can now save this and now we need to add this agent as a tool to our supervisor agent let's go back to the dashboard let's click on
[35:12] supervisor and on the supervisor let's click on add tools and let's select call nnn workflow tool let's give our tool a name like email agent and in the description we need to tell our supervisor agent when to use this tool so our description could be use this tool for any email related actions then from the list of workflows let's select our email agent and lastly i am going to rename this to email like so let's
[35:44] go back to the canvas let's put it next to the rest of these tools let's save this workflow and let's test it let's say send an email to my email address inviting leon to my birthday party next saturday let's send this and we can see our agent is running and it is calling our email worker which completed and in the chat response it says the invitation to your birthday party has been successfully sent to leon if you need anything else feel free to ask now there's a problem looking at
[36:16] my emails i didn't receive this email and this was intentional i want to show you how to troubleshoot workflows in nnn we can see the email node was indeed called and on the left hand side we can see the query that was passed into that node along with the output on the right hand side we can also view the data that was passed to this email workflow by going back to the dashboard let's click on email agent and let's click on executions here we can view all
[36:47] the past executions for this workflow and if we look at the last one we can see exactly what was passed into this flow we can see the query has this structure which contains the subject line along with the body of the email etc but this doesn't include the email address at all so it seemed like our supervisor attempted to write the email itself and totally forgot to include the email address which is not what we want in fact if we open up this send node we can see this email was
[37:19] sent to leon at example.com so this agent unfortunately had no idea what the supervisor wanted it to do so it just kind of made up its own email address now what we need to change here is the supervisor node should only be responsible for delegating tasks between the workers it should not try and write emails or content or anything itself its function is simply to orchestrate the tasks between the different agents so let's fix that ASAP let's
[37:49] go back to the supervisor let's open the supervisor node let's go to the system message and let's improve the system message so let's add your job is to orchestrate activities between different agents and then formulate a friendly response back to the user and just to really hit home let's also add you should never write emails create content create summaries yourself your job is to call agents and tools in the correct sequence and finally think carefully about the
[38:20] sequence of events some tools might require you to first call another tool in order to pass it to correct information now this should greatly improve this behavior let's go back to the canvas and let's try this again they sent exactly the same message so i will just say send an email to leon.fonsal at gmail.com inviting leon to my birthday party next saturday let's send this we can see our agent is calling the email worker node and finally it says the invitation has been
[38:50] sent and again although it said it sent the email i still didn't receive it this agent still sent the email to leon at test.com again this was intentional and i bet you've been experiencing the same issues when using the grok models and that is because these agents need a lot of intelligence in order to function correctly so for the model i'm going to switch it from mini to gpt4 0 and if you do have access to 0.3 mini i
[39:22] highly recommend using that as well but now look at the difference when we save this it's going to open chat let's send that exact same message again like so so of course it's going to call the email tool but it's not saying it sent the invitation and looking at my emails i did indeed receive the invitation this time so hopefully you can see what a difference these more intelligent models can make because we probably could use 0 4 mini or maybe one of the llama 3.3 models but you
[39:53] would have to prompt the crap out of this thing in the system prompt to get a decent result but do take note you can still use a cheaper model for some of these specific workers this email agent does not require an advanced model and the open source models or something like 4.0 mini is perfect for this use case next we're going to add a contacts agent to this workflow because passing the entire email address into the sprint is not practical we basically just want to say something
[40:23] like sending email to john inviting him to my birthday party next saturday and our agent should somehow retrieve the email address that be stored against john from some sort of database and then send the email to that address now let's create this contacts agent let's go back to home let's create a workflow and let's rename this to contacts agent for the trigger i'm sure you've guessed it we will add this when executed by another workflow trigger we'll set the input data mode to accept
[40:56] all data we can pull some mock data in by clicking this link then let's delete all of this and i'll just add back like so let's rename this property to query and for the text let's say retrieve john's email address let's save this let's go back to canvas let's click on add advanced ai ai agent of course let's change the source to the find below and let's add the query to this text let's go back
[41:27] to the canvas let's add a chat model we'll go to open ai chat model we can use gpt for omini in this case as we don't need an intelligent model and now under tools let's search for google contacts tool here we have to set up credentials again so let's click on create new credential and we have to specify a client id and secret so all we have to do is go back to google cloud platform and copy these exact same values so i'll copy the id let's also copy the secret like so
[42:00] and of course if you already close this pop-up all you have to do is go back to credentials click on this oauth 2.0 client id and you can get your client id and the secret on this screen now before we click on this sign in with google button we do have to first allow access to this api so back in google cloud let's go to menu let's go to apis and services app library then let's search for contacts api you will notice though that the contacts api has actually been deprecated
[42:32] so instead we need to use this google people api instead let's simply enable this api then let's go back to n8n let's click on sign in with google let's select our email account i'll just simply tell this to continue let's click on continue again and that should be it let's close this pop-up and let's close this screen as well and for the operation let's simply select get many we'll simply limit this to i don't know five results
[43:03] for the fields let's grab the email addresses and the names of these contacts it's an able use query and for the query it's simply let the ai decide and that should be it let's go back to the canvas and one thing we didn't do was actually set up our google contacts account thankfully this is super easy go to contacts.google.com and sign into your google account initially we won't have any contacts so let's create one i'll simply
[43:35] call this person john and for the last time i'll just call him demo and let's add an email address i'm actually just going to use my personal email address again and let's save this right so now in our contacts we do have one contact called john so in our workflow if we send a message retrieve john's email address we should get an email back so let's run this it's searching our contacts and if we look at the output we can see that john's email is well my email address cool i am
[44:08] going to rename this node to search and let's also add another google contacts node so google contacts let's change the operation to create and for the family name let's simply let the ai decide the same with the given name then for the additional fields let's add emails and other emails let's click on add email and for the type we'll just select home and for the value of the email address we'll let the ai agent
[44:39] populate this field let's also rename this node to create let's go back to the canvas and let's change this trigger node's input to to create a contact so let's say add a new contact for mary lamb with the email address mary at test dot com let's save this go back to the canvas and let's test this workflow we can see the create node is executing and we
[45:10] got something back let's have a look at the response the contact for mary has been successfully created and going back to google contacts let's refresh this and we now have mary in our database with her email address we can also change contacts so again let's change our dummy data in the trigger node to update mary lamb's email address to mary dot lamb at test dot com let's save this and
[45:40] under tools let's search for google contacts under operation let's change this to update and for the contact id we'll let the agent populate this and for the fields let's let the agent decide as well now we have to also tell it which fields are allowed to be updated so let's click on add field let's select emails and under add email let's select home and for the value let's let the agent decide great let's go back to the canvas oh let's also rename this
[46:11] to update like so and let's actually test this workflow so it's searching for mary's details and we can see the update node actually failed saying the field dot join is not a function all right so i think this field selection is not going to work if the model tries to decide so instead let's go back to fixed and then in this list let's select email addresses and let's select names as well i think only selecting emails will work but let's give
[46:41] this a spin let's test this workflow so it's searching for mary now it's updating mary which was successful this time and the agent says mary lamb's email address has been updated and going back to our contacts it's showing mary at test at the moment and if we refresh this this now changed to mary.lam at test.com and of course this also changed the name so this is not ideal it's just fixed this again by changing this to mary and lamb let's save this let's go back to
[47:11] our contacts and i think the reason that happened is in this update node under update fields let's also add the family name and we'll let the agent decide and let's also add the given name and let's let the agent decide this as well so hopefully this will resolve this issue i'm just going to rename this back to mary lamb let's change the email to mary at test.com let's save this and let's give this agent a spin it's updating mary and if we refresh this this
[47:43] is now working correctly i could have cut this part out in editing but i do want to show you the process of troubleshooting and fiddling with these nodes until things work now that we have our contacts agent we can assign it to our supervisor so let's go to supervisor let's click on add tools let's call initn workflow tool and let's call this contacts agent for the description let's say use this tool for all contacts related actions then from the list of workflows let's
[48:13] select contacts agent cool and i'm also going to rename this to contacts now as a little aside you might see other tutorials injecting the list of tools into the system prompt but quite honestly it's quite redundant what initn will do is look at all the tools assigned to the agent and then dynamically inject the list of tools into the system prompt itself so there's no need for you to do that all you have to do is ensure that the description of each tool is accurate now
[48:44] let's try this out this time let's say send an email to john inviting him to my birthday party next saturday now watch what happens so let's send this message first it's going to our list of contacts to retrieve john's email address then it's sending the email as well and indeed i did receive that email addressed to john you might notice that the google search tool gets called from time to time as well and this is simply the agent retrieving the date for next saturday so the
[49:14] google search thing can be quite random but the agents usually got some reason for it now that we have the ability to send emails and easily access contacts let's also set up a calendar agent so when we invite john to our birthday the agent will also create an entry in our calendar let's go back to our dashboard let's create our workflow obviously let's give it a name like calendar agent and for the trigger let's select when executed by another workflow then let's select acceptable
[49:45] data let's pull in mock data let's remove all of this let's remove this and of course we need to change this to query and for the text let's say create a calendar entry for one march at 3 p.m titled birthday party let's save this let's go back to the canvas and generally speaking whenever we're dealing with dates and times i highly recommend going to settings then for the time zone let's select our local time zone so that will be Joanna's book for me
[50:15] let's save this and of course let's add our ai agent let's change the source to the query property over here let's go back to our canvas let's add our chat model which will be the openai chat model and we'll simply use gpt 4.0 mini let's add our first calendar tool let's search for google calendar tool then let's set up our credentials and as per usual we need the client id and secret so back to google cloud platform let's click on
[50:45] library let's search for calendar let's select google calendar api and let's enable access to this api let's go back to our credentials let's click on the oauth credentials i'll copy the client id and add it to n8n let's also add this client secret like so let's click on sign in with google let's select our google account on this screen let's simply go to n8n let's provide permissions to all of these let's continue and that's it the connection to google calendar was
[51:16] successful so for the operation we do want to create a calendar event i'm actually going to rename this note to create as well then in the calendar list i'm going to select my email account and for the start date and time i'm going to let the agent decide this and i'll do the same for the end date and time then under add additional fields scroll down to summary and we'll let the ai agent select this as well summary is simply the title of the invite let's go back to the
[51:46] canvas and looking at my calendar i don't have any invites for the first of march so in n8n let's run this workflow this is creating the event and looking at the response we can see that i've created the calendar entry for birthday party on the first of march at 3 pm but it used a year 2024 and that's because the allo m things we stole in 2024 so to fix that we can simply open up the agent then let's add a system message and we'll switch over
[52:18] to expression let's expand this and what we want to do now is simply tell the agent what the current date and time is like we did with the supervisor so let's say the current date and time is double curly braces dollar sign we'll use now dot to string that one so this one i'll tell the agent what the current date and time is and i will greatly fix the issues with creating these events let's run this again so it's creating our
[52:48] event we can see it created the entry for march 5th 2025 and going to our calendar we can indeed see that entry wonderful now let's also give our agent the ability to check our calendar entries for us so let's add tools let's go to google calendar tool then under operation let's select get many and from the calendar list let's select our email account then for the limit i'll let the agent decide the same for the after and the same
[53:19] for the before then let's go back to the canvas i'm just going to rename this node to search and also let's go back to our trigger node and let's say what does my calendar look like on one march so let's save this let's test this workflow and it's calling the search tool and finally our agent is saying that on one march 2025 you have the following scheduled events and it's only the one which is the birthday party between three and five fantastic
[53:50] let's go ahead and add this agent to our supervisor so let's go back to the dashboard let's go to supervisor let's click on add tools call in it in workflow tool and let's call this i'm also going to rename this node to calendar like so then for the description let's say use this tool for any calendar related actions and then from the list of workflows let's select the calendar agent and hopefully you're starting to see a pattern this is actually a lot of fun to use so now that we have a
[54:22] calendar agent set up we can provide some nice and complex scenarios like saying john an email asking if you wanted to play squash tomorrow at 2 pm also create an entry in my calendar let's just go ahead and send this and this is actually going to orchestrate quite a few activities first it's fetching john's email address then it's sending the email and creating the calendar entry first let's have a look at the email here i've received an email with the title squash
[54:52] came tomorrow and is saying hi john hope you're doing well i was wondering if you wanted to play squash tomorrow at 2 pm awesome let's also have a look at our calendar so let's go back to tomorrow and here we can see the entry called squash with john between two and three i don't know about you but this is phenomenal we still have two exciting tools to go next we're going to create a content creator and we can use this creative writer to come up with blog posts social media posts
[55:24] effectively we can add any type of creative writing over to this agent and after that i'll show you how to create a custom knowledge base where you can upload your own documents to effectively train your agent to answer questions from your own documents right let's go back to the dashboard and let's create this content creator agent let's rename this to content creator and of course for the trigger let's select when executed by another workflow from the drop down let's select accept all data
[55:56] let's pull in some mock data let's delete all of this let's delete this guy and let's rename this to query and for the value let's also pass in generate 10 unique title ideas for a youtube tutorial on building ai agents in n18 let's save this go back to the canvas and of course we'll add our ai agent we'll change the source to this query property and i am going to add a system message for this agent and this will simply be something
[56:26] like this add a role and let's say you are an expert content writer create seo-friendly posts articles etc based on the user's query and then of course let's also assign a large language model or any model that's very good at writing content and believe it or not that's actually all we have to do with this agent let's test this workflow just to make sure it's all working and i get my titles back awesome let's simply attach this agent to our supervisor so back over here let's add a
[56:57] tool let's add the call in a 10 workflow tool let's call this content creator i'm just going to rename this note to writer and for the description we can just say use this tool for any creative writing actions and from the list of workflows we'll select our content creator let's go back to the canvas let's move it next to the rest of our tools let's save this and of course let's give it a spin like write a detailed summary of the new grok3 model so what we're expecting to
[57:28] happen here is for the google search to be called and now our writer is being called as well to write the summary for us right and having a look at the output we can see that our writer came up with this beautiful article using its research results on this grok3 model so if we wanted to we could even instruct the agent to send that report via email as well that is how powerful this agent is getting now finally let's add a custom knowledge base to this agent the custom knowledge base will allow us to upload documents
[57:58] from our own machine and we can then ask the agent questions about those documents so these could be like pdf files it could be csv files word documents or anything you want so this allows our agent to go online to perform research but it can also answer questions from our own custom knowledge base almost like a second brain to set this all up we do have to create a database where all this custom data will be stored for this we will be using pinecone and this is free to
[58:29] use as well go to pinecone.io and sign up for your account then from this dashboard click on create index and give it a name like n8n masterclass and within configuration select text embedding3small this guy over here and click on create index right now we have our database set up and the next thing we want to do is to create an api key by going to api keys click on create api
[59:00] key and let's name this n8n masterclass and let's create this key you can simply copy this key and store it in a safe place but we will come back to this key in a few minutes first let's go back to n8n and let's click on add tool then let's select pinecone vector store click on create credential and then copy your api key and add it to n8n and let's save this great let's close this and for the name let's
[59:30] just simply call this knowledge base and i'm also going to rename this note to knowledge base as well and for the description let's enter use this tool when ask questions from our own knowledge base then from the list of indexes select n8n masterclass we can leave this value as 4 but this value simply tells the database how many documents it should return right let's then move the knowledge base down next to our tools and we also have to attach an embedding node so let's select openai **[01:00:02]** and for the model selection we need to use the same model that we used when creating our pinecone index which was text embedding 3 small great at the moment if we ask agent any questions about our knowledge base it won't return anything as the database is currently empty we can see that by going to database let's click on our index and here we can see we don't have any records at the moment so what we'll do is we'll create a new workflow that is kind of a utilities workflow that we can use to upload documents into **[01:00:34]** the database so let's give it a name i'm actually going to select this tool icon this time and let's call it update knowledge base for the trigger let's assign a form submission so we'll show a form to the user that they can use to upload documents for the title let's just call this upload documents for the field name let's call this file for the element type let's select file i'm going to disable multiple files as that does mean you have to deal with loops and things like that we'll **[01:01:04]** simply keep it simple and this is indeed a required field let's go back to the canvas and let's click on test workflow and here we can see that we do indeed have a form i'm going to select the sample invoice on my machine and click on submit we can now close this pop up and if we have a look at this node we can see that the output is a binary file this simply means this is an image or a text file whatever after this let's add a new node and let's search for **[01:01:35]** pinecone vector store and for the action let's select add documents to vector store from the pinecone index list let's select our database let's go back to the canvas under embeddings let's add the openai embedding node let's select text embedding3small then let's also add a document loader let's select the default data loader and instead of json let's change this to binary since this node is receiving a binary file on **[01:02:06]** the left hand side we can leave all the other fields on the default values let's go back to the canvas and let's add a text splitter and i'll add the recursive character text splitter i'll change the chunk size to 2000 with a chunk overlap of 200 so how this works is when we upload a document we don't want to upload a document in its entirety into the database and the reason is if the agent queried that document and let's say this was like a 20 000 character **[01:02:37]** document or maybe something with thousands of pages we don't want to inject that entire document into the context or into the prompt to the agent that's going to get super expensive be very slow and most likely produce rubbish results so the solution is to take that large document and to chunk it into smaller pieces and we can determine the size of those chunks so that means when we query the database only the most relevant chunks will be returned and **[01:03:07]** only those chunks will be injected into the prompt let's go back to the canvas and i think we should be able to test this let's click on test workflow i'll upload the sample invoice let's click on submit and this can be any document by the way you can take lecture notes it can be receipts pretty much anything so this execution was successful and if we go back to the database i'll refresh this page and we can see we now have two documents so my invoice was basically split into two separate documents and let **[01:03:39]** it into the database now that we've got data in our database we should be able to test our agent so let's go back to this and let's ask a question relevant to that document and just to show you what that sample invoice looks like i actually just downloaded this online and this seems to be some internet invoice and if we look at the total amount it's 453 euros so let's ask our agent what was the gross amount of the invoice so looking at **[01:04:11]** our workflow it correctly determined to call our knowledge base and in the response we are getting the correct value back so now your agent also has access to your second brain which is a custom knowledge base which you can upload any documentation to if you ever wanted to delete documents from this knowledge base simply go to the database and delete these entries so you can actually take this agent to the next level the next step is to integrate this agent into something like whatsapp or telegram so **[01:04:42]** that you can access it from any where and did you know these agents also have multimodal support so you can have real-time voice conversations with these agents and you can also ask it to describe images for you check out how to do that in this video over here hit the like button subscribe and share this video and i'll see you in the next one bye bye