n8n Tutorial: AI Agents with Human Feedback β
n8nRecentπ
2025-02-11
Multi-Agent Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MULTI-AGENT SYSTEM β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββ β
β β ORCHESTRATOR β β
β β (Manager Agent) β β
β βββββββββββ¬ββββββββββ β
β β β
β ββββββββββββββββββΌβββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ β
β β Research Agent β β Writer Agent β β Reviewer Agent β β
β β β β β β β β
β β β’ Web Search β β β’ Compose β β β’ Validate β β
β β β’ Data Gather β β β’ Format β β β’ Improve β β
β ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β SHARED TOOLS β β
β β [Web API] [File System] [Database] [Code Executor] β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββAgent Workflow β
- Agent configuration
- Tool integration
- Memory management
Transcript β
[00:00] Hi there, I want to go over the benefits of using human in the loop in your N8N workflows. So looking at this example, we've got an awesome multi-agent AI team that can write any story for us. But it's got one massive flaw. When I ask it to write a story, the workflow will execute one agent at a time in sequence, and when it's done writing the story, it will email the complete story to my email account. But what's the problem with this? Well, each agent in this workflow is responsible
[00:31] for a critical component within the story. So the premise agent will come up with a unique premise, and it will then pass that premise over to the character creator. But what happens if we don't like the premise of the story? Well, at no point does this workflow allow us to provide feedback before continuing on to the next agent. So we just kind of have to wait for this entire thing to complete before we can provide any type of feedback. Now this is a waste of time, a waste of tokens, and a waste of cost. Thankfully,
[01:02] N8N now allows us to add human in the loop before continuing on to the next node in this workflow. So here is that exact same workflow, but this time the workflow will wait for us to provide feedback before continuing. And let's have a look at what this actually looks like. So let's say, create a short horror story about teenagers and a cabin in the woods. And what will happen now is our workflow will start executing, and after the agent generated the premise, it will wait for the user's feedback before continuing.
[01:32] So here's the premise of the story that it came up with. Now we have an opportunity to change this premise any way we want. So we can just click on Respond, and this will open up a unique form provided by N8N to provide this feedback. And what's really cool is you can customize this form if you want. So you could add additional input fields as well. So let's say, make this a slasher form instead, and let's submit this. And going back to N8N, the agent was called again, this time including our changes, and now
[02:04] it's waiting for our feedback again. And now our story wasn't the change to a MOSP killer. So let's start it to continue to the next step in the story. And we can see the next step in the workflow was triggered, and it's created our characters for the story and waiting for our feedback. And going back to Telegram, we can see the details of each of the characters in our story, and it's created three different characters. Let's say we wanted a fourth character as well. So in this form, let's simply provide feedback like, add a fourth character called Slime, he
[02:34] is new to the group and acting suspicious. Let's save this, and now we can see in N8N that we've now gone back to our character agent. We can indeed see the fourth character called Slime Foster with all of his character details. So let's continue with the story. Now we get the story outline, so if we wanted to, we could also change the outline. So let's say something like, end the story with a cliffhanger. All right, so now we get an update from the story right there. And looking at Act 3, we can see this part here
[03:06] saying the killer is defeated, but as the survivors begin to mend their bonds, a final horrifying message appears on the cabin's wall, anything that the killer may have a connection to Slime. Wow, what a cliffhanger. Let's continue with the story. So we'll just say, continue, let's admit this. Now the workflow is writing the final story, after which it was sent the full story to my email address. So in Telegram we get this message saying, "Good news, your story is ready," and was sent to your email address. And looking at the
[03:36] email, we get this AI-generated title for the story, along with five chapters. The human interloop feature can be a game changer for many different types of use cases, whether you're generating content, calling tools, updating databases, or pretty much any process that needs verification. Now let's have a look at building this. Let's start by creating a new workflow, and let's call this human interloop tutorial. Let's add our first step, and for this I am going to add a Telegram
[04:06] trigger, and more specifically, the on message trigger. I will assume that you are already familiar with using Telegram with N8n. If you're not, I already have a video on setting up Telegram with N8n, so check out that video first, and then come back to this video. So when we receive a message via Telegram, that will trigger this workflow. Then let's get some data into this workflow by clicking on test workflow, and let's send the message from our Telegram chatbot. Great, now we can see the message that we just received. Let's add our first
[04:38] AI agent, and let's rename this agent to premise generator. For the source of the prompt, let's select define below, and let's add the text from the Telegram trigger. Then under add options, let's also add the system message. So I'm actually just going to switch this over to expression, let's expand this, and now we can see what we're doing. I'm going to paste in the following system prompt. I do want to mention at this point that you can download this workflow for free, and you will find the link in the description of
[05:09] this video. That link also includes all the system prompts that I used throughout this video. So if you don't want to type all of this out, simply copy and paste it from that document. And this is just giving basic instructions on how to write a premise for a story. Let's go back to the canvas, and let's add our large language model. For the model, I will be using OpenAI, but to keep things simple, I'm going to use GPT40 Mini. Cool, so let's go back to the canvas, and let's execute this premise generator agent. All right,
[05:41] looking at the output, we can see that the premise was generated, and it would be tempting at this point to simply add another agent, and that agent could be responsible for creating the characters. But we want to add human in the loop at this stage, so that we can review this premise and suggest any changes. So how do we do that? First, let's click on add, and let's have a look at what we can find in this human in the loop node. Here we have human in the loop integration with Discord, Gmail, Google Chat,
[06:12] Outlook, email, Slack, and Telegram. For our use case, we'll select Telegram, and ensure that the operation is set to send and wait for response. And for the chat ID, I'll switch over to schema, under the Telegram trigger, let's add the chat ID, and for the message, let's grab the output that was generated by the premise agent, like so. We can also select the response type. For approval, it will show buttons to the user to approve or decline this request. In fact, let me show you what
[06:44] that looks like. So under approval options, we can change this from approve only to approve and disapprove, and this will show two different buttons. So if I had to execute this, it will send the premise to Telegram, and if we scroll down, we've got these options to approve or decline this request. So I'll simply approve it, and back in Telegram, we can see this structure coming back with approval set to true. So now you can just use a simple if statement to decide where to go next. But what we want
[07:15] to do is change the response type from approval to free text, and do take note you can create a custom form as well, which allows you to add multiple input fields on the form, but we'll keep it simple by selecting free text. And if I send this to Telegram, you can see what that looks like. So scrolling down, we get this button that says respond, and if we open this up, we get that full premise, and we get a single field where we can enter our response. And of course, if you use the custom form, you could add additional fields
[07:46] that you can use throughout your process. Let's simply say, change this to a comedy, and let's submit this. Cool. So back in 8n, we now get this text back with the feedback from the user. So depending on what the user entered, we either need to continue with the workflow or pass this feedback back to the agent. So how can we determine whether that text means we want to continue or make changes? Thankfully, that's very easy. We can use the classifier now to do this. So simply
[08:17] search for text classifier, then we need to provide the text to classify. So we'll simply grab this text that we received from Telegram, like so. And now we have two different categories. One is to continue with the process, and the second is to send a revision. I'll simply call this revise. Now we simply need to describe these fields so that the large language model can understand when to classify the text under each of these categories. For the continue category, I'll simply enter the text
[08:49] indicates that the user approves and the process may continue. And for the revise category, I'll simply enter the text is requesting changes or revisions. Cool. So we can close this classifier, we can assign a large language model to it. I'll simply reuse the same model for all of our different nodes. And in that instance, I actually like to move this model above the flow, like so. And when we run this classifier, we can see it's going down the revise path. And that
[09:20] is because we said changes to a comedy. So it's determined we want to make revisions. Okay, cool. So the user asked for revisions, but how the heck do we get this revision back into this AI agent? Now, if we look at this agent node, we can see that the agent is expecting the input to come from JSON dot message dot text. This is simply saying that the node that is calling this agent needs to have a field called message dot text that contains the prompt. So what we could simply do is add a new
[09:51] node, let's create a set node. And in the set node, let's create that field. So we'll create a field called message dot text. And within the text, we can switch to expression, let's open this up. And what we can do is add this instruction from the user, right, like change this to a comedy, but the agent might be slightly confused by just receiving the text like this, like what are we referring to over here. So it is basically two approaches that we can follow here. Either we can add agent memory, but I personally think
[10:23] that's overkill for what we're trying to achieve here. So what I like to do here is to simply add everything we need into this prompt. So the start of the prompt, we can simply say the user has suggested changes. Here is your previous attempt at writing the premise, then we can simply grab the premise that was generated by the agent. And finally, we can add here is the user's feedback, followed by the feedback that was provided by the user. And that's actually all we have to do. So let's go back to
[10:54] the canvas, let's attach this edit fields node to our premise generator. So I'll just clean some of the stuff up. I'm going to rename this node to premise feedback is also renamed the edit fields node to edit premise. Cool. Let's test this out. So let's run this workflow in telegram. Let's send a new message. I'll just send the same story again, and I will be using the same story throughout this tutorial. So I'm just going to copy it. Let's send this. All right, so we're
[11:24] getting this premise back. And let's go to respond. Let's give feedback like change this to a slasher instead. Now we get the new premise back. And this time, this is a slasher. And in in it, and we can see that this revision path was indeed followed. And our feedback was passed to the agent. Now this time, let's simply approve this premise. So in this form, let's say this is good. And now we're following the continue path. Right now we're ready to add the next step in our story writer. And that is the character
[11:55] creator. And before we do that, I do like to add these sticky nodes, just to highlight different functionality within my workflow. So let's simply expand this and let's double click on the sticky. And let's add a new heading. And I'll call this premise generation. Awesome. Now let's add our character creator. Let's click on add node. Let's add advanced AI and AI agent. And on the source for prompt, change it to define below. And what we're going to do here is
[12:26] actually write a new expression and we'll select JSON dot message dot text. This is identical to the source of this first agent as well. It's simply this, this will simply allow us to grab the input from this type of edit node. So we'll be proactive and set that in the meantime. Now it's not going to work as is. And that is because the text classifier node is the one calling our agent and the text classifier node is passing the data in a different
[12:57] structure. So it's passing the data in this data field with a text property. And of course, our agent is expecting the values to come from message dot text. Again, this is a very easy fix. We'll simply add another set node just before our agent node. And within our edit fields node, let's add a new property. And of course, we'll call this message dot text. And for the value, we'll change this to expression. Let's open this up and let's simply add approved premise. And of course, the
[13:30] premise was the last value that was generated by the premise generator agent. All of these errors will go away in a minute. Let's simply go back and let's wrap up this AI agent node. So this means we will receive our text from this edit fields node, but we also have to provide a system prompt to this agent. Let's switch over to expression. Let's expand this. And for the system prompt, let's add the simple prompt that tells the agent how to create a character sheet. So we
[14:01] need the name for the character, the core of the character, wants and needs, relationships between other characters, etc. And again, you can download all of these prompts in the description of this video, absolutely free for the chat model will simply reuse the same open AI node like so. Let's also rename our agent to character developer. And let's run this workflow just so we can get some data into this workflow. So I'll send my story again. So this time, we'll simply approve
[14:31] this premise. So I'll just type continue. Let's submit this. And back in n8n, we can see that this agent generated our characters for us. And again, we want to add human in the loop in this process. So all we have to do is click on add nodes, select human in the loop, let's select telegram. I'll just select the correct credentials for the chat ID. We can grab that from the telegram trigger node like so for the message. Well, actually just grab the output from the character developer agent for the response type.
[15:03] Let's set it to free text. And let's actually run the step. I'm also going to rename this node to character feedback. Let's click on rename, let's go back to the canvas. And now we'll simply create the same process that we followed for the premise generator. So after we receive feedback, we need to classify that feedback. So let's search for text classifier. And for the text to classify, we can actually take a little bit of a shortcut from this point forward. We can simply go back to this premise generation nodes and simply copy the
[15:35] values from there. So for the text to classify, I'll copy this value. And in this node, we'll switch over to expression and copy in that value. Cool. For the categories, we'll do exactly the same thing. We'll have two categories. So I'll call this one continue. And this one we'll call revise. Then for the descriptions, I'll also copy those descriptions from this node. So I'm going to copy this and I'm going to copy this and paste those values into these fields like so. And for the large
[16:07] language model, we'll just reuse the same open AI node again. And if there is a revision required, we'll simply call the set node, which we can rename to edit characters. And for the fields, we'll create a new field called message.text. And for the text, I'll switch this over to expression. Let's open this up. And I'm going to paste in this text. And all this is doing is it's saying, please make changes to the characters for the approved premise. Then we grabbing the output from the premise agent. Now we're
[16:38] also including the output from the character sheet agent. And we're also including the input from the user. Let's go back to the canvas. And all we have to do is attach this edit node to our character developer. And to pretty things up, I'm going to copy the sticky note and add it just beyond these nodes, like so. Let's rename this to character creator. Great. Let's go ahead and test this. So in n8n, let's send the same story again. So I'll just say continue.
[17:10] Let's submit this. So here are all the different characters. Let's try to make a change to this. And let's add a fourth character. So let's say add a fourth character. This character should be a love interest to live. Let's send this. And let's see what we got back. So we've got the same characters. We still have love. So now we've got this fourth character called Ethan. And yes, Ethan develops a budding romantic interest in love. Awesome. Let's go ahead and approve this. So in this form, let's say I like this. Continue.
[17:42] Now let's add the final agent that will also require human in the loop behavior. Let's add another agent that will be responsible for generating the story outline. Right. So for the sticky, I'm just going to rename this to story outliner. And we're basically going to repeat everything we did within this flow over here. So to speed things up, I'm simply going to copy and paste the values that we used within this flow. And I'll simply make a few small changes. So I'm going to copy this set node and paste it in here. And then let's
[18:14] connect our continue path to the set node. Right now, let's open up this node and let's expand this value. And now what we want to do is to include our characters in this field. So I'll type double curly braces. Then from earlier nodes, let's select character developer dot item dot JSON dot output. I'm simply following a similar format to what we used up here. Cool. Now let's go back to the canvas and let's copy this agent node. Let's
[18:46] paste it down here. Let's connect these two nodes. Let's open up the agent. Let's rename this to story outliner. We will leave the text as message dot text. And for the system prompt, I'm going to remove all of this and paste in these basic instructions on writing a story outline. Cool. Let's go back to go back to the canvas. Let's attach the chat model to our open AI model node. Great. Let's also attach our human in the loop telegram node. So let's open this up. Let's
[19:16] rename this to outline their feedback. Everything will stay the same. Let's go back to the canvas. Let's copy our classifier and paste it down here as well. Like so. Let's attach these two nodes. And I think the classifier will have everything it needs. So it will simply grab the text from the agent. It will either continue or go down the revised path. Cool. Let's copy this edit character set node. Let's paste it down here as well. Here we'll connect the revised path to the set node.
[19:47] Let's open up the set node. Let's expand this text. And now we just have to make two small changes to this. First, let's change the text up here to make changes to the story outline. And let's also include the outline to this context. So here we can just say current story outline and below this let's go to earlier nodes and let's select the story outliner dot item dot JSON dot output. Just like that. And let's also just fix up
[20:17] these curly braces. Cool. So let's go back to the canvas. Oh, let's also rename this node to edit outline and let's attach this node to our story outliner agent. And of course, our text classifier also needs to be added to a large language model. Awesome. Let's go ahead and test this. So back in Telegram, let's ask it to write a story. We'll simply continue with this premise by typing continue. Let's submit this. And now we should receive our character sheet any second
[20:47] now, which we do. So all we have to do is continue with this as well. Let's say continue. Cool. Submit this. And now we're waiting for our story outline. Great. We get our outline. So we've got act one, act two, act three. All right. So let's start by asking for a change to this outline. Let's simply say something like change the ending to a cliffhanger. I'll submit this. Great. So in our workflow, we can see that we've gone down this revised path and it's waiting for our feedback. Back in Telegram, we can see
[21:18] the new outline and in the resolution, we can see that it's far from over, leaving this open for a cliffhanger ending. Now, finally, let's decide to continue with this. So let's type continue. Let's send this. And now we're going down the continue path. And now we can finish up the easiest section in this whole tutorial. We will grab all of the parts that were generated by these previous agents, write the story and send it via email. Of course, you could change this workflow to review the story as well, but at this
[21:49] point, we'll simply send it to the user. Let's click on add notes. Let's add an edit field node. I'm going to rename this to writer input, like so. Then let's click on add notes. Let's add another AI agent node. Let's change the source to the find below. And in this text field, let's add JSON dot message dot text. This will simply grab the value that we've written in this set field. Then in the set node, let's add a new field called message dot
[22:20] text. And for the value, let's switch it to expression. Let's open this up and it's placed in the following fields. And I do have to apologize for all of these errors. Typically, to get rid of those, you would have to execute this flow every single time you make a change just to get data into this flow. But for the sake of the tutorial, I think you guys get it. Then let's add a new node. Let's add advanced AI. Let's add the agent. Let's change the source to define below. Let's switch this to expression.
[22:50] And we will grab the value from the previous node. Cool. Let's also add a system message. So I'll switch this over to expression. Let's expand this. And for the system message, we can write this text. And this prompt provides a lot of instructions to the agent on how to write a compelling story. Great. So let's go back. Let's attach our chat model. So I'll simply do this. I'll attach it to open AI. And finally, to get this agent to send an email or click on tool, we can search for Gmail. I've already set
[23:22] up my Gmail credentials. But if you don't know how to do this, I do have another tutorial on my channel that shows you how to connect Gmail to your agents as well. So I'm simply going to add my Gmail address. For the subject, I actually want the agent to generate the subject for me. So I'm going to click on this button and let's do the same thing for the message. I'll just click on this. And that should be everything we need for this workflow. Let's test it. And in Telegram, let's go through this whole process together. Let's send a story
[23:52] idea. And let's click on respond. And it looks like the story contains some supernatural elements. So let's say change this to a slasher instead. And let's give it a minute to respond. All right. Now we get this new premise, which I'm actually happy with. So I'm just going to say continue and submit. Now we're waiting for the characters to come back. All right. So we received our characters. We've got Lily, we've got Ben, and we've got Rachel. Let's respond. And let's ask you to add another character. So let's say
[24:24] add another mysterious character called Slime. Let's send this and let's see what we get back. All right. So after a few seconds, we get this response back. And now we have this character called Slime. Awesome. Let's respond. Let's say we're happy with this. So we'll continue. Okay. And now we get our outline back. So we've got act one, act two, act three. Let's actually respond. And here I'm just going to say continue. Let's submit this. And in an item, we can see that our agent is executing. The email
[24:55] was sent. We just didn't get any feedback on Telegram, but we can fix that just by adding another node. So we could search for Telegram. On the Telegram, we can select send a text message. We can select our credentials. We can also grab our chat ID. So if I scroll down to Telegram trigger, we can add the chat ID. We can also just add some generic text, like the story was sent to your email address. So if we had to run this again, we will get some feedback back on Telegram. But what we're really interested in is
[25:26] the email that was sent. And looking at my inbox, we can see this email titled Whispers from the grave. And here we have a story containing all of our characters, including slime. I hope you found this video informative. And if you did, hit the like button, subscribe to my channel and share this video. Also check out my other NNN content over here. Otherwise, I'll see you in the next one. Bye bye.