Optimized Email Agents with DSPy

Use DSPy's declarative programming model to build email agents that improve automatically through optimization — with MultiMail handling delivery and human oversight.


DSPy is a framework from Stanford for programming with foundation models using signatures and optimizers rather than manual prompt engineering. Instead of writing prompts, you declare what the model should do, and DSPy compiles your declarations into optimized prompts. MultiMail provides the email infrastructure that DSPy modules need to actually send, receive, and manage messages.

By integrating MultiMail with DSPy, you can build email agents where the email composition logic is optimized automatically. Define signatures for tasks like 'compose a professional reply' or 'classify email urgency,' and let DSPy's optimizers find the best prompt strategies. MultiMail handles the delivery with configurable human oversight.

DSPy's assertion system complements MultiMail's oversight modes. Assertions validate email content quality at the generation level, while MultiMail's gated_send mode adds human judgment at the delivery level. Together they ensure both content quality and send authorization.

Built for DSPy developers

Optimized Email Composition

DSPy's optimizers automatically improve your email drafting prompts based on metrics. Define what a good email reply looks like, provide examples, and let DSPy find the optimal prompting strategy for your use case.

Assertions Meet Oversight

DSPy's assertion system validates email content quality (length, tone, completeness). MultiMail's oversight modes handle delivery safety (correct recipients, appropriate timing). Together they cover both content and delivery concerns.

Declarative Email Workflows

Define email tasks as DSPy signatures rather than writing complex prompts. A signature like 'email_content -> professional_reply' is clearer and more maintainable than a hand-crafted prompt, and DSPy optimizes it automatically.

Metric-Driven Improvement

Use DSPy's metric system to measure email quality. Track response relevance, tone appropriateness, and accuracy against your knowledge base, then optimize your modules to maximize these metrics.

Reliable Email Infrastructure

While DSPy optimizes the intelligence layer, MultiMail provides production-grade email delivery, thread tracking, contact management, and audit logging. Each handles what it does best.


Get started in minutes

Define Email Signatures and Modules
python
import dspy
import requests

MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}

"cm"># Define signatures for email tasks
class ClassifyEmail(dspy.Signature):
    """Classify an email by urgency and intent."""
    email_subject: str = dspy.InputField()
    email_body: str = dspy.InputField()
    urgency: str = dspy.OutputField(desc="one of: urgent, routine, low")
    intent: str = dspy.OutputField(desc="one of: question, request, update, complaint")

class ComposeReply(dspy.Signature):
    """Compose a professional email reply."""
    original_email: str = dspy.InputField()
    context: str = dspy.InputField(desc="relevant background information")
    reply: str = dspy.OutputField(desc="professional email reply body")

"cm"># Configure DSPy
lm = dspy.LM("openai/gpt-4o")
dspy.configure(lm=lm)

Create DSPy signatures for email tasks and modules that call MultiMail for delivery.

Build an Email Processing Module
python
class EmailResponder(dspy.Module):
    def __init__(self):
        self.classify = dspy.Predict(ClassifyEmail)
        self.compose = dspy.ChainOfThought(ComposeReply)

    def forward(self, email_subject, email_body, context, mailbox_id, message_id):
        "cm"># Classify the email
        classification = self.classify(
            email_subject=email_subject,
            email_body=email_body
        )

        "cm"># Compose a reply
        reply = self.compose(
            original_email=f"Subject: {email_subject}\n\n{email_body}",
            context=context
        )

        "cm"># Send via MultiMail (gated_send queues for approval)
        resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
            "message_id": message_id,
            "body": reply.reply
        })

        return dspy.Prediction(
            urgency=classification.urgency,
            intent=classification.intent,
            reply=reply.reply,
            send_result=resp.json()
        )

"cm"># Use the module
responder = EmailResponder()
result = responder(
    email_subject="Pricing inquiry",
    email_body="What are your current rates for the Pro plan?",
    context="Pro plan is $29/month with 25 mailboxes and 30,000 emails.",
    mailbox_id="your_mailbox_id",
    message_id="msg_abc123"
)

Create a DSPy module that classifies, composes, and sends email replies through MultiMail.

Add Assertions for Email Quality
python
class ValidatedEmailResponder(dspy.Module):
    def __init__(self):
        self.classify = dspy.Predict(ClassifyEmail)
        self.compose = dspy.ChainOfThought(ComposeReply)

    def forward(self, email_subject, email_body, context, mailbox_id, message_id):
        classification = self.classify(
            email_subject=email_subject,
            email_body=email_body
        )

        reply = self.compose(
            original_email=f"Subject: {email_subject}\n\n{email_body}",
            context=context
        )

        "cm"># Assertions validate content quality before sending
        dspy.Assert(
            len(reply.reply) > 50,
            "Reply is too short. Provide a substantive response."
        )
        dspy.Assert(
            len(reply.reply) < 2000,
            "Reply is too long. Keep it concise and professional."
        )
        dspy.Suggest(
            "thank" in reply.reply.lower() or "appreciate" in reply.reply.lower(),
            "Consider acknowledging the sender&"cm">#039;s message."
        )

        # Send via MultiMail after assertions pass
        resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
            "message_id": message_id,
            "body": reply.reply
        })

        return dspy.Prediction(
            urgency=classification.urgency,
            reply=reply.reply,
            send_result=resp.json()
        )

Use DSPy assertions to validate email content before sending through MultiMail.

Optimize with Metrics
python
from dspy.evaluate import Evaluate

"cm"># Define a metric for email quality
def email_quality_metric(example, prediction, trace=None):
    reply = prediction.reply
    score = 0
    "cm"># Check reply addresses the question
    if example.intent == "question" and "?" not in reply:
        score += 1  "cm"># Answers don't contain questions
    "cm"># Check appropriate length
    if 100 < len(reply) < 1000:
        score += 1
    "cm"># Check professional tone
    if reply[0].isupper() and reply.rstrip().endswith("."):
        score += 1
    return score / 3

"cm"># Create training examples
trainset = [
    dspy.Example(
        email_subject="Pricing question",
        email_body="What does the Pro plan cost?",
        context="Pro plan: $29/month, 25 mailboxes, 30k emails.",
        mailbox_id="test", message_id="test",
        intent="question"
    ).with_inputs("email_subject", "email_body", "context", "mailbox_id", "message_id")
]

"cm"># Optimize the module
optimizer = dspy.MIPROv2(metric=email_quality_metric, auto="light")
optimized_responder = optimizer.compile(
    EmailResponder(),
    trainset=trainset
)

Define email quality metrics and use DSPy's optimizers to improve your email agent automatically.


Step by step

1

Create a MultiMail Account and API Key

Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.

2

Install Dependencies

Install DSPy and requests for calling the MultiMail API.

bash
pip install dspy requests
3

Define Email Signatures

Create DSPy Signature classes that declare your email tasks — classification, composition, and any other email processing steps.

4

Build Your Module

Create a DSPy Module that chains your signatures together and calls the MultiMail API for delivery. Add assertions for content validation.

bash
responder = EmailResponder()
result = responder(email_subject="...", email_body="...", context="...", mailbox_id="...", message_id="...")
5

Optimize and Deploy

Define quality metrics and use DSPy's optimizers to improve your email agent. Review pending emails in the MultiMail dashboard when using gated_send mode.


Common questions

How do DSPy assertions work with MultiMail's oversight?
DSPy assertions validate content quality before the email reaches MultiMail's API — checking length, tone, and completeness. MultiMail's oversight modes then handle delivery-level safety. Assertions catch content issues automatically, while oversight catches contextual issues that require human judgment.
Can I use MultiMail approval data to optimize my DSPy module?
Yes. Track which emails humans approve vs reject in gated_send mode. Use this as training data for DSPy's optimizers to improve composition quality. Over time, your module produces emails that need fewer rejections, building toward monitored or autonomous operation.
Does DSPy support tool calling for MultiMail operations?
DSPy focuses on optimizing LLM signatures rather than tool calling. The recommended approach is to call MultiMail's REST API directly in your module's forward method after the LLM generates the email content. This keeps the optimization focused on content quality while the API call handles delivery.
Can I optimize email classification and composition separately?
Yes. DSPy modules are composable. Define separate signatures for ClassifyEmail and ComposeReply, each with their own metrics and optimization. This lets you optimize classification accuracy and reply quality independently, then combine them in a higher-level module.
How do I handle different email types with DSPy?
Use DSPy's classification signature to categorize inbound emails, then route to specialized composition modules for each type. Each module can have its own signature, assertions, and optimization metrics tailored to the email category (support requests, sales inquiries, etc.).

Explore more

The only agent email with a verifiable sender

Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.