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.
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.
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.
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.
Let’s see how to use both agents on a real email thread. You’ll read an email from a file, parse it, classify its intent, and then summarize it.
src/mastra/index.ts
- The code reads the email thread from a file and parses it.
- It gets both agents from the Mastra instance.
- It sends the first email to the intent agent to classify the intent.
- It then passes both the email and the detected intent to the summary agent for processing.
Suppose your example-email-thread.txt contains:
When you run the code, you might see:
- 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!
