I automated my Gmail Inbox with AI Agents (#crewai tutorial)

In this tutorial, Tony Kipkemboi demonstrates how to build an AI‑driven workflow that cleans, categorizes, labels, drafts replies, notifies via Slack, and empties the trash of a Gmail inbox using CrewAI. He walks through the overall architecture (terminal UI, main entry point, crew orchestration), the five specialized agents (email categorizer, organizer, response generator, Slack notifier, email cleaner), and the supporting tools (IMAP email fetcher, date utility, Gmail and Slack APIs). He explains the pre‑kickoff function that fetches a user‑specified number of unread emails, stores them locally, and computes email age. Each agent receives a clear role, goal, backstory, and task description, and uses tools such as read‑file, Gmail label/apply, draft creation, and Slack webhook. The categorizer assigns categories (newsletter, promotion, personal, YouTube, etc.) and priority levels (high, medium, low) based on custom rules, while the organizer applies Gmail labels and stars. The response generator drafts replies for high‑priority emails (saved to drafts when storage permits). The notifier sends concise Slack alerts for urgent items. The cleaner deletes low‑priority or aged emails and empties the trash. Tony also discusses model selection (OpenAI GPT‑4‑mini, Gemini, Llama‑3 via Groq, Ollama) and the trade‑offs of privacy versus performance. He shows how to obtain Gmail app passwords and Slack webhooks, run the project locally, and optionally deploy on CrewAI Enterprise with a cron job. Finally, he mentions extending the system with vector‑store search, a chat UI, or integrating with the MCP protocol for tool‑driven interactions, and invites viewers to contribute or request UI enhancements.
Chapters
Hi there. So this is my email address, at least one of my email addresses, but this one specifically, I've neglected this email address for a very long time. And as you can see here, it says my storage is full. And because probably I received a lot of emails that I never opened, that I never deleted. And also the other thing too, I share storage with my Google Photos. So my Google Photos probably is another culprit where I need to clean up, but you can see that I can't send any emails or receive any emails at all. And that email sent to me will bounce back to the sender and can never be recovered later. So those are gone. So if somebody tried reaching out to me with some sponsorship or something of the sort, that's just gone. I'm never going to get that. And the last time I was actually able to receive an email was August 28th, 2024. So that was last year. That's a long time. So I'm going to try to do something about it. The first place I'm going to start with is cleaning up a bunch of these emails on my Gmail account and also probably Google Photos later. But for now, I just want to experiment with something. I want to use AI agents to help me clean up this inbox. I want them to categorize it and do some extra stuff, even generating drafts for some of the emails that are important. But the big thing, I wanted to read through the emails, classify them, add labels to them, and do some extra stuff, and even delete some of the stuff that I don't need on there. So let's get started. As always, we start with going over the architecture diagram of the project. So here I have the user interface. This will be your terminal for this case. Once you run the project, courier run, it will ask you for user input. It will say, how many emails do you want to process? And you maybe say 10, 20, depending on the context window of the LLM you're using. I would recommend starting with probably a smaller, small section. I've been doing 5 at a time, but we'll try some bigger number in this demo. So once you enter that, it will go to the main function, which is where the entry point for the entire project. It will take in the number that you put in. Then it will trigger the crew to start running, and the entire crew is in crew.py folder. And this is just kind of like core in there, and it will orchestrate the agents. And now let's look at the agents that I have here, and I'm going to zoom in a little bit so you can see the agents here. So here I have 1, 2, 3, 4, 5— 5 agents. So first one is an email categorizer agent, and what I'm doing here as well, and I'll show you in the code, so fetching emails from I'm actually using IMAP and I'll talk a bit about IMAP here in a second, fetching all the emails. And I'm just using regular function. I don't need an agent for that. That's just going to kill tokens for nothing. So I'm using— there's a feature in CrewAI called before kickoff and after kickoff. So before kickoff is what I'm going to use. So before you kick off the entire crew, do something like you have a function that runs before it kicks off the crew. So you can use that for so many reasons, like even validating the input that the user is putting in. But for this specific case, I'm going to use a function that actually fetches those emails and then it passes, it saves it to a folder. And then the next agent, which is the email organizer, will be able to read in using the read file tool from CrewAI, read in all that content and start categorizing it based on my categorization criteria there. And if you're going to run this, I really kind of fine-tune this for my own inbox based on what I'm seeing in my inbox. It's kind of like the emails that I think are important to me. what should be deleted. So I would say starting with the organizer, we'll just organize it into buckets. So I'm going to use labels and I'm going to show you how that works. So each one of these has a task. So you see this is the agent on the yellow and then the white section is just a task for that agent. That's what we are doing here. And it's going to run in a sequence. So the organizer will start with categorizer, will start and then we'll organize and then we have a response generator. So if there's emails there that meet the criteria that I've set, which is high-value emails or medium, I would say generate a draft email response to that email. Email and save it in a drafts folder so I can review it later. And then I also have Slack Notifier. And I added that there specifically more of a specialization to me. I use Slack more often than I actually check my emails. And mostly because Slack, I use it for work. So I get more notifications there even on my phone. You could add, you could swap that for something like Telegram, for example, or if you have WhatsApp, you could swap it for that. And that's just basically for me to, I wanted to notify me of the high-value emails, especially just Any email marked as high, that means it's important that I probably should respond in like the first 24 hours. I want it to notify me and tell me, hey, this is happening, check your email. So that's what I added in there for that. And then I have the email cleaner. Essentially what it will do, first of all, it will just look at the emails, figure out which one it wants to delete. And then once it deletes that, it figures out which ones to delete, it deletes them. And then also at the end, it just kind of goes and cleans the trash folder. So just Get rid of the trash folder to create more space. Because if I'm deleting it, then why do I just need to keep it in a trash folder? Just empty the trash, right? Same thing here. So we have tools. So these agents will need tools to get their job done, right? It's just like an employee. I like to think of each one of this one as an employee. So each of the yellow agency right there or the agents right there, all of them are— think of them as an employee. Email organizer could be a title for somebody at a company, right? And then you have email organizer. Just think of them as individuals that work. And then the tasks here are basically kind of like their job descriptions. What did it— it says exactly what they need to do and how they need to do it and what's kind of the expected outcome out of them. That's exactly what it does down there. So each of these agents needs a tool to kind of get their job done. For example, there's a date tool there that I have, and one of the agents is going to use the date tool to figure out, okay, what is the date of the email and how do I categorize that so that we can calculate the age of the emails? Because I have an age calculation there where I'm like, If an email is older than this time period, just delete it because I don't need it. And then there's also a Slack tool, which is Slack Notifier, that is going to use it to send notifications to my Slack. And those are the external services right here. And then there's a Gmail tool, which actually pulls the emails from Gmail, categorizes, does all the Gmail actions that need to be done there. And it uses the IMAP protocol. And I'm going to talk a bit about this on the next section here. So if you go to my GitHub account here, I do have this repo that that I'm going to share in the description here for you. And this is where all the code is hosted for this project. So if you look here, I've already kind of added everything that you would need here to get started. So if we look at the features for this app, the first one, it does email categorization. So it automatically categorizes your email into, you know, specific types. So there's newsletters, promotions, personals, like so many other categories you can add. So if you're trying to go kind of improve on this project, look at your inbox, figure out what emails you think buckets you would want to put them in, in terms of labels. And then there's priority assignments. I want to assign priority to each of these emails, right? And to be clear, these are mostly— it's going to go over the unread emails. The read emails, it won't go through them. This is just mostly unread. So it will assign a priority as high, medium, or low. And I chose those 3 because it's just easy. I don't want to overcomplicate stuff. And then based on the content and also the sender with more of like strict classification rules in terms of what qualifies as high, I want that to be really high bar to that. So I don't get notifications randomly for stuff that I don't really need to. And then there's smart organization. This is of course, that's what I talked about a bit there, which kind of labeling stuff and also starring some of the stuff based on some of the categories that I think are important to star them so that I know where they are and also put the labels on them. And then there's the automated response. This is just really just generating a draft. I'm not giving you the power to send the email because I want to be in the loop and figure out, did it write correctly? I want to review them, but it saved them in the drafts folder. But for my email here, something to note. For this email that I'm going to be cleaning up, it's full. I can't send or receive emails. So guess what? I can't even create a draft to begin with. So it's not going to work. It's going to write the email, but it's not going to be able to save in the drafts because I reached a quota. It's full. I need more memory. But if I run it on a cleaner, more newer email address that's not out of memory, it will save it to the draft. So that's a cool thing there to do. And then there's Slack notification. Of course, it will just send a really short, sweet message there. Let me hey, you got this email coming on. And then the intelligent cleanup, it will safely delete low priority. So low priority content gone, emails gone. And also based on age, I have some age categorization. We'll check on the on the code. If it passes on age, just delete them. Just get them out. And then there's the YouTube content protection. So I have emails there from my from YouTube either giving me notification of somebody commenting on my YouTube video or notification directly from. YouTube partner program. So I want those to be saved for good reason. So I don't want those to be deleted. And then there's trash management and just emptying the trash afterwards. And then just thread awareness. Basically some of the emails are probably in a thread conversation that we've maybe gone back and forth with somebody in the past. I wanted to read all the threads and figure out what's happening, what's happened before, what to be responded after now. And so that will help it draft a good response based on the history of what it's read. So good thing to keep in mind. So if you're looking at this project and you want to get started, this is how you get started. You come to this link and just git clone this and cd into the project, create your virtual environment, activate it, then create install, add your API keys. For me, I've experimented a bit with this, so I'm going to just tell you some of the ones and some of the results I've got from some of this. So I started off with all LLMs first because, you know, who wants to send their email data to LLM provider that is closed, right? You don't want to do that. But Some caveats here. I used Ollama. Most of the Ollama models, however they say they're good at function calling, it's very inconsistent. And this is just maybe due to the non-deterministic nature of LLMs, but sometimes it just fails and it just skips through some of the hoops or just gets really caught up in the loop so much. So I decided not to use it. The one that gave me more success a little bit was this Llama 3 Groq tool use. It was pretty good. Also is good, but I shied away from using it after a while because I was like, yeah, I'm not just going to use it at all at this point. But it's very iffy. It's not consistent. The other one I use also as well is Gemini. It was okay, but it kept throwing some errors a little bit. And I think it has to do with LightLLM, which is kind of like the wrapper for CrewAI to be able to call LLMs. And it didn't work greatly there for me. And I think maybe it's just because of the codebase has changed a little bit on LightLLM over the weekend. Last week. And then there's also OpenAI, especially GPT-4 or Mini. It works great for me. It's fast, doesn't cost much. I think it's just about $0.05 or something per million tokens. So it's the cheapest in my opinion, and also very fast. And I mean, it's not— you don't want to bring a big model to this because essentially it's just really reading emails and figuring out. GPT-4 does that pretty well. It can understand the emails. So there's nothing complex there. We're not generating code. We're not generating anything super, super like that needs a lot of high thinking here. This worked out pretty good. The other option really for you, if you're really concerned about just sending data, your emails, because at the end of the day, you don't want them having content of your email. There's that. But for me, this email address, and I'm going to really let you decide if you want to hook up your email to some of these LLMs because at the end of the day is what is in your emails? Is it something important? For example, I wouldn't hook this up to my work email, for example. Or I wouldn't hook this up to my other personal emails that I value the content that's in there that I don't want to share with anybody. But in this case, it's just really an old YouTube account, not YouTube account, but more of like old Gmail account that has just a bunch of— it's really mostly I used to sign up to some of these services and I get newsletters, I get notifications for connections, emails that I kind of don't care much about. And that's why I haven't even looked at it for years. That goes to tell you, low priority for me. I'd be cautious about that. But one thing you can do here as well, you can even fine-tune a model here for your use case. That's really going all the way out. Probably not going to do that. But there's so many things you can do here with Ollama. Just like I mentioned, find one that's really good at tool use. Try different ones. I didn't get so much luck there. The other portion is Gmail credentials. The agent would need to be able to— well, in this case, we're not really the— yeah, we're going to use the agent. In the beginning, we're going to need to log in, get the emails, but then also the agent will be able to use to send notification through your Gmail account and take some actions there. So you would need to go to your Gmail account, whatever Gmail you decide to use, go to that account. And I'm going to show you here in a bit on how to do that. And I even added a tutorial down here, a section to do that. You would need to go get your app password and it's under app in your settings. Just go there, generate one. You build an app and then it gives you a password. Just generate that. Adding your email address that's associated with that app password. And this, all this, you save this in your .env file. So that way, and make sure you add it to .gitignore so that way you don't commit it to GitHub and people get your credentials. And the last thing here is also we need to generate a Slack webhook. That's also pretty easy. You build an app and then you get your Slack webhook and then you add it in there so that way you get some notification back and forth. So let's talk about how to get a Gmail app password. I've added here a very detailed instruction on how to do that. So you would go to Google account settings and let's just go there. If you come here, first of all, what you need to do is switch to the email that you know you want to use. So I'm going to switch to that email. This is the email. The next thing is it tells you to go to security and then find app password down here. I wasn't able to do that. I wasn't able to find that. So what I did is just come up here and just type in app password. And you should get that as— there you go, app passwords. And you should be able to get that open for you. So when you click that, It opens, asks you to verify, and it's going to ask me to verify. Use touch to sign in. I'm going to use that. It's going to now load up and it should ask me now to put a name for an app that I want to create. So here you can just put a random app, for example. So you see, you already have another one already up there, but you can just put a random one and I'm going to delete this. So don't need to copy this. So it will give you a password like this. So this is the password you need to copy and save it in your .env and also Make sure you add this under password. The one thing that I would mention to you is when you save it, you see these gaps, make sure you delete these little gaps in between this so that way it doesn't throw a fit when you go to run the app. So I'm going to do that and I'm going to delete that so that way nobody can think, okay, wait, you exposed the password there. No. So now, you know how to generate the one for your account there. Now the next step here, let me just go again to my GitHub. And the next step from there, I want to talk a little bit about IMAP so that way, you know, actually what happens with IMAP. It's. Essentially a protocol that allows you really to communicate the email securely. And so if we come here, let's talk about IMAP and then we'll go to Slack after that. So here, IMAP is Internet Message Access Protocol, and it allows you to really securely connect to your Gmail account and manage your emails. And you can do some actions like here, read unread emails, apply labels, move emails to trash, save draft responses. And there's so many other functions you can add on there, like archiving. So basically you can do a bunch of stuff you could do in your email. Just using IMAP protocol. And there's a lot of extra information here. It's pretty much safe. But what we're doing as well here to make sure it's safe, after each operation that we get the agent to do, maybe just create a draft and put it in your Gmail, it opens the connection, does the action, and then after that, just closes the connection to just maintain security there. So each time it wants to take an action, it logs in, does the action, logs out, and back and forth until it finishes over the entire process. So that's pretty good right there. So let's go talk about how to get that Slack webhook. So this is also pretty straightforward. I don't need to show you how to do this. Just follow these instructions here. So you would go to your api.slack.apps and it's pretty straightforward from there. Just create a new app from scratch. It's literally just follow these entire instructions and you'll be able to get the Slack webhook at the end. It's a webhook. Get that, save that to your .env file and you should be able to be ready to rock and roll from there. So we've talked a bit about that. Now let's talk about what the application will do here. So if, if you run that, it will of course ask you to input a value for how many emails you wanted to process. So it'll fetch your unread emails first of all. The next step, it'll categorize them by type and priority, apply appropriate labels, generate a draft, send Slack notification for important emails, clean up low priority emails based on age, and then empty trash to free up storage at the end of it. So Those are the big ones. And smart deletions. This one I just want to touch before we go to the demo. So if there's promotion emails that are trying to promote some stuff for me, if they're older than 2 days, they automatically just delete them. If they're older than 7 days, unless it's high priority, just delete them as well. And then there's Shutterfly. I get a bunch of emails on Shutterfly, and that's why I had to put that on there. Just delete them regardless of age. Just delete them. And then there's receipts. If there's any receipts, I could archive them. I didn't add the archive function in here, But those are the ones that you could archive. But I don't have any receipts in here, so I just ignore that. If you have them in yours, you can add that function to do that. And then there's a YouTube, of course, protection. So any YouTube videos, any YouTube emails, don't delete them regardless of the age. And then thread handling and all that. So it's pretty fun for you to try. And now let's go to the code and go over how the code works and actually run the demo itself so you can see how it works. Okay, so I'm going to go through real quick the code base so you can see the structure and know what's where and what does what. And then quickly run through the demo so you can see the demo. So let's go ahead and do that. So of course here I have a virtual environment, pretty explanatory. So this is just assets, images that I have. Knowledge, we don't really want to use that here. But the main part here is you've got— you've come— there's a config file though. So this will hold all the YAML file configurations. And these are— that's where we define our agents, our tasks for the agents as well. And then we have tools. We'll be able to build some tools, the tools I showed you to do some of the stuff that we need agents to do. So we'll build those tools in there. And then we have finally the crew. This is where we assemble the entire crew, the main pipe, which is the entry point. We have models also, which is really important. And this is where we, we'll hold all the Pydantic models, which basically allows us to— we get an output from the emails, but we want to extract specific elements in the email. We want to extract what date it was sent. We want to extract the sender of the email, the recipients, if there's anybody CC'd. The models, essentially the Pydantic models allows us to intelligently go through the email, extract those pieces, and save them in like a JSON format in a really clean way. So that's another really awesome way to do that. So let's start with the config here and look at the agents file. So here's the agents. We just defined them here. They're pretty explanatory. So you can see we have categorizer, organizer, response generator, notifier, cleaner. And each one of them has a role, which basically says their job title, and then goal, which is really to monitor. And when I'm going through this, think of it as an employee, like I mentioned, right? So you have The role, which is like the job name, title, whatever. And then the goal is basically what are you supposed to do pretty quick? And then backstory is more of like the experience, right? If you are working at the job, what is your experience? Where have you worked? Are you senior? Are you really somebody that's done some stuff? So there's that there. And then there's also other extra stuff that we are adding here. Verbal is basically saying, once you work through, when you're walking through your thought process, when you're actually completing the task, I want you to add what you're doing. So essentially just follow along what the agent is doing there. Then we have the memory. I turned it true so you can remember some of the stuff that it's doing over time. It's saving that vector database so that way when it's working through this, it knows what it's done and that type of thing as well. Cool. Now let's get to tasks. Each agent needs to have a task. Every employee has to have a job description, what's expected of them and things like that. Same thing as agents. Here we start with the first one, which is categorization task. It's supposed to just categorize. It gets an email. Goes through the emails. It needs to categorize. Is it a newsletter? Add a label to it and say that's a newsletter. Add a label to promotion email. Add to personal emails, GitHub, YouTube, receipts, other. Just label them. And those are the labels that I'm adding there. So once you finish all these things, you'll be able to see how that works. And then there's also priority. So there's medium, high, low. And I'm also defining here what that means. So for high, only emails for urgent matters. Immediate attention or response. And then medium only for important but not urgent communication from real people or not. So do not mark promotional content. So any of the other content that doesn't really qualify as people, don't put them in medium, put them as other things. These exceptions and then required actions as well for each one of them, either say you need to do a reply, you need to read only, task, ignore. And this will be used by other agents to figure out, do I need to ignore this? Do I classify this as ignore? Do I classify read only, just reply and things like Like that. And then important YouTube, important YouTube email rules. So this is just for really YouTube. Don't delete anything from YouTube. That's essentially what I'm telling it to do there. And then format instructions. You know, your final answer must be a valid JSON and blah, blah, blah. I'm giving it an example with this. So this is one-shotting it and one-shot meaning is giving an example and it's just supposed to follow it. Pretty straightforward there. And then expected output. Think of this. You work at a job. There's expected output from you. What are you supposed to do to keep the job? This is essentially what I'm telling it to do, a JSON object. With email ID, subject, category, priority, required action fields. That's what I needed to do for me. And I'm assigning an agent to this task, which is the categorizer agent, which we already defined in the agents. And once it's done this task and it finishes, I wanted to output what it did into this folder, categorization_report.json. I have— this is just a patch for now. It doesn't really work because each time it runs through another email, it will keep updating that file. Like, it will not— it will kind of overwrite that file that it saves in. So You'll always won't have the entire full history. There's other ways to do kind of managing that so that way it writes after the next one so that way it doesn't delete all the entire history. But for this case, it's not important. I don't need it. That's just extra memory. I just added that just for the sake of it, but I don't really need that. Same thing with the rest of this. So task generator, they'll see this generates responses and I've gave it some extra information here so it should know. So for YouTube comments here, I'm just thank them for the comment, answer their question if possible, end with the best regards and my name. And I'm giving extra information there. I'm also at one point, let me see, I think I might have taken it out. So I added my meeting link. So when you want somebody to meet with you, share a link with them, hey, book time on my calendar. I added my calendar link to a previous version of this and you could add it in here as well. So if there's somebody requesting to meet with you or they just want to, you know, there's a potential there to meet, you can just add it there and tell it if somebody is asking to meet, blah, blah, blah, whatever. Generate the email draft, but also make sure you add my link in there to tell them to book time with me. So, and it did that pretty well for the other emails that I did. So that was awesome. And notification tasks, of course, this just sends me a notification to Slack that says, hey, check out this email. It's supposed to tell me the email subject, the sender, category, if the priority is high, and then a quick summary, a brief summary of what that email is about, and then what the action I need to do potentially. And some other extra information there that I need to do. And then the cleanup task, it just does what it needs to do, deletes all the stuff that I don't need to, or empty the entire bucket. So we've gone through that pretty fast and quick. Now let's try running this app. But before we do that, let's go over one section here. So this crew, this is where we bring all the entire crew together and we bring all the agents, we bring all the tasks, we bring all the tools, and then we run it from there. But one thing here that I mentioned, remember I mentioned before kickoff function here? So this is really important if you're building agents. So not all tasks require agents. Something like this, for example, this is just really just going to that, log into that email address, fetching the emails. So you don't really need an agent to do that. Just use a regular function. And this is what I'm doing here. It's taking in the input that you'll put in. I want the first 5 emails. We'll take in that. And then we'll go ahead and create a folder called output if it doesn't exist. And then It will just go over and grab all the emails, first 5 emails. And I added, I told it to do it in descending order. So the newest email and read email should be the ones that is grabbing from the top to going down. So that way it's kind of looking at the first new email. So that's pretty straightforward. And almost, I added a function here to get today's date because remember in the email, we want to figure out if it's older than 2 days, delete that, older than 7 days, delete that. How do we figure out if it's older than 7 days? I don't want to, I don't want the LLM to guess or look at it and be like, okay, I can't do math and then give me wrong math and then delete some emails. I want it to be really specific. So in this case, I added a function here to get today's date. And then using the Pydantic models that we'll define, we'll capture the date from the email itself when it was sent. And we'll do this function here, we'll do the math to determine how many days between that. And I've added the function here down here to do that. It will do the math, figure out how old the email is and put it in a bucket between this is older than 7 days, this is older than this. So when the agent comes back and looks at it, oh, okay, this is older than 7 days, just delete that. It does the function to delete that. That's pretty much what it does there. And then here we just have the LLM that we instantiated. We are getting that from OpenAI and then we are passing the agent there as well. So that's all crew.py is about. And then let's go to main function. This is the entry point for, for code, right? So if you come here, just add in here a section that will ask you a question on when you run here on the terminal, it'll ask, you know, How many emails would you like to process? You put in your number. If you don't put a number and just hit enter, it will default to the first 5 emails, unread emails, and it will just pull those in and start processing them. So that's all that section does right there. Main. One other thing that I want to talk about is models. So these are Pydantic models. And for the most part, really, you don't have to stress so much about this. If you have an AI on your system here, for example, if you're using something like Cursor, Windsurf, or even ChatGPT. Literally, you can just paste it there and ask it to generate this stuff for you. It's pretty straightforward. Add CrewAI docs for how to build your own custom agent and some of the instructions. Just prompt it really well and it will generate most of this stuff for you. So you don't have to worry about this. But this essentially, by Dante, just understand what you're doing here is really you're creating classes that the LLM will be able to follow and extract those pieces that you want to extract from the email. For example, this Email details. I wanted to extract email ID, which will be used to actually do some functions with the tools to draft emails and do all that stuff. And then subject, sender, body, date, age in days, threading for is part of a thread, thread size, thread position. This is really just more of like a threaded email, figure out exactly all the details about that thread. And then from email as well. So there's a whole bunch of categorization I'm doing here. So categorization as well. I'm just grabbing some stuff there. So all this essentially I'm just passing to the agent. And when you see here in crew.py, We have this function called output_pydantic, and this is the parameter for the tasks, for the task function within CrewAI. So it's a parameter. So there's output_pydantic and there's output_json. So since we created Pydantic models, we just use output_pydantic. And essentially we're just telling the agent, you know, for this task, make sure you follow this Pydantic model to be able to extract some of the stuff that you'll be running for this task specifically. So that's pretty much it. And now let's go ahead and run. So once you get your API keys, you save it here. Just to show you, I added an example here, example.env, _example. So for you, once you get your API keys, just come delete all this stuff and just add yours. So for example, if you don't want GPT-4 Mini, you can just change that to another model or entirely just do Ollama and then add the model that you want to use there. Add your API key there for your Gmail credential. Do the same webhook. Do the same there. That's pretty straightforward there. Now make sure you add it to GitHub. No, I already added here for you, but confirm that before you commit any any stuff to to to your project or to your repo. And this is just my as well. I know a lot of you will have questions about this, so I'm just gonna say it right now. I know a lot of people ask me, hey, what's that extension you're using to preview your markdown? Because it looks really nice, right? It looks pretty good, I have to say. This is Markdown, so if it's Markdown file, it's able to render like this. I'm going to show you that right now because I know most of you are going to ask about it, and I'm not gatekeeping none of this stuff, so I'm going to share this with you. It's this Office Viewer Markdown Editor. I'm not affiliated with this person, not affiliated with the creator of this, but I'm just happy to share some good stuff, right? So go delete the one you have, download this Office Viewer Markdown Editor. Just go to your apps, download that extension. And you have to restart your system first before it actually takes effect. And boom, you will get this. And it's really cool. I love it. It really displays everything the way I need it. Like, it gives you a really nice preview, right? Like, it looks pretty— looks pretty nice. So, and use this eye here to be able to preview the entire thing. You can actually from here just export it to a PDF directly. So extra stuff that I'm teaching here that I— it wasn't part of this thing. So anyway, that aside, We're now ready to run our application here. So what I'm going to do is I'm going to open my email side by side with this while it's running. So you're able to see what is being processed at what point and what we need to do. So bear with me here a second. I'm going to pull this up together and I'm going to get my email to the side. And this is not corresponding the way I want it to. So let's just put this here. There you go. Finally figured it out. All right, cool. So we are ready to run. And once you've installed— so if you're running this for the first time, like I said, crewai install, it will install all the dependencies, and then crewai run. So I'm going to do Command+B to kind of like close the side window, and I'm ready now to, to run it. So let me do this, open a little bigger there, and we're going to be using here. So what you need to do is crewai run. And if you run it, it should spin up the agent and ask us to enter how many emails we want to process. So looking here, we have how many emails? We have a lot of emails, 756. And that's just unread. And I'm sure there's more there. But let's say let's process the first 5. Why not? Right. And it should be this Google email notification. There should be this YouTube, Reddit, Google, and then something from Allison. And let's run that and see. Actually, I don't know who Allison is. So as you can see here, this is our before kickoff function and it just went and downloaded all the emails. And you can see here it says processing 5 emails, search result. Okay, it found 879 unread emails. Like I said, it's probably more than that and it just downloaded all the emails there. And it also did the calculation of how old those emails are based from today's date to the date that's on the email. So for example, this one from all of them are from the same date except this last one. So, you know, we got 191 days, 193. Those are pretty old. And it saved all the 5 emails under this folder right here. And you can just open that and we should be able to see all the emails. And these are all the emails that it fetched. And you can see it has all the information that we need. And using Pydantic models again, it just went through the emails and captured everything and put them in a nice JSON format. So at the end of the day, Pydantic is all you need really in this case. So Pretty good. Now let's see what it's doing. So the first agent here is the categorizer. We're supposed to categorize. You see something happen here with our emails. It generated labels. But before we get there, let's just go look and see what the agent did here. So it's supposed to— the first categorizer is supposed to categorize the emails based on this. And let's scroll down. And oh, I just noticed that I didn't share my Gmail account on the other side so you guys can see what's happening. But I'm going to share the entire screen here in a second. But let's just go over what the agents are doing here for a second. So the agents here, what they're doing here, essentially it went ahead and got all the emails from that folder and it just went through it. And here is where the stuff happens. Email categorizer here, it went and categorized the emails and it did categorize this first one from Google. It's more of telling me like, hey, I'm out of quarter, man. Like your entire storage is shot. So you need to do something about it. Pay us or know, clean up. So age is 191 days, and then required action is task. I need to do this as a task. And same thing here, it went and captured another email here. So YouTube reply, somebody replied to my video there, and just telling me just read only. You don't need to do anything, just need to read it. Uh, same thing here, it went and found one from Reddit, and they say just ignore it. It's just Reddit anyway. I could classify that differently as well. So the same thing here, ignore. And then there's a task here for Gmail again, warning that it will stop working in 2 days. And then we have email organization specialist. So this is supposed to organize my emails. And here it is doing some organization. So successfully logged into Gmail and then successfully disconnected. What I mentioned, log in, do some action, log out, back and forth. Right. And this is really for security. So here it passed in. So read file contents and then organize. It tells me what it did here. It added it into other, and this is that first email there. And then start true, it did start and all that extra stuff. So it does that for each and every email and categorization. So I don't want to go specifically into each one of them, but it went and did all that for all that. And then when we got here to email organization special, this is what it did. So this is like a summary of what it did. So it went for the first one, you can no longer receive emails. You say that's, you know, start that. It's true. Result success. It tells us, you know, I was successful in doing that. Same thing to the rest of them here as well. Now we get to the draft response. You need to figure out which one he wants to create a draft for. Here it went and created a draft. Let's see which one is this. You can no longer send or receive emails on Gmail. Really don't need a response to that, but it did generate it anyways, but that's fine. It's not going to save it anyway because the memory is full or storage is full here. Slack notification. This one also went and sent a Slack notification. I'm going to log into my Slack so you can see that it actually sent me a notification there. And we can see the final notification here. So he sent me notification for the two, you know, Gmail storage is full, and it did send me you can no longer send or receive emails, blah blah blah, all this stuff here as well. And the last one here, email cleanup. So it's supposed to go clean up all the emails, and so it identified which emails it needs to delete. So here it says the first email ID is marked high priority and will be preserved. Second email ID is also marked high priority, will be preserved. The third email is categorized as low priority. It's not from personal contact and it's not subject to any other retention rules. Thus, it meets the criteria for deletion. Reason for deletion, low priority email. Boom. 4th email, it's similar to the 3rd one. Categorization as low priority, so it will also be deleted for reason, low priority email. 5th email ID is marked high priority, will be preserved. So it's doing exactly what I wanted it to do here. And you can see here what it did as action. It's saying the days that it was out. Delete it, does what it needs to do there. And here is the cleanup report. Also, you should give me a cleanup report. So you can see here deleted emails. It added here within this JSON. It says it deleted this email and tells me which email ID, subject of the email, the sender, the age of the email, and is it deleted? True. Reason for the deletion? Low priority email. Boom. How many did it preserve emails? It preserved 1, 2, 3, 4 emails. Or 3 emails in this case. And then it also trash removed the 2 emails. So it deleted the 2 emails up here. And afterwards it went to trash and just cleaned out the trash because we don't need it. So it went and did all that. So let's actually go ahead and check my email and see if that— all right. So now this is my email. It went ahead and deleted it. And you see it created these 3 labels here, as you can see. And I'm going to zoom it in a little bit so you can see this. So you could see it went ahead here and created 3 labels. So it Created a label for other, Google, Reddit, and another Google here. So urgent as well. It created for the 3 as well. Google and YouTube, and then YouTube, it just added the YouTube onto there. So there's some classes here that overlap, but here you can see it added the YouTube one as a YouTube classification there. So now let's go and see. The trash should be empty because it deleted everything. There shouldn't be anything in there and it's loading. Yep. It's empty. And if you go to our inbox, it did clean that up a little bit. So let me run it again, but now this time let me maybe— let me, let me do maybe what— let's try 10, right? And see what it does. And so it should be the first 10 here. So 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Okay, let's see how it does. Let's give it 10, enter. So there you go, it's starting the process, and you'll see kind of All this move, see, it's moving away because it's being classified as red. And then also we'll start applying the labels once it starts doing that as well here. So here we go. So it's downloaded all the emails and we have the age for all the emails here as well, but it's going through the entire thing right now and feeding it to the context window. So let's see, we should be good on context window. Emails are not long enough to be able to consume a lot of tokens, but eventually it will. But so just something to be mindful there. I think rate limit is probably a bigger thing to be mindful of there. But so here it is. It's already got to the classification section of all the 10 emails. And you can see here, ignore, ignore, low, low. So there's a bunch of them that I'm expecting them to delete here and high, high, high, low, low, high. So let's see what we get here. So it should be some promotional ones. So we should see a promotional label here. Based on what it's saying. Let's see if it's going to do that here. And let's keep monitoring it. It's still working. So this is the organization specialist. So it should organize this as well. So let's see. So yeah, labels. And so it's putting the labels. We should see a label here pop up for promotions, I believe. Yes. So promotions. Yes. So it should be a label for promotions. So in the first run, it created these 3. In this run, it should have a promotional one because we don't have a promotional one. And it should— within that, we should see that promotion emails. In there. So there it is. It's running still. So the email organization specialist is still, it's essentially just going through each email one at a time and getting that done. I think there's a space here for optimization in terms of maybe batch processing some of these emails in a sense, some of the things to think about. But for me, this is more really a personal kind of really exploratory project. I know there's other tools out there like Superhuman AI. And as you can see, while I was just talking, promotions just popped up in there. And if you click on promotions, we should be able to see some promotion emails. So everything is running so slow right now. Yeah. So they added the Shutterfly promotional email on there as well. So like I was saying, these other email managers or management software out there, SaaS products like Superhuman, I think that's the most kind of popular, the one that's been talked about a lot because maybe it has some really good investors as well that can make the noise for them. And also maybe just people love it, you know, but I think it's more geared towards teams and I think there's a It's a monthly fee. I think the lowest was $25. I've never paid for it myself because I didn't think I needed, but just doing this as more of like a side thing, like, oh, you can actually build one yourself that kind of does the similar thing. They probably have more features in terms of like other different management things. Like I think they allow you to search over your emails. Like you can just search through a user interface and just search and say a specific keyword or specific thing. It just kind of shows you all those emails and summarizes them as well. So there's a cool thing about that as well. But I think adding that functionality on top of this, You could do that. Just add a vector database here and all the emails we've downloaded, just chunk them, store them in a vector database, and maybe add a chat UI within this entire app that you can actually chat with those emails over time. I think there's going to be a lot of work in terms of organizing those emails over time as well. So pretty interesting things to experiment with, but this should be like a starter project for you to just kind of get things going, doing it yourself. You don't really have to do it, but why not? It's an interesting way to do it. Here it is still running through it and it's going through the trash. So let's see the trash before it gets deleted. Oh, it actually deleted, deleted 6 emails in this case, which was what the low priority, like, and like I said, I think most of them were low priority and it deleted all of them that were low priority. So that's really the whole thing there. And you can see like we've ran through, you know, 740, you know, we've cut down a lot of emails so far. So I can run this like on, I can deploy this app first of all. to maybe something like CrewAI Enterprise. And it's pretty easy to do that as well. And then I can set a cron job on AI Enterprise. They have a way to set a cron job and I can let it run every other day and say, get the first 20 emails. Or I can even get to a point where I can actually fine-tune and say, get all the emails from today that I haven't responded to and just create responses with them or just delete, just clean it up for me essentially. One thing that I, before I forget, I need to show you the Slack notifications that we've got. So let's go ahead and do that. Okay. So this is my personal Slack and this is some of the previous runs that I ran earlier and yesterday, a few days ago. So we can go to today's run and let's see today. So this is today's run. So all this stuff that I just ran a few moments ago and it did alert me on some of them. So Gmail is about to stop working and it's telling me from Google, categories, other priority high, and then summary notification that your Gmail account will cease to function in 2 days. And then action needed task. So just telling me what to do there. I could specify a little more. And then there's just new comment alert and it's a new comment that's been added on your video, blah, blah, blah. Cool stuff. So let's just do a quick test here and let's run it and see. Cause so you can just see how the notification is coming in. Right. So there's a few emails here. Maybe let's run the first 5 in this case. Let's run the first 5 and see. There should be an email here that should be coming in. Let me show you this. So there's this YouTube one right here. So that should be the one that we should expect to send us a notification. But let's see if it's going to capture that YouTube. Yep. It should have captured it. So it should get the YouTube. So the YouTube one should come in here and here in a second. Let's see which one that is and scroll down. Cool. Yeah. The YouTube creators one. So it should come here in a second. So let's just monitor here and see. It should pop up here. It's now organizing the emails. We had an email organizing specialist. So this was sent at 1:28. So we should see a new one here in a sec. Cool. If you end up like building something with this also, please let me know. Happy to see what y'all end up building on top of this or not. Cause I think there's other ways to do this. That's not this complicated. But like making, you know, building things a little more complicated and having more control in a sense. So this is one of those places. The other thing that I'm going to experiment with as well is using MCP, context protocol. I know that's a new thing that's being talked about a lot, but I think that's— there's another aspect of you don't have to build a UI for it. So basically if you can download a desktop, you can download it to your system. You can build MCP, basically the tool that we built to do all this work. You could convert that into MCP server and essentially hook it up to Claude Desktop and you have the UI. And there it is. We got a new message and it says new music alert, or this was sent just now, 1:35. Attention music curators, check out this important update. YouTube creators. Sent by YouTube creators from YouTube. And it's saying the email discusses the increasing number of songs available on Creator Music platform. I don't really need to do anything about that, but thank you for letting me know. You could clean up more on the task to make sure that it doesn't send you things like this, but I wanted it to send me everything about YouTube because I never know what's what. So now I want it to figure it out for me. I'll let it figure it out for me. But I was talking about MCP. Once you build a server, MCP server, you have the clients which interacts with that MCP server. Which would be their cloud desktop, for example. And you would just use cloud of how you you generally use it, but then you're just able to let it do something for you. And while we're talking about that, I can actually show you how it works with something like like an IDE because you also have access to that here. This video is not about MCP because I think I might need to create another video about MCP here if you want me to. I think I will eventually. But here it is. So there's this MCP. Configuration here. You can configure an MCP server, you can build it locally. We could convert essentially our Gmail tool there to become an MCP server, and then we just need to configure sections here and we're good to go. We can hook it up to this. And what the cool thing about it is, you can just chat with it here. For example, I have one here from GitHub, and these are all the actions this MCP could do. So think of MCP as like a tool that interfaces with another UI or another web page that you can do actions on it. For example, With GitHub, I do actions on GitHub. I can go there and do actions. For example, I can go create and update a file there. I can search a repo there. I can create a repository. I can get file contents on GitHub and do a bunch of things with this tool. It kind of wraps that and it's able to take those actions and let you do it here. So for example, if I was maybe building a repo here and I have a project that I was building, if I want to create, usually typically what I have to do is once I get to a point where I want to share this repo others and I want to commit this code to GitHub. Usually what I had to do, I had to go to GitHub on the UI, create the repository, and then copy the commands from there for being able to commit the code to that repo and then come do that on my terminal here. And that way to be able to commit that repo to my GitHub repo. Now with this MCP server, it can create a repo for me. I can just tell it, hey, create a repo for this project and it will figure out the name. Build the repo. Once it's done that, I can tell it, okay, go ahead and generate the commit messages for this repo. And it will probably generate the best Git commit messages than I probably would because it understands the code better and it could just generate very concise and really fast too. It can generate that. And then I can tell it, just go ahead and run it and it will run all the commands, commit it to GitHub and boom, all of it done. Didn't have to touch anything on GitHub essentially. Just did all the stuff that I needed to do there. And that's the cool part about this entire thing with— so the UI is already there. Just hook it up to it and get it going. Same thing with Claude Desktop. If you have that app, you can just hook it up, get it done, or you can build your own UI, whatever, and then use it straight from there. So I could cover that in another video, but I just wanted to mention that because that's another alternative that I could do here. Maybe create one with MCP server that interfaces with my email. And all I have to do is just really chat with her and just tell her, hey, go fetch my first 5 emails unread emails. I'll fetch it. I tell it summarize a bit of them. Respond to this one. Draft the response first. Let me see it and then before I tell you to send it and they can actually do those extra steps just through the chat. I don't have to actually log into my Gmail. So there's that and that's actually pretty cool as well to do that. So this wraps up this entire tutorial and one thing that I encourage you to do of course go to my GitHub get the Get the code, run it. If you run into any problems, like always, put it in the chat. I'll try as much as possible to help you in this case. And also give me feedback on if you like this type of tutorial, if you didn't like it, what things you would like me to maybe build up on top of this. I could build a UI, for example, to go on top of this, like maybe a Streamlit app, or maybe try something different, maybe with Gradio, or even if you don't know, that's another Python web UI frameworks that we could use, or go You know, full-on mode and use something like V0 from Vercel, or even Cursor here, or Riff, or Codium to be able to build like a UI for us, for example, for this project. So there's a lot of alternatives there. But for now, thank you for watching. If you haven't subscribed already, please subscribe and I'll see you in the next video. Goodbye.
More from Tony

Summarize Videos and Research Companies with CrewAI + Gemini Models [Quickstart Guide]

How to Make Your AI Agents More Reliable with CrewAI Task Guardrails (Step-by-Step Tutorial)








