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

import { Agent } from "@mastra/core/agent";
import { openai } from "../openai.js";

export const IntentAgent = new Agent({
  name: "Email Intent Classifier",
  instructions: `
    Given an email thread, classify the intent as one of:
    - Question
    - Meeting Request
    - Status Update
    - Other
  `,
  model: openai("gpt-4o"),
});
  • The instructions field 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

import { readFileSync } from "fs";
import { parseEmailThread, processEmail } from "./emailHandler.js";
import { Mastra } from "@mastra/core";
import { SummaryAgent } from "./agents/SummaryAgent.js";
import { IntentAgent } from "./agents/IntentAgent.js";

export const mastra = new Mastra({
  agents: { SummaryAgent, IntentAgent },
});

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:

From: sarah.jones@acme-corp.com  
To: engineering-team@acme-corp.com  
Subject: Project Phoenix – Action Items & Confirmation Needed

Hi Team,

Thanks for your work on Project Phoenix so far. We’ve finished the requirements and have a draft of the technical specs. The client reviewed our docs and sent feedback (see attached).

Action items for this week:
- Priya & Ahmed: Update the specs based on client feedback by Thursday.
- Jason: Set up the dev environment and confirm access for everyone by Wednesday.
- Emily: Start drafting test cases from the updated specs.

Please reply to confirm you’ve received your tasks and let me know if you have any questions or concerns.

Thanks,  
Sarah

When you run the code, you might see:

Intent: Status Update
Summary: Sarah summarizes project progress, lists action items for the team, and requests confirmation and questions from recipients.

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!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal