hi I'm Max and welcome to part two of the building AI agents tutorial Series in this video we're going to look at how to set up app AI tools for our AI agents to use I'm also going to show how you can add guard rails to that tool uses so you can constrain what your AI agents can and cannot manipulate and we'll do this all by building a Gmail draft writing assistant so that way you can see these key Concepts at play in a real use case in part one we covered some foundational aspects of
building AI agents in Ann so if you're watching this video I assume you understand how to use idence chat trigger how to add an AI agent how to add chat models modify its system messages add memory and publish the workflow if you don't know what I'm talking about go ahead and watch part one and then come back here let's quickly Define what an AI agent tool is and then we'll flip to ended end and build one out think of an AI agent tool as a function that the AI agent can call on that helps it
complete a task that it cannot do itself tool allow AI agents to interact with other systems gather data or take actions in those systems so they make an AI agent far more capable and useful in all of my agentic buildings so far the biggest lever for success in your use case is the context that you can provide to your AI agent and since tools are a real-time way to fetch context they're super super powerful the nice thing is they're pretty easy to work with let's jump to the work for canvas and set up an AI
agent that can interact with an app through an AI tool in part one we focused on a chat AI agent let's do something a little bit different with this Gmail AI agent that's going to write drafts for us so the way this automation is going to work is each time I receive a new email in Gmail I want to ingest the contents of that email and any metadata that's associated with it I want to pipe that into my AI agent I want them to generate a draft and I want to post that draft which means
the first step is I want this to run each time I'm getting a new email into my inbox so that's the trigger that we want to add so since that's an app event let's go in on app event here and let's search for email we'll click on that and we'll add the on message receive trigger so the first thing I'm going to want to do is create a credential let's create a new one real quick the nice thing is because I'm using init and Cloud right now I can have this nice click to connect experience
so we're connected now if you are self-hosting Eden you are going to have to do some of the O steps the screen is going to be a little bit more complicated you can use my coupon code Max 50 you can get 12 months off and it in Cloud now that my credentials connected let's close that the Gmail trigger in n8n is a pole based trigger this means under the hood it's doing sort of a get many operation and doing the diff under the hood so it's polling every minute for that we could poll more or
less often every minute sounds good and we want this trigger to run when a message is received so that's a correct event if you ever see a simplified toggle in an nadn node this controls how verbose the output data is so some apis can be clunky and have a lot of nested structures and stuff so for popular nodes n8n maintains a simplest schema for those we do want to arm our agent with the most context possible so we want the full raw email so we're going to turn that off here and what we can do
to really get data in our worklow so we can build out the rest of our flow is fetch test event so what this has done is simulated what happens when this workflow runs and it's fetched the most recent event so this email didn't just come in but the schema of this email and if I flip the schema view here the schema here will be the same when it does run so we can use this to map Downstream and build out the rest of our workflow now that we've got some test data in my Gmail trigger
let's actually go pin this so I can click this pin data button here and what this will do is persist this data even if I clear my workflow data and that's also true between sessions right so if I save this workflow come back to this data will be pinned here and in the Gmail API it's not as important but for some apis they're heavily rate limited or you have to pay for them it's a great strategy when you're testing so you know unnecessarily using your quotas so we've gotten our email so the next part that
we need to do is pipe that into our AI agent so that they could write an email draft for us so let's do that let's search for our AI agent and let's add that so the Gmail trigger is going to be sending its payload of data this one item of data the thing is is it's an object with lots of unnecessary data perhaps even personal data that we don't necessarily want to send to our llm in part one you saw with a chat trigger we're just Auto feeding that data into the AI agent in this
case what we can do is instead of using the default prompt Source we can open this drop down and and click Define below here so what I can do is using the schem of view on the left here I can actually drag and drop variables into here that we want to populate at runtime so let's build out that user message that we want our AI agent to to work on so this is going to define the actual task that we want the AI agent to do here we're actually going to combine some static text along
with the the email content itself so to save everyone a little bit of time we're going to use this pre-written message that I have here we'll paste that in here it's saying write a reply to the following email then add it as a draft to the email thread now let's flip this to an expression this is going to send every time this AI agent runs every time we receive a new email and if we collapse the headers here let's find the actual email body here so we have the text is HTML so let's say we
call this message and let's drag and drop that there so we can see it's now rendering that dynamically in here let's also maybe add the subject that's probably some relevant context as well and we probably want the from email because it's going to have their name and the email so it's also relevant and so we've got the from value here but we can see Gmail supplies us with a sort of a nicer one with a bit more Rich context it's got the name and the email and our AI agent should be able to extract that
easily and pass that if it needs to we got from subject message that should be good to go now one thing we could do since our message here has uh HTML or could have HTML we're going to actually wrap all this in some tags so we're going to use pseudo XML so let's say email and at the end we'll do close email and so this creates a bit of a wrapper some Syntax for the AI agent to understand we're saying hey here's the following email and then right after that there's also a callon we see
this very clear delineator that in here is the email so this will help especially if in the message we've got some syntax or some h HML that creates structure and styling and this way we'll have a more consistent result across so now that we've got our user message set let's go back to the canvas and add a chat model in this case let's use CA 3.5 but again you should be able to use any larger more sophisticated model should work here since we're going to be setting up tools some of the really small models the
1B self-hosted models might not work now there's nothing we really need to tweak in here so if you're curious on more details on chat models do check out part one now that we've got a chat model and our user message we could actually test this so let's run it and what we're going to see is we haven't really defined where it should create these drafts we haven't done it explicit we haven't given it any tools to create the draft all we've piped in is the context of the message and told it to write a reply
and so it's done that it's written a reply right now it's just outputting this draft but I don't actually have a draft in my inbox yet so let's give our AI agent the ability to create that draft on our behalf and let's make sure that it can only create drafts it can't delete emails bulk search everything else because this use case is just for creating drafts so let's give it exactly the permissions it needs to and nothing else but to do that I'll go back to the work for canvas let's click this plus here to
add a tool this is going to open up the tool section of our nodes panel and since we do have an app tool for Gmail let's click the Gmail tool to add it to our canvas it's automatically connected to my AI agent let's double click on the Gmail tool to set it up now let's rename it first so this is going to be create draft and so for the first couple parameters these are already preset the tool description this is the description being created by this tool for the AI agent to understand what it is
we want that to be done automatically and then the resource that we'd like to operate on is not a message a draft is a different entity in Gmail so we want to operate on a draft and we want to be creating draft so we'll keep that to create we're not aggregating drafts or or deleting or anything so we just need this action here and the subject line this is one of the first parameters in this tool that we want our AI agent to decide what to populate with it so we want to give the AI
agent context in terms of levels of abstraction the tool description is telling the AI agent hey you can use this tool to create drafts in Gmail that's essentially the information that this is going to send and then we can decide to expose one or many of these other parameters that we want the AI to manipulate any that we don't explicitly expose to the AI or give it permission to modify it won't be able to so that's a great way that that we're adding guard rails to our AI agent tools so you're getting more predictable results
in your systems obviously your Gmail is not somewhere where you want an AI agent to run a mck and so to tell our AI agent that we want it to be able to manipulate the subject field we can flip this to an expression and in here we'll go with open parenthesis to open up the auto complete and we want the from AI method so if I hit dollar sign from AI it'll filter by that there is some nice inline documentation on the different arguments you need to use in especially optional ones so let's go set
that up I'll click to add it and the first thing it expects is a key so this is the name of the variable from the ai's perspective let's call it subject so far when I'm building my AI agent tools I tend to just pick the exact same parameter name that it's labeled and then if that's not working or it's not descriptive enough I add on top of that and that's definitely something I can recommend generally when you're building out your AI tools less is more test it with less add more when you need to when
you identify edge cases where it's not performing as you'd like great so we've got the subject here now the email type let's stick with a text email for now although chat models could definitely handle formatting in HTML as well and for the message text we're also going to flip this to expression and also use that from AI method so from Ai and what we want to do here is again give it a name message and here we could use some of these optional arguments so we can Define the key so this is the name of
the variable that we want our AI to inject at runtime we can also give it a description this is super useful if for example would expect some sort of iso timestamp we can Define it there there's also a data type that you could provide here so if it expects a bullying or a number you could Define that there and you can also give it a default value we don't really need a description in this case and some of the stylistic things like I want it to just be a plain text email and whatnot we're going
to place in the system message so we've got that all in one neat source of Truth inside our AI agent something I try to do much like an engineer might on a project I try to separate concerns so this Tool's job is to send a draft the how that draft is going to look the style of it the length all that I like to keep in my AI agent in my system prompts so we're having a nice clean separation so I could duplicate this tool and use it in another use case without it having an
opinionated approach on things like style this is going to create a draft the thing is in Gmail someone has already sent me an email if we go into our Gmail trigger we can see in here that there's this thread ID so every message in a specific email chain or an email thread is related to this ID and since we want to create that draft as a response to someone that's already emailed me we want the AI agent to post this reply to an existing thread right to an existing email so if I'm getting this email
from Ant Wilson at superbase hey ant let's definitely collaborate I wanted to post the draft here right because I'm going to open up up my email be it on my phone here see it tweak it and send it right so we want to add an option here and we want to define the thread ID now the thing is each time this workflow runs we know that in the the Gmail trigger we have the thread ID of the email that just came in that's where we want to post the draft that will always be the case
for this scenario so this isn't a good example where we want to give our AI agent control now it probably could figure it out and we could use from AI here and have our AI agent populate that but since we don't need it to Let's constrain that so we've got less degrees of freedom that could potentially go wrong in production right because again AI agents are not deterministic they're probabilistic there's a bell curve of output now with all the things that you're learning and all the tooling and prompt engineering whatnot you can tighten up that
bell curve but you're still going to want to use the right tool for the right job so in this case we're going to want to flip our input to mapping mode here we can see the data from my gmail trigger and I can drag and drop that thread ID here so now this is not something that our AI agent can control this is pre-populated at runtime now that we have told the AI to populate the subject and the message and the thread ID let's run this quickly and I always like to run it before I'm
adding a more sophisticated system prompt so in my AI agent if I add my system message we can see all it's saying right now is you are a helpful assistant there's nothing about how it should write there's nothing about ways of working in which order it should do things so let's run this and get a baseline Let's test the workflow since we've pinned the Gmail trigger it's going to reuse that data and send it into our AI agent okay exciting stuff it's created the draft in there let's open up my AI agent we've got an
output great the draft have been successfully created let's look in the logs we did use the create draft tool we got a successful response let's check in my Gmail let's refresh okay we've got a draft dear ant thanks for the heads up d d d d da okay so some things to improve firstly I don't really write like this secondly I have a name uh etc etc so this is where the system prompt comes in right the system prompt is where we Define the how it should complete the task the style the constraints so let's
add my sister system prompt now since today is about tools and not system prompt writing we're going to copy one in that I wrote earlier much like in your favorite cooking show but I will quickly voice it over so you understand what's happening in there so let's open it up and in the system message let's actually flip this to an expression and open this up here so let's replace this and let's break it down the roll this is something I'm almost always including is a roll and again it's prefix in a title you are an
AI assistant specialized in replying to incoming emails to Max's Gmail email inbox capabilities and limitations you cannot send emails you can send email drafts some stuff on tone casual modern professional the important thing here is you should sound like Max here are some examples of Max's voice I took two emails from my inbox I wrapped each one in some simple example tags and again you're seeing what I'm doing is I said here are examples and then that word is what I'm using in this tag that wraps that thing that I'm feeding in his context so
there's less ambiguity so I've got one email where I'm excited about something I'm down to collaborate I want to do it and then I've got one where I said hey I don't really have bandwidth for that a much shorter kind of email I only have two examples here now these LM models have large context Windows you could easily feed in 510 and probably get better results but let's see how fitting in these two examples with this little bit of prompting is improving this so let me go in here and delete this draft and let's just
run this again it ran let's have a quick look again here let's look in the logs and let's check my Gmail real quick let's give this a refresh and in here hey Aunt thanks for the heads up okay it's shorter it's feeling more like me I do use ps's for sure if you've ever received an email from me it's got my name I am definitely a cheerer more than a best regards are you may have noticed that this tool allows users to create a draft it's a single action if you think about a lot of
different things that you're going to want your AI agents to do it's usually going to need multiple tools to get that done take the classic case of booking a calendar appointment in let's say your Google calendar it needs to first know your availability so it needs to get your current events Place those to find the gaps or maybe you have an API that can tell you where the gaps are or the availabilities are but it has to go fetch that first and then afterwards it can create an event in a slot that is available so
that would require two separate tools that's all for part two now we only covered app AI tools and it then has a few different types of other tools there's the HTTP request tool that lets you interact with any arbitary API endpoint and you can also call another entire workflow and use that as a tool so that lets you access all the different apps and Integrations that nedin has now I didn't include them in this video because those features are getting an overhaul right now so as soon as those drop I'll do another part going in
depth on those and I will be doing a part three of this series but I'm not sure what to do yet so drop a comment on what you want to see thanks so much for watching I really hope you found this video valuable if you did part three is coming so make sure to subscribe to catch that this video is part of my work at the Ed end Studio where I build various Ai and automation projects in public that's a wrap