Building AI Agents: Chat Trigger, Memory, and System/User Messages Explained [Part 1]

105.11k views4261 WordsCopy TextShare
n8n
Watch Part 2: https://www.youtube.com/watch?v=lkudvrC3AOU Welcome to Part 1 of the "Building AI Age...
Video Transcript:
hey everyone Max here welcome to the building AI agents tutorial series over the next couple videos we're going to Deep dive into what AI agents are how you can build them using n8n a powerful work for automation [Music] tool by the end of this video you're going to know what an AI agent is and you'll have built a simple AI agent in Ann which means that you'll know how to use the chat trigger which is going to allow you to send messages through UI and send it to an AI agent you'll know how to configure
the chat model for that AI agent you'll know how to configure memory so that your AI agent understands the context of a single Conversation Over multiple messages and we'll also touch on user messages and system messages and some best practices around those because a lot of the success of an AI agent solution comes down to the prompting this is going to be primarily a Hands-On course we're going to be in N end building out AI agents but just to contextualize all this we are going to do a quick few definitions and context for newcomers so
if you're familiar with AI agents and have built them in a different tool or perhaps coded them out yourself and you're looking to just understand how to do it in an ATN you can skip this section but we should be quick I'll also call out that I expect that you have some very basic technical understanding so you know what an API is you know what a variable is but you don't have to be an expert coder I most certainly am not and I almost never write lines of code GPT does that for me these [Music]
days to frame what we're doing let's first Define what an AI agent is and to do that let's quickly Define what a large language model is right so a large language model generates text based on input text it's basically trying to guess what the next word is based on what you input so it's a function right input comes in output comes out an AI agent on the other hand Builds on top of what an llm can do by adding goal oriented functionality that's that task completion that we were talking about it also has the ability
to leverage tools and other such dependencies in short an llm model just take some input text and output text versus an agent can take your input text feed that into an llm make multiple thoughts under the hood those thoughts can decide to use a tool it could use that tool it can ingest the output of that tool back into the llm and decide what that next step is and then finally decide in those many steps once it has the final answer once it has completed your task and output that now there's dozens and probably at
this point hundreds of different tool tools and Frameworks and systems that help you build AI agents we're going to focus on using NN to build our AI agents and in short NN is a workflow automation tool where you can build workflows with multiple steps we call them nodes and some of those steps can be AI agents now the really neat thing about NN workflows is not every step of a use case needs to be solved by an AI agent if the first step in your workflow is to receive an email and check if it's valid
there's probably a really simple Le reject function you could run to check if it meets a certain set of rules relatively speaking it's going to be a lot more expensive and laggy to try and use an AI agent for that job so the nice thing about using a Kul like NN is we have the flexibility to combine these deterministic steps the things we do with traditional programming with the power of LMS to process things like natural language and also multimodal use cases right so images audio video that sort of thing now that we've got the
definitions out of way let's open up n ATN and build our first AI agent so I'm here in my n account and I'm currently inside a project but if you're in your home section this view is going to look very similar what we're going to want to do is to create a new workflow so we'll do that now in my blank workflow there's this add First Step action that I'm going to click and this is going to open up what we call the nodes panel the nodes panel is where you access all the different nodes
in NM that you can use to build your workflow so think of a node as a step in your flow the first option here that we need to decide on is how we trigger the work for how it runs for this example we're going to build a classic chat assistant so the user is going to type something in the AI agent's going to take that perform its task on that message and send back a response so to do that let's pick this on chat message option which is going to add the chat trigger to the
niden canvas this is just one way to start an AI agentic workflow you could use a web hawk or hundreds of different triggers but this is a great way to get started because this chat trigger is integrated with nit 's chat UI so we can actually test it in line and once it's ready to go we can also publish that and then once we're ready to activate this workflow we'll be able to access a public version of this UI so basically creating your own chat GPT this trigger node's job is essentially to collect the user
input and send it along to the next step so let's click this plus button here and again we're going to open up the nodes panel for most of this tutorial series we're going to be hanging out in the advanced AI section but there's hundreds of other nodes for example if you want to fetch something from an app before the AI step or do various conditional logic and whatnot and it then has all those sort of Key Parts ready for you when you need to build that out so let's go into the advanced AI section and
we're going to want to add an AI agent there's various other options here for really specific use cases for example if you need to extract information from a certain input or analyze sentiment most of these you could achieve with an AI agent these are just more opinionated if you're doing that specific use case so let's go ahead and click the AI agent to add it to my workflow once I add the AI agent there's going to be a few things that we need to set up before we can run and test this so the way
AI agent nodes work in N ATN is they have one or more dependencies that they need to run the chat model is always required this is the llm that's going to use under the hood to process semantic text and to think so to speak and then there's various other options that we can also add since the chat model is required let's set that up first but throughout this series we'll get into all the different dependencies and options available for AI agents so to add a chat model let's click this plus here now there's various different
models that you can connect if you'd like to self-host llms olama is going to be a great way to go and it then has a self-hosted AI State you could leverage for that and that's going to let you leverage dozens of Open Source models that you can run on your own Hardware or on your own bare metal since I already have an open AI API key I'm going to go ahead and add the open AI chat model because I'd like to use some of their models so let's click on that the first thing you're going
to want to do is to add your credential now in N credentials are separated from workflow so you can have credentials in one spot for example here I've got my open AI account that I'm leveraging in each one of my workflows that needs this chat model but if you're opening this up for the first time when you click this drop down there's going to be the create new credential button you're going to click that in here you're going to have to add your open AI API key that's out of scope of this video but if
you're not sure how to do that a quick Google search uh should help you there and you only need an organization ID if you belong to various organizations so for most folks you won't have to fill this in so once you have that API key hit save and you should be good to go and then most chat model nodes you will select the specific model that you want to use so here we could use GPT 40 for example and then there's also options here now I'm not going to go into every single option but if
you're ever reading an article on how to tweak some settings in an llm to get the results that you want those are usually going to be found here in the options so now that we have our chat model set up we can test our AI agent using the chat button because we're using a chat trigger here so if I click this let's just write hello and we can see it's running and let's have a look at these logs so what's happened is we can see we got a response from our AI agent hello how can
I assist you today if we open up my AI agent what we can see is we've got some input data this is from our chat trigger here we can see it sent along a session ID that's going to be great in future when we're setting up some memory the action so this was a send message and then the chat input that's the message that we just wrote and then we have some of the settings for our AI agent here that we're going to configure in a sec and we can see the output this is the
message that it outputed there so inside of as you can see we've got the input data that comes into a node we can set up the settings of that node to configure how we want it to complete its step you can reference input data to do that to give it context and then it outputs something so let's have a look at the important parameters when setting up the AI agent so there's the agent type this is the first option here its default is the tools agent and we're going to focus on the tools agent throughout
this tutorial series because it's the most advanced and comprehensive NN AI agent available today there are a few others but with the tools agent you can basically achieve a lot of what these other ones are doing I recommend for most folks unless you really know what you're doing just stick to this as your default the first option here is the prompt Source you can think of this as the user message and if you don't know what that is we'll explain in a moment but this is basically the task that we're asking the AI agent to
complete so AI agents take a task a question then they try to answer they try to complete the thing they were asked to do in here the default is take from previous node automatically so what this does is it expects in its input some input data with the key of chat input so if I go over to Json view here we can see this is actually how the data that's coming into this node is represented so we've got one item of data coming in and it's got this chat input top level text variable here that's
set to hello so as we can see that's what it's referencing here now I could overwrite that I could click on prompt source and change it to Define below and then here I could type out static text so I could say here is my Tusk right and now if I run this of course please go ahead and let me know what task you'd like assistance with it's responding to this text here in short be it using the chat trigger and automatically piping that in through the taken from previous nodes automatically option or through defined below
it's in this text or this text from previous node where you're defining the task the thing that you want your AI agent to accomplish now there's a few other parameters that you can use to control how your AI agent completes your task but one of the most important ways to control the results and the output and basically the efficacy of an AI agentic workflow is with what is called the system message which you can add from the options in here so if I click to add that you can see it does have a default message
which is you are a helpful assistant so we're going to cover a few best practices for system messages but I could do a whole multi-part tutorial series on system messages themselves so know that this is General guidance and it will depend on your specific use case nonetheless there are some best practices that you can abide by before we get into those you can see already we've got two basically places where we're providing text to our AI agent to modulate or to inform how it's going to complete the task right so we have the prompt Source
or the user message as it's called in some of the docs you might read for AI models and then we have the system message so what's the difference between the two so in the user message goes the actual task the instruction for the thing that you want done and then the system message defines the context behavior and rules for how that task should be completed think of the system message as an instruction manual on the tone perspective and functionality of how basically your AI agent completes the tasks it's asked to do crafting your user messages
and system messages are what's collectively known as prompt engineering now again that could be a whole tutorial series and in fact it is an entire profession so let's cover a few Basics right now to get you started but we will do more of a deep dive throughout this tutorial Series so my bare bones checklist when I'm creating system messages is it should define the role of your AI this can be a direct voice like you are a helpful assistant for example this is the default message here it's defining the role of the assistant the next
thing I typically like to provide is the style so this is the tone a great example might be be concise and avoid jargon that's telling it how the output should sound what words it should gravitate towards what length of the text it should be outputting and then the last part of my simple checklist is to make sure that you're adding boundaries to the task now obviously that can look different from task to task but a great example of boundaries would be don't hallucinate that's something if you've been in the AI space for a little bit
you've definitely heard these prompting hacks like don't hallucinate which improve results of these L agents a more tangible example of that could be something like ask clarifying questions if you're not sure what the user wants to ask or don't make assumptions the system message is a great place also to include static context especially with these newer AI models that have very large context windows so an example of a static context could be the time right now the GPT model that we're using right now does not know what time it is right now in the system
message this is a great place where we could add that kind of static context static in the sense of it's not something AI agents using as a tool at runtime so to add this context we can actually use inid in Expressions so I'll switch this to an expression and what this is going to let me do is basically combine JavaScript with my static text very similar to how a mail merge might work in a email marketing program to do that I'll hit open parenthesis and we see Ed 's auto complete here and I can use
the now variable here and I'll add that and this is including the date time stamp here now we can see it's including this date time here I could write something like today is and now we have the date and the time and even the time zone so if we run this again what date is it today we can see that today is November 26th and that is correct that's when I'm shooting this video so that's a very simple example but it shows how we can add context as a system message how we can modulate its
response another great example could be output in German so now when you ask what date is it today is of November my German's not that good and as you saw I'm just writing these in sentences here grouping this by sections and different syntax around prompt writing is definitely helpful here's a quick example template you can borrow already today but we'll get into the details in in future videos here we've added a chat model we're sending in the user message and we're also feeding in context in the system prompt and modulating the tone or the output
so that it's in German now that we've got the basics working let's have a look at these other dependencies and see how we can leverage those in our AI workflows so memory gives your AI agents state fness so the way a chat model works today is if I send in a message its job is to predict how to complete the output it's a function right I send in some text it will output some text it does not remember memory in context and when you use tools for example like chat gbt it's actually using a memory
functionality under the hood and remembering your messages together and then when it sends it to their jpt model it sends a stack of those messages so that it has the extra context that's what n atn's memory feature does as well here so you don't have to m manage that stack to highlight this let's try to have it remember some stuff between these executions and we'll see that it's not able to do that so let's change this back to English so I can communicate with it and let's give it something that it has to remember so
the number is three what is the number so it should reply with three okay the number is three now if I ask what is the number it's not going to know what I'm talking about because it does not know that this happened so let's change that by adding some memory I'll click the plus here we have a few different memory options so the easiest one as it's labeled here is the window buffer memory let's click the window buffer memory now how memory works at a high level is it expects a session ID every time you
reference the same session ID it finds that session and it'll append that latest message to that session creating basically an array of messages or a stack of messages the way it works in N ATN if you're using the chat trigger is there's really nothing to set up by default it's taking the session ID from the prev node where Auto piping in that session ID you could however Define that below but let's keep it on the default now and then the default context window length is five this means it's only going to get the last five
messages which for now is fine but if you're doing a chat based use case you're probably going to want to bump that up so now that we've attached some memory let's try that example again so let's say the number is five what is the number we can see we've run the memory as well so we check that okay okay the number is five great what is the number the number is five so now that we've run something with multiple dependencies we can see how the logs could be useful here here we see the order in
which the AI called its various dependencies so we can see at first it used the window buffer memory and added this message to the buffer under the hood it also checked if if there was any messages previously there wasn't because we had just added it right and then made the call to the open aai chat model and then to the window buffer again now we have an AI agent that can respond to your task using a chat model and it has statefulness which means it can remember a chat session and multiple messages so has that
context as well let's say we've tested my AI agent and it's ready to go admittedly this is not the most sophisticated example but let's say we built it out and we want to publish it we want to use it in prod so to speak what we could is double click on the chat trigger and make the chat publicly available and we can copy this URL and then what we're going to want to do is save the workflow and activate it this is going to make sure that that public URL we just copied is live so
now if we go to a new tab and enter that URL we can see this chat UI which is being served by my edn workflow and so here I could ask what is the time right now and because we're feeding that in as context we can see it's giving the right time now there's some defaults in here like the text and whatnot those can be customized in the chat trigger you can also attach some authentication at top so you can ask someone to have to enter a username and password before they use this form which
is super useful if your AI agent can interact with your proprietary systems which we'll cover in the next video when we get into the tools that the AI agents can do and Ned's chat trigger can also handle various options like allowing the US to upload files and whatnot so there's options for all those sorts of things as well these aren't complicated settings and there's documentation for this stuff as well so just go check that out if you're curious on on any of those details now that the workflow is active if we go over to the
executions tab here we can see the various executions of this workflow if there's a beaker icon next to it it means it was a test execution which means that we ran it in this editor otherwise if there isn't one it means it was a production execution which in this case means it was run through this uh web U right here there's a super useful feature that I'm going to call out when you're inspecting your production runs and you want to perhaps iterate on your AI agent or troubleshoot an edge case when you have your execution
open this is true for an error execution or successful execution is you can copy the data of this workflow run to the editor so if I copy to editor I can now open up my AI agent and I can see the state of its data when it ran I can see the logs from when it ran as well it's useful for troubleshooting and iterating once you published your AI agent that's all for part one of the intro to AI agents tutorial Series in part two we'll explore setting up tools for your AI agents so they
can interact with apps Services databases and even perform mathematical operations this series is part of my work at NN Studio where we build Ai and automation projects in public so make sure to subscribe to catch part two of the series when it comes out and for updates on future projects I'm Max catch you in part two [Music] now
Related Videos
Building AI Agents: AI App Tools and Guardrails explained, Gmail Draft Agent [Part 2]
17:25
Building AI Agents: AI App Tools and Guard...
n8n
28,969 views
8 Insane AI Agent Use Cases in N8N! (automate anything)
40:53
8 Insane AI Agent Use Cases in N8N! (autom...
AI Foundations
167,824 views
MCP Explained in 15 Mins | Build Your Own Model Context Protocol Server Using Zapier & Cursor
15:37
MCP Explained in 15 Mins | Build Your Own ...
Helena Liu
35,185 views
n8n Masterclass: Build AI Agents & Automate Workflows (Beginner to Pro)
1:31:43
n8n Masterclass: Build AI Agents & Automat...
Nate Herk | AI Automation
268,434 views
Master Any AI in 2025: Ultimate Comparison Guide (from ChatGPT to Gemini)
26:18
Master Any AI in 2025: Ultimate Comparison...
AI Master
16,682 views
Full Course (Lessons 1-10) AI Agents for Beginners
1:04:28
Full Course (Lessons 1-10) AI Agents for B...
Microsoft Developer
116,392 views
How To Use AI Automation To Do ALL Your Work - Full n8n Course for Beginners
2:08:44
How To Use AI Automation To Do ALL Your Wo...
Tyler AI
11,299 views
AI Agents: What You ACTUALLY Need to Know
14:54
AI Agents: What You ACTUALLY Need to Know
Grace Leung
17,621 views
Master n8n in 2 Hours: Complete Beginner’s Guide for 2025
2:10:36
Master n8n in 2 Hours: Complete Beginner’s...
Jono Catliff
160,912 views
n8n Ai Agent: Build a Self Learning Agent! (n8n tutorial)
27:29
n8n Ai Agent: Build a Self Learning Agent!...
Productive Dude
104,628 views
Building and evaluating AI Agents — Sayash Kapoor, AI Snake Oil
20:00
Building and evaluating AI Agents — Sayash...
AI Engineer
78,107 views
How to Build & Sell AI Agents: Ultimate Beginner’s Guide
3:50:40
How to Build & Sell AI Agents: Ultimate Be...
Liam Ottley
543,435 views
AI Agents Fundamentals In 21 Minutes
21:27
AI Agents Fundamentals In 21 Minutes
Tina Huang
919,284 views
Build a Complete AI SaaS with zero code (Lovable + N8N)
1:28:51
Build a Complete AI SaaS with zero code (L...
Simon Scrapes | AI Agents & Automation
12,276 views
How Nate Herk's AI Agent Is Revolutionizing Lead Response Times [With Human In The Loop]
29:15
How Nate Herk's AI Agent Is Revolutionizin...
n8n
18,341 views
How To Build No-Code AI Agents (For Beginners)
30:37
How To Build No-Code AI Agents (For Beginn...
Brendan Jowett
3,186 views
N8N FULL COURSE 6 HOURS (Build & Sell AI Automations + Agents)
5:58:32
N8N FULL COURSE 6 HOURS (Build & Sell AI A...
Nick Saraev
93,020 views
n8n Tutorial: 37 Tips and Tricks (n8n Masterclass)
1:27:43
n8n Tutorial: 37 Tips and Tricks (n8n Mast...
Productive Dude
30,976 views
10 Mind-Blowing ChatGPT Features You’re Not Using (Yet!)
14:16
10 Mind-Blowing ChatGPT Features You’re No...
Kevin Stratvert
326,855 views
N8N Foundational Concepts (AI Agents, JSON, Logic)
2:04:52
N8N Foundational Concepts (AI Agents, JSON...
Nick Saraev
22,681 views
Copyright © 2025. Made with ♥ in London by YTScribe.com