in today's video I'm going to show you guys how to build an AI agent that has the ability to store long-term memory often times when you're chatting with an AI agent it can keep the context of the conversation when you're in a single chat but what if you had a chat with it 10 days ago and now you want to reference back to some of the insights that it gained from that conversation that's exactly what I'm going to show you how to do in today's video and I'll show you how to build this workflow inside
of n8n now this workflow basically just uses a telegram trigger which you can see over here this is where I'm triggering the agent by sending either a voice note or a text note to it and every time that I send a chat through it's querying this air table database right here that has information about me it can even add new memories in real time using this memory tool here so that it's actually self-improving and I'm even going to show you guys the secret sauce I'm going to give you the system prompt for this AI agent
if you stick around until that portion of the video but before I kick this off let's show you how this agent works so I'm going to go ahead and send it a voice note hey there what's your name now this automation has triggered and it's going to reference this row of the database and as you can see it says my name is super wizbot perfect it grabbed that from right here I want you to store a memory in your long-term memory that reminds you that you were part of an amazing YouTube video where I shared
how to build you super wizbot and if you watch down here it should add that memory momentarily just a quick Refresh on my air table database and as you can see it added in that memory so Carter was part of an amazing YouTube video where he shared how to build super wizbot now the exciting thing about this agent is you can actually ask it to improve its memory so I can just say hey what are some things that you need to know about me and this overall system in order to improve your memory the first
question that it asks here are what are some specific personal or professional goals you have for this year then it says are there particular Hobbies or interests that you're currently pursuing or would like to explore more deeply what topics or themes resonate most with you in your reading and learning Beyond cryptocurrencies and AI which it got from right here cryptocurrencies and AI all right so it asked me some questions here so that it can improve its future context on me so I'm just going to give it a quick reply some personal goals that I have
for this year are to get more fit and to get better sleep I want to aim for 7 to 8 hours per night and a professional goal of mine is to reach 100,000 subscribers on YouTube some Hobbies or interests that I'm exploring more deeply include collecting records and learning more about deeper disciplines like engineering I also am interested in scaling up my small town and making it more of a prominent town in the area and just improving the life quality for people around me so I sent that off and it should go now and add
some memories here to the air table and then it'll come back with a reply so as you can see it's adding all of these memories right here it's adding my professional goal for the year uh some themes that I'm resonating with when learning it's also sharing some hobbies that I'm exploring some of my other goals and interests and it covered my health goals right here particularly that I want to improve my sleep so now even after this chat session is complete it's going to have access to the past 50 memories in my air table database
and it's going to sort them by the date that they were created so look guys I'm going to show you how to build this entire thing today for free on YouTube it's my gift to you because I want you guys to be able to implement this for your agents but if you want to take the community aspect of your learning up a notch and you want to network with people who are fired up about Ai and maybe you even want to share some of your own insights or maybe you just want to learn this stuff
in a more structured way rather than jumping around YouTube videos If any of those cases sound like yourself then I highly recommend investing in the AI foundations community at AI foundations we do regular weekly Zoom calls for both support and just general Community interaction and networking and we even just implemented a new course in our classroom on how to master nadn this tool is going to be vital in the future for agent Builders so if this group sounds interesting to you if it sounds like a place that you'd want to hang out then I highly
recommend using the link in the top pinned comment or the description below to join AI foundations today all right now let's go ahead and dive into building this nadn agent to kick things off we're going to head over to nadn and we're going to create a new workflow I'll just create it in personal and we'll kick things off by adding our telegram trigger so I'll click this plus button in the middle and I'll type in telegram I'll click on Telegram and we want to go with on message so this is the trigger for on message
so I'll hit on message if you don't have your credential in here then you can just click on create cre credential here but I'm going to click the drop down and hit create new credential and then it brings me to this screen where it needs an access token to get this access token I'm going to open up telegram either on my computer or on my phone and on the computer you can just click up in this corner here create a new channel I'll just call it test and hit create and I'll make it a private
Channel and hit save I'll skip adding people for now and the first message that I'm going to type in here is at botfa I'll hit enter next I'll click on the link for the bot father and I'm going to type in slstart and after sending off SL start it's going to give me a list of commands here so I'll do Slash new bot and I'll hit enter and now it's asking me what I want to call the bot so I need to give my bot a name I'm going to call it super memory bot and
I'll hit enter and now it needs a handle for the bot so for this one I'm going to go supercore memory uncore and I'll end it with bot and hit enter ah this username is already taken so let's try super memory is one wordbot that one's taken too let's try ultracore memorybot all right that one worked so it says done congratulations on your new bot and it gives me this access token here so if I click on this access token it'll copy it to my clipboard we can come back to our naden account and paste
that token right here and then hit save now I'm going to close this window and I'll hit back to Canvas so now we have our telegram trigger so when we send a message it's going to come through right here now we need to wait for this bot to access access the ongoing memory so for that we're going to be using air table so I'll click plus right here I'll type in air table and I'll click on the air table icon and then what we want to look for is search records click on search records and
I'm going to create a new credential for my air table and I'm going to use the access token to get the access token I can just come over to my air table account get logged in and then I can click on my account in the upper right hand corner go to Builder Hub and from here I can hit create new token I'll give my token a name of test and then under access I'm going to hit add a base and I'm going to select all current and future bases in all current and future workspaces and
just for testing purposes I'm going to add all of these Scopes you can decide to limit these if you want to but just to get this through and to make it work I want to give it everything so that the agent doesn't run into any issues when it's accessing this token and accessing my workspaces and this is basically just all the permissions that the agent has has so here C data and Records this is probably the only one I need for this step but later I'm going to need to write to the records as well
and I just want to leave it wide open for the agent especially in this testing ground and then later I can come back and refine this for security purposes but I'm going to hit create token and I'm going to give it this token that allows nen to access my air table account so I'll copy it and hit done then I'll paste that access token here and hit save it should say connection tested successfully so now I'll click close this popup window make sure your resource is set to record your operation is set to search and
we want to select a base but right now we don't have any bases for the agent to access so I'll head over to air table and hit the blue create button in the bottom left hand corner I'll select a workspace I'm just going to go with my first workspace and I'm going to hit start from scratch then we need to give the base a name I'm just going to call it agent memory and I'll call the table here memory I'll hit save and for the name column here I'm going to rename it to memory and
I'm going to make this a long text in case the memory gets a little bit longer then the single text can handle then I'll hit save I'm going to delete these other default fields that it gave us and I'll delete these initial records and in the first record I'm just going to give it a test memory so that it actually has something it can pull from this database for testing purposes so I'm just going to say users name is Carter and that's pretty much all you need for the memory database and on the base you're
going to make sure it says from list you're going to click on the dropdown and I'll type in agent and I should see my agent memory base here I'll click on that and for the table we're just going to make sure it's on from list I'll click the drop down and we're going to click on that memory table that we created now when you're passing memories to your agent you don't want to return all because if you were returning everything the responses would get too long as you add up your memories so you can either
set a filter for certain memories that you want it to bring into context for your agent or as a basic way of managing the length of the memories you can just turn return all off and then change the limit to make sure that it's not going to be too many memories that it pulls in I'll just change the limit to 50 and then if this makes my agent bug out at a certain point you can lower this a little bit if you want to but you can just play around with the limit to get the
right amount of memories in there and to make sure that your agent isn't dying out because you're overwhelming it with information and now I'll go back to Canvas and the next step is to aggregate this data because it's going to send it through as individual items but we want to turn it into one array that has all of the memories in it so to do that there's a handy node that you can set up in nadn and it's the aggregate node so I'll just hit the plus button here and I'm going to type in Aggregate
and then I'll see it here it's just this little tree this little orange tree so I'll click on that I'll make sure aggregate is on individual fields and then for the input field name I'm going to enter the name of the field for our memories and that's right here in our air table it's just called memory so I'm going to use that to pull it through so we'll type in memory and I'll go back to Canvas now the pathway for this chat needs to go straight to our AI agent it can't go through these other
nodes here so what we need to do is we need to create a merge node so I'm going to hit the plus button right here and I'm going to type into the search nodes merge and I should see this little blue tree here I can click on that one and I'm going to change the mode to combine and then for Combined by we'll change that to all possible combinations and I'll go back to Canvas now we can drag our telegram trigger to go through and be input one or input two it doesn't really matter how
you do this it would actually probably be better though if this was input one and then this one was input two there we go so now it's merging those two pieces of information together when that telegram message is sent it's going to go grab all of the memories that are currently in the air table it's going to merge them into a single item using the AG Ator and then it's going to take the text that I sent through Telegram and it's going to put that into a single item so then we can pass this special
packet here over to our AI agent this is the fun part this is where we're going to add in our AI agent so I'll hit the plus button right here I'll click on Advanced Ai and I'll click on AI agent now I'll go back to Canvas and I need to send some test data from our telegram trigger this will allow me to map the chat to the AI agent so I'm going to copy the name of my bot and then I'm just going to type in at I'll paste that bot in and then hit enter
and now it gives me this hyperlink for the bot so I'll click on it and I'll hit start and now I'm able to chat with my bot now for now what we're going to do is we're going to delete this connection to our AI agent and we'll bring it back in in a moment and I'm going to right click and deactivate my AI agent for now so that it doesn't trigger an error then I'll hit test workflow and now telegram is waiting for me to send a message so I'm going to send a message to
that agent that we've connected over here I'll just say this is a test and I'll hit send all right and what you should see is you should see the items move through here so it pulled our air table memories and it also pulled through the chat that we sent to telegram right here then it merged those into one item that we can pass to our agent so now I can connect this back to the AI agent I can rightclick it and it activate and then we can click into that AI agent and I can pass
in all of the variabl that this agent needs because I now have them here so I see our memories right here and since there's only one right now it's just showing one but they would list out right here and I could just drag this memory in and it would have access to all of those but the first and most important thing to map in is this text right here this is the actual conversation text that I sent through over here so I said this is a test and as you can see it's passing that variable
right here so we'll change this prompt source to Define below and then all I have to do is is click and drag that text into the text field over here and now that agent should be able to receive my messages next we need to add the system prompt for this agent so that it knows how it works so I'll hit add option and I'm going to click on system message and it starts by default just saying you are a helpful assistant but we're actually going to delete that and the first line of the system message
that I'm going to give it is the current time and day I need to change this to expression here and once I change that to expression this expression right here just the curly brackets around the money sign now is going to pull back in the date and time so that the agent always has the current date and time next I'll enter down just a couple lines and I'm going to paste this next section so this next one says you are a helpful assistant with the ability to add and read memories about the user simple as
that I'll enter down again and for this next part I'm going to tell it how to add those memories so let's go ahead and open this up in a full window and I'm going to paste this next of text here so I just say to add a new memory or insight to the top of your list of recent memories you can use the memory tool and I'm telling it the name of the tool so we haven't created that yet but we're going to all right and it says which allows you to add short one sentence
insights about the user to your memory for the future in order to help you customize your response output we'll go down to the next line and I'm going to paste in this next bit of information so this next line is just more generalized instructions for how I want it to add those memories because if I don't give it instructions like this it's just going to go crazy adding memories and it's not going to be relevant to the way I want this agent to work and the way that you probably want this agent to work so
now I just say you don't have to always customize based on the memories because I don't want it going over the top right if it has these memories I want it to be able to access them when they're handy and when they're useful but I don't want it just like implementing the memories like if I say I have a dog I don't want it to Implement that I have a dog into all of the responses I only want it to bring that up when it's relevant so I said but if there's a good reason to
customize your response you can use the memories below to do so these are memories that have been added by you if you need to learn more about the user for the future you can ask questions in order to take note of their preferences now for this next line I'm going to say use your current memories of the user to recall past insights about them then I'll paste in this next line where we have the curly brackets around this Jon memory and it says read your most recent memories here and as you can see on the
preview of the result over here it's showing the memory that we have in our air table database that it pulled through using that search step and finally I'm going to add some last instructions here so I just say tailor your response based on the relevant memories if you find that a memory is relevant to the response a bit redundant but I like to add that here and then this one is a little bit um of my own secret sauce for how I like to get the agent to talk talk to me so I just say
always output your final response as a conversational piece rather than a list or blog post if you must make a list keep it simple and don't add too much hierarchy only share the most important notes after thinking of your response consider the tldr version or a to Long didn't read version and always give a conversational cheeky fun reply while remaining assertive helpful and not too playful and then for this last line give a Meek tone to your response I really just like how the responds when I tell it to give me a Meek tone but
I like pairing it with these other kind of keywords as well and that gives it a really good balanced response from my experience I'm going to close this window now we'll jump back to the canvas and now we need to add a chat model so I'm just going to hit the plus button right here I'll go to open AI chat model and here you need to connect your credentials for open AI so I'll just hit create new credential and now it needs an API key from open AI to get this just go to platform. open.com
create an account if you don't already have one and then click on the settings you'll go over to billing and you'll add some billing details in just $5 to $10 to test with if this is your first time and you're just trying this out and then you'll go to API Keys create a new secret key and I'll call this test I'll select a default project and hit create secret key and I'll copy my key paste and save now it should say connection tested successfully and we'll close this popup window from here you can choose your
model I like just using GPT 40 mini because it's pretty inexpensive I'll go back to Canvas now and next I want to add a memory so I'll hit the Plus on this memory right here and I'll click on window buffer memory and we'll change this to Define below I'll go to mapping and for the key we're going to want to drag in the chat ID then you can select the context window length so this is how many past inputs from the chat it's actually receiving in its context window so that it can provide you with
a better response I'll leave this on Five for now and I'll go back to Canvas and this is where the real magic happens now we're going to give this agent a tool that allows it to log to this air table database so that it's basically recursively improving its memory here add this tool we can make it so that it can add new memories so I'll click on the air table tool and first things first I'm going to add a name to this air table tool and we're just going to call it memory uncore tool and
hit rename just like I had in the system prompt you should be connected to your air table credentials here so that shouldn't be a problem tool description can be set automatically or you can set it manually and give it more instructions on how to use the tool I just set automatically and that seemed to work fine for the resource we're just going to choose record and you're going to have to change this operation from get to create then just like we did earlier when we were searching for the memories and pulling in the most recent
memories we're just going to click on this from list base drop down here and we're going to go find the agent agent memory right there and for the table we'll drop that one down I'll type in memory and I'll click on the memory table this should be set to map each column manually so I'll change this to the expression tab on my memory and I'm just going to paste in my expression but I'm just using the from Ai and then I'm putting in a name for the tool and now this from AI expression is going
to allow the agent to write its own memories right here now I'll go back to the canvas and now this is getting really close to being completed now we just need to send the message back to telegram so I'll hit the plus button right here I'll type in Telegram and I'll click on the telegram I'll search for send and you should see send a text message I'll click on that I'm going to paste in this expression that I already have here it's just calling for the telegram trigger and it's pulling in the chat ID from
that telegram trigger so that's just referring to this and it's pulling the chat ID that this outputs just like we had to put the chat ID in the window buffer memory here so if the name of your node is telegram trigger this should work and again I'm referring to this name right here telegram trigger so we'll go back into send message this one's going to be an expression as well and I'm just going to paste in this expression json. output so this is just pulling the output from the previous node so if you see a
dollar sign and it's not referencing a node like this it's just pulling it from the previous node and all it's doing is grabbing the Json objective output from this AI agent node when it's done outputting so I'll click back in now I'm going to go ahead and test out this entire workflow to do that I'll activate the workflow and hit got it I'll make sure it says saved up here and everything looks pretty good so I'll open up my super memory bot here and I'm just going to send it a message and just say what
do you know about me and let's see if it can figure out that my name is Carter all right perfect it came back and it said I know that your name is Carter I didn't tell it that it's not in its context the reason that it knew is because this air table record right here sent in that memory that I added to begin with now for the next experiment we're going to head over to air table while we still have telegram open and we can see here it says user's name is Carter but if I
wanted to add a new memory to the memory database I can just say something like I have a dog named Rees and I can say send that off and as you can see it sent a response here it says that's great to know Reese must be a wonderful companion if you want to share more about your dog or anything else I'm all ears and furthermore it added this memory in here Carter has a dog named Reese it can even add multiple memories at once so if I just said something like I live in Michigan and
I'll add another list item here my favorite Starbucks order is a caramel latte with a a double shot of espresso so we'll send that off and boom you saw that add those real time Carter's favorite Starbucks order is a carel latte with a double shot of espresso and Carter lives in Michigan and it gave me this beautiful response right here now if I wanted to make this a voice agent our community actually has a plug-in play way to actually set up this telegram voice agent so I'm just going to go to the classroom in AI
foundations I'll go into the nadn Mastery course and I'll scroll down to communication mediums and on communication mediums we show you guys how to connect to all these different platforms like slack telegram WhatsApp open web UI and it's a growing list so my brother Drake actually made a telegram Text Plus voice automation right here that allows you to plug this into any of your agents so I'm just going to go ahead and download the Json for this and then I'll just paste it straight into nadn I'll show you how simple that is so we'll download
this file and I'll just open it up in a text editor or vs code and I'll copy all of this Json here then I can head back into n8n and I can just paste it right above my automation down here and we'll drag it up and I don't need all of this right I already have an AI agent and I have a lot of these things set up but what I can do is I can just pull the important components out of here so I have my telegram trigger what I don't have is the Switcher
right here so I need to remove this and connect it to the switcher I need to delete this connection here because it created a connection uh I'll delete this too and I'll add that back in in a moment but we need edit fields and we need this edit fields to go into the merge and I'll just drag this out so it's a little less confusing here now I'll go into this telegram get file and I'll make sure that my credential is connected here I'll go back to Canvas and now you see that there's no more
warning on that one and when you get this file from telegram you need to add an open AI transcriber so I'll go open AI transcriber recording I'll go back to Canvas and now that this transcriber recording is in here I can drag that into input one as well now to get the window buffer memory updated and the input variable for the AI agent I can go into the memory of this template Automation and copy the key then I can go into my window buffer memory here and I can just paste that key in and the
reason this doesn't work is because his was named telegram trigger one mine is just named telegram trigger so I'll just delete that one out of there and now it's pulling through my session ID so that it can keep a short-term memory of the conversation I can go back to Canvas and I'm going to delete the rest of this information since we already have it and there's just one final step to get voice working with this AI agent I just need to make sure sure that the variable maps in properly because it's not pulling the text
directly from this telegram trigger anymore it's pulling it from either one of these fields to get this variable I'll make sure that this is active I'll hit save and I'll go back to my super memory bot and I'm just going to send through a voice note hey there this is just a test voice note to map the variables properly so now it's going to send that voice note through it's going to go down this voice route it'll transcribe that recording into text and then it'll pass it into the merge here so now that execution should
have gone off in the background so I can go to executions and I'll see that there's an execution here that has an error now the reason that it has an error is because you can see that it went down that voice route here and it went into the merge but once it got to the agent the agent didn't understand this response that was coming in because it no longer had the input field from this telegram trigger it went down this voice route and it's a different input field now so I'm going to hit debug in
editor and this is going to pull me back to the editor with the information passed through I can open up my AI agent now and instead of this Json message text we're just going to remove that text and I'm going to drag in this new text property now if I go back to Canvas and I hit save and we'll go into our execution here and I'm going to unpin it and hit back to Canvas once more and now we'll send it off a new test hey there what do you know about me and as you
can see it came back with the proper response if I go into executions on nadn you can see that it succeeded here so if I click on that succeeded one you can see how that response played out here I hope you guys enjoyed building this agent that has long-term memory using air table and nadn if you want to improve your knowledge around AI agents or just the basics of artificial intelligence and surround yourself with the community of like-minded individuals then again I recommend you join AI foundations I hope to see you inside but