Have you ever needed to automate a task that involved looking up information in your CRM, making a decision, then repeating that process over and over. This was previously impossible to automated, but with advances in AI Agents, it's now possible! In this tutorial, I'll show you how to build an AI agent that generates unique three-letter codes for contacts in a Notion database used as a CRM, which involved being smart about which code to generate and checking the CRM to make sure it doesn't exist yet.
Why AI Agents?
Unlike standard AI steps that simply take inputs and generate outputs, AI agents are sophisticated tools that can handle complex, multi-step reasoning tasks. They can be equipped with specific tools and capabilities, making decisions about when and how to use them to accomplish their mission.
If you have a use case that requires iteration and judgement, AI agents can do things that were previously impossible in automation tools.
What We'll Build
We will build a workflow that does the following
- Triggers when a new entry is added to our CRM
- Activates an AI Agent to generate a unique code for that entry, using the following tools:
- Generate a new code based on the customer's name
- Check if that code already exists in the database
- If there's a collision, the AI agent will keep trying to generate a different code until it succeeds.
- Finally, write the successfully generated unique code back into the CRM.
This process requires multiple steps and iterations - perfect for demonstrating the power of AI agents.
Prerequisites
Before we begin, here's what you'll need:
- A Relay.app account. If you don't have one, you can sign up for free!
- A CRM (in Notion or any other tool) to use for testing
Setting Up the AI Agent
1. Setting up the Trigger
First, to kick off the workflow, we'll trigger every time a new customer is added to our CRM. In my in this case, I will use the "New Page Added" trigger in Notion.
2. Creating the AI Agent
In the AI menu, select "AI Agent" to create the framework for the agent.
3. Equipping the Agent with Tools
Before we write the agent's top-level prompt, it's useful to first specific the tools it will use. Our AI agent needs two essential tools: one to generate codes and another to look up in our CRM whether we've used a code already.
Tool #1: GenerateCode
- Purpose: Creates new three-letter codes
- Inputs: Customer name and list of previous attempts
- Logic: Uses first letters of names or alternatives to generate codes
- Output: Returns a three-letter code
Tool #2: CheckForCollision
- Purpose: Verifies if a code already exists
- Input: The generated code
- Action: Searches the Notion database
- Output: Returns whether the code exists
4. Configuring the Agent's Prompt
The agent needs clear instructions to coordinate these tools effectively, so I broke the prompt down into a clear step-by-step guide for the agent.
5. Testing your Workflow
Now that we have a complete workflow, it's time to test it. It's best to test the agent several times, including multiple instances that will have the same name and therefore cause the agent to need to generate multiple codes.
Conclusion: The Power of AI Agents
This example demonstrates several key advantages of AI agents:
- Multi-step reasoning
- Tool coordination
- Iterative problem-solving
While this specific example focuses on generating unique codes, the principles can be applied to many other scenarios. AI agents represent a powerful evolution in automation capabilities. By combining multiple tools with intelligent decision-making, they can handle complex tasks that would be difficult to automate through traditional means. The key is to:
- Break down your problem into clear steps
- Provide the right tools
- Give clear instructions
- Handle edge cases appropriately
Ready to Try It Yourself?
Start by identifying a repetitive task in your workflow that requires multiple steps and decisions. Consider how you could break it down into tools and let an AI agent handle the complexity for you.