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.


Try it with your agent

No code, no dashboard. Paste this to your AI agent — it connects MultiMail, creates an inbox, and builds the flow for you.

1. Get MultiMail ready first: read https://multimail.dev/llms.txt, connect the MultiMail MCP server exactly as documented there, create a free inbox through the MCP signup flow, activate it, set up a verified sending domain, and configure the mailbox oversight mode to gated_send. 2. Wire MultiMail into my DSPy agent using DSPy's real MCP tool path: install DSPy with MCP support, open an MCP ClientSession to the connected MultiMail server, initialize the session, list the available MCP tools, convert each needed MultiMail MCP tool with dspy.Tool.from_mcp_tool(session, tool), and pass those tools into dspy.ReAct with a dspy.Signature for email work. Use react.acall because MCP tools are async by default. 3. Give the DSPy ReAct agent three capabilities only: check the inbox using the discovered MultiMail inbox tool, draft replies with DSPy's declarative Signature output before sending, and send or schedule email only through the discovered MultiMail compose/send tools. Do not invent any API path, webhook, header, or tool name; use only the tools the MultiMail MCP server actually exposes. 4. Send one test email from the verified sender to my developer test address. The email should say it is a DSPy plus MultiMail smoke test, include the inbox address, and require no recipient action. 5. Keep the mailbox in gated_send for the test and for the generated agent. After the send tool is called, stop and report the pending approval result so I can review before anything sends; do not switch to monitored or autonomous unless I explicitly ask.

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.

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.

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 hosted MCP server. Formally verified in Lean 4.