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

Learn how to add task guardrails to CrewAI agents to enforce constraints like word counts. The tutorial walks through building a blog-writer agent with a custom validation function that checks output and triggers automatic retries until constraints are met, using OpenAI and Serper.dev.
Chapters
Hey there. In this video, we're going to learn about task guardrails in CrewAI. First of all, what is a task guardrail and why should you even care? Well, it is a powerful way for you to add custom validation logic to your agentic workflows. So it's like this GIF right here. You see there's a child throwing down the bowling ball, but then on the sides, there's guardrail essentially that's guiding the ball all the way down the lane. And at least it's going to strike some pins because it's not going to go into the gutter. Same thing with your agents. You want to have guardrails that guide it to the expected output that you want it to have at the end of the day because you want it to be more deterministic. So what we're going to go through is we're going to go through how to write a custom guardrail function, how to attach it to a task, and then learn about how it automatically retries until the output meets your criteria. So let's get started. First of all, let's set up the imports and add the API keys that we need. So we'll need Serper Dev Tool, and this will be for doing some search on the web. It's a tool that basically searches the web for specific keywords trying to answer our question. And then we have task outputs. That's just an object that we'll be using to handle the validation logic. So first of all, let's do the installation here. So pip install updates and quietly crewai and crewai-tools because that's what we'll need. Serper Dev Tools depends on our crewai-tools and then crewai, which is our package itself that we'll need to be, to use here. And then once that's finished, we have to import CrewAI. And then from CrewAI, we're importing the agent, the task, the LLM, and the crew. And then we're importing typing, we're importing task output here from CrewAI as well. And then all separate dev here. Those are the most important ones that we need to import. And then here I'm just suppressing some of the warnings that we'll get. And just that way it doesn't clutter our notebook a bit. And then here we're getting all the API keys that we need, and I've saved them here in my notebook secrets. And this is on Colab. So I have my OpenAI API key. You can use any other LLM provider API key. And then also I have my Serper.dev API key as well that I'm using, and I'm adding that into the environment so that way it picks it up from the agent itself. Now that portion is done. Now let's go ahead and create the custom guardrail function. So here's where the actual magic happens. And the demo that I'm going to show you here is basically an agent that writes a blog given a specific topic. I'm going to give it a topic and it's going to write a blog, but I want to guardrail the amount of words that it's going to use in that blog, for example. So here, first of all, we create a function validate blog content, and this function is going to take in the task output. So for the task that will be writing the blog, we want the output of that task to be passed to this function, which will validate the output. Basically what it does, it takes the output from that task that the agent has finished. It does the count of how many words are there. If it's beyond the limit that we've set, it asks the agent again, hey, you didn't meet the requirements of the guardrail, so redo this again. And it does that iteratively until it meets the word count. So essentially until the validate blog post content is true, and then it's able to continue. So Here you can see, check the word count, and we're passing in the row output and we're splitting it, and then we're doing the count there. If the length of the words from here is greater than 50— and this is where we can change this, we can maybe start it with 200— if the word count is greater than 200, let's fail. Essentially just say, hey, it's beyond 200, let's redo this again. And then if not, you know, return the— that it worked, it's, it's correct, it's got the correct word count, so that it can pass it to the next agent or task or finish the process and actually return an output. Now that we've done that, we come here and now build our agents. So our crew, essentially, it's made of agent tasks and tools. So here we start with LLM, which is the brain of our agent. And I'm going to be using GPT-4o Mini. It's cheaper. No need for any extra stuff here because it's just a simple blog. And so we have the blog agent here. The role is a blog writer. Goal is write a blog post. Their background story, an expert blog writer. And we give it a tool, Sopra DevTool, basically to go search the web for that keyword that we've given it so that it can return up-to-date information. And then we give it the LLM, of course, that we needed to use, verbose true, so that it can tell us exactly what's happening under the hood on what it's working on. And then now we create a task for this agent, which we start with the description and write our super detailed blog post about this variable, which we've interpolated, which will give it at the bottom what we pass it into the agent. We want the user to be able to pass in what the blog post is about and also the year. This will be automatically be using the datetime package to be able to pass in the year that we are in today. And then expected output, a properly structured blog post under 50 words, but we see it up there, we want 200. So under 200 words, and then the blog format, we want just title, subtitle, and the paragraphs. So very simple. And then agents that will do these tasks, we've— is the agent we've built up here. And then markdown true. So basically return the final output here as markdown. And then we add the guardrail. This is the important part. We the function that we created up here to validate the blog, we just added here under the parameter guardrail. We just add that function right there. And then we have the max retries. So the default is three, but you can up this to however much you want. So basically you wanted to iterate how much how many times on this specific task until it gets it right. And then finally let's put the entire crew together. So we have the agents. We only have one agent. We have one task. And then verbose true, and that's it. So let's run that, and that should be running here in a second. It's done. Now we finished. Now let's go ahead and launch the crew to get it going. So first of all, we create a variable here, results, and then crew kickoff. So we want to kick off that crew, and then the inputs. This is where the extrapolation is going to happen all the way back to the crew. So the prompt, we wanted to write about crew AI, and the year that we wanted to write about, it's today, and which is the year today. So 2025 essentially. So if we run that, it should kick off the agent that's going to go to the web and you'll see here it's thinking. So it's saying write a super detailed blog post about CrewAI for 2025. It's going to go to the web, search some few words about CrewAI, and you can see here that the search query that it is using, CrewAI 2025 features and advancements. That's one of the tool input the agent decided I'm going to search about this. And this is the results we got from searching the web. And it's going to look at that and figure out, okay, I'm going to write a blog about it. So it got the content and it's going to write a blog. And here's where you see the guardrail check coming in. So you see here, define the definition of the function here and it checks attempt 1 evaluating. And I added a print function in the guardrail function up there to just kind of give me the word count so I can know how many count, how many words it was. And it was 169 for that blog up there. And the validation passed, of course, and it's on the first attempt. And now we get the final blog outputted here. So it's finished. And if you want to just display this for the sake to make it look pretty and using Markdown, so I'm just importing IPython there and rendering it with Markdown. So you can see here is the final blog, Future of AI Automation, CrewAI 2025. And it has a blog here already created. So that's really it. It's pretty Quick and, and simple. And you can change this to however many other words. So I'm going to share this Colab notebook with you so you can play around with it. You can change this to even 50 words and it should work, and it's just going to adhere to the parameter there. So if we change this to 50 and then come here and change this to 50 as well, so that way it knows that's what we wanted to do. So let's run this again and see what it gives us. It should give us a 50-word blog, which should be very short. And here it did. It went and searched the web. It's going through the process here of creating it and evaluating 45 words. So it got it right on the first go. So there you go. And here's the blog with just 45 words. So that's it. And for further more information, you can come to our docs. We do have a lot of information on our documentation that you can follow along as well. But this is how you would set up Guardrail essentially for your tasks. And you can think of this as just a simple example. You can build however much you want in a custom Guardrail function. So you can create a function that fitting your task that you want the agent to perform. You basically decide what parameters you want to pass in to make sure that this agent perform this task. This is the output I expect from this agent. So that's it for this video and see you in the next one. Bye.
More from Tony

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









