Adding a Second Agent for Intent Classification
Introduction: Why Use Multiple Agents?
So far, you’ve built a single Mastra agent to process and summarize emails. In real-world projects, it’s often better to split complex tasks into smaller, focused agents. This modular approach makes your system easier to manage, test, and extend.
For example, you might want one agent to summarize emails and another to classify the intent behind each message (such as a question, meeting request, or status update). In this lesson, you’ll add a second agent for intent classification and see how to use both agents together in your project.
Quick Recap: Agents in Mastra
A Mastra agent is a smart assistant that follows your instructions to process data. You’ve already set up a summary agent that can read an email and generate a summary, decide if a reply is needed, and suggest a draft reply.
Now, you’ll add a new agent that focuses only on classifying the intent of an email.
Defining an Intent Classification Agent
Let’s create a new agent whose job is to figure out the intent of an email. You’ll use the Agent class from Mastra and the openai model provider.
src/mastra/agents/intentAgent.ts
- The
instructionsfield tells the agent exactly what to do. - The model is set to use OpenAI’s GPT-4o.
Registering Multiple Agents in Mastra
You can register multiple agents in your Mastra application. Here’s how you do it in your main entry file:
src/mastra/index.ts
Now, both SummaryAgent and IntentAgent are available in your app.
Running the Agents Together
Example Input and Output
Suppose your example-email-thread.txt contains:
When you run the code, you might see:
Summary
- You now have two agents: one for intent classification and one for summarization.
- You registered both agents in your Mastra app.
- You used the intent agent to classify an email, then passed that intent to the summary agent for richer processing.
- This modular approach makes your assistant more flexible and easier to extend.
You’re ready to practice building and running your own intent classification agent using this structure!
