Lightweight Email Agents with smolagents

Combine HuggingFace's smolagents framework with MultiMail's email infrastructure for simple, effective email agents — with human oversight at the API level.


smolagents is HuggingFace's lightweight agent framework that supports both code-based and tool-calling agents. It emphasizes simplicity and works with any LLM available on the HuggingFace Hub, making it easy to build agents that interact with external services. MultiMail provides the email infrastructure that smolagents need to send, receive, and manage email.

By integrating MultiMail with smolagents, your agents can compose and send emails, check inboxes, and manage contacts through simple tool definitions. smolagents' code execution mode can write arbitrary email-sending code, but MultiMail's API-level oversight ensures that even code-generated send calls go through approval when configured in gated_send mode.

Connect smolagents to MultiMail by defining Tool subclasses that call the REST API. Both ToolCallingAgent and CodeAgent can use these tools to perform email operations within their respective execution patterns.

Built for smolagents developers

Simple Tool Definitions

smolagents uses a clean Tool base class pattern. Define MultiMail tools by subclassing Tool with a name, description, inputs, and a forward method that calls the API. No complex abstractions required.

API-Level Safety for Code Agents

smolagents' CodeAgent can write and execute arbitrary Python code, including HTTP requests. MultiMail's oversight operates at the API level, so even dynamically generated code that calls the send endpoint goes through approval in gated_send mode.

Hub Model Flexibility

smolagents works with any LLM on HuggingFace Hub or via API. Pair your preferred model with MultiMail tools to build email agents without being locked into a specific LLM provider.

Lightweight Architecture

Both smolagents and MultiMail's REST API are designed for simplicity. No complex SDK installations or heavy dependencies — just define tools, create an agent, and start processing email.

Graduated Trust

Start with gated_send where every email your smolagent drafts requires human approval. As you build confidence in the agent's output quality, graduate to monitored or autonomous modes for less friction.


Get started in minutes

Define MultiMail Tools
python
import requests
from smolagents import Tool

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

class SendEmailTool(Tool):
    name = "send_email"
    description = "Send an email through MultiMail. In gated_send mode, queues for human approval."
    inputs = {
        "to": {"type": "string", "description": "Recipient email address"},
        "subject": {"type": "string", "description": "Email subject line"},
        "body": {"type": "string", "description": "Email body content"},
        "mailbox_id": {"type": "string", "description": "Mailbox ID to send from"}
    }
    output_type = "string"

    def forward(self, to, subject, body, mailbox_id):
        resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
            "mailbox_id": mailbox_id, "to": to,
            "subject": subject, "body": body
        })
        return str(resp.json())

class CheckInboxTool(Tool):
    name = "check_inbox"
    description = "Check the inbox for recent messages."
    inputs = {
        "mailbox_id": {"type": "string", "description": "Mailbox ID to check"},
        "limit": {"type": "integer", "description": "Max messages to return", "nullable": True}
    }
    output_type = "string"

    def forward(self, mailbox_id, limit=10):
        resp = requests.get(
            f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
            headers=HEADERS, params={"limit": limit}
        )
        return str(resp.json())

Create smolagents Tool subclasses that wrap MultiMail API endpoints.

Build a ToolCallingAgent
python
from smolagents import ToolCallingAgent, HfApiModel

"cm"># Use any HuggingFace Hub model or API-based model
model = HfApiModel(model_id="meta-llama/Llama-3.1-70B-Instruct")

agent = ToolCallingAgent(
    tools=[SendEmailTool(), CheckInboxTool()],
    model=model,
    system_prompt="You are an email assistant using MultiMail. "
    "The mailbox operates in gated_send mode, so sent emails "
    "are queued for human approval before delivery. "
    "Always confirm what you&"cm">#039;ve done after each action."
)

result = agent.run("Check my inbox for mailbox your_mailbox_id and summarize unread messages")
print(result)

Create a smolagents ToolCallingAgent that uses MultiMail tools for email operations.

Build a CodeAgent for Email
python
from smolagents import CodeAgent, HfApiModel

model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")

"cm"># CodeAgent writes Python code that calls your tools
code_agent = CodeAgent(
    tools=[SendEmailTool(), CheckInboxTool()],
    model=model,
    system_prompt="You are an email assistant. Write Python code to "
    "accomplish email tasks using the provided tools. The mailbox "
    "uses gated_send mode — emails are queued for human approval. "
    "The mailbox ID is &"cm">#039;your_mailbox_id'."
)

# The agent will generate and execute code like:
# inbox = check_inbox(mailbox_id="your_mailbox_id")
"cm"># for email in inbox['emails']:
"cm">#     if 'urgent' in email['subject'].lower():
"cm">#         send_email(to=email['from'], subject=f"Re: {email['subject']}", ...)
result = code_agent.run(
    "Check my inbox and reply to any emails with &"cm">#039;urgent' in the subject"
)
print(result)

Use smolagents' CodeAgent which writes and executes Python code to handle email tasks.


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 smolagents and requests for calling the MultiMail API.

bash
pip install smolagents requests
3

Define Email Tools

Create Tool subclasses for send_email, check_inbox, and reply_email that wrap MultiMail API endpoints.

4

Create Your Agent

Choose ToolCallingAgent or CodeAgent based on your needs, and pass your MultiMail tools along with a system prompt explaining the email context.

bash
agent = ToolCallingAgent(tools=[SendEmailTool(), CheckInboxTool()], model=model)
5

Run and Approve

Execute the agent with agent.run(). Review and approve pending emails in the MultiMail dashboard when using gated_send mode.

bash
result = agent.run("Check my inbox")

Common questions

Should I use ToolCallingAgent or CodeAgent with MultiMail?
ToolCallingAgent is simpler and more predictable — the LLM selects tools and the framework calls them. CodeAgent is more flexible, writing Python code that can implement complex email logic. For most email workflows, ToolCallingAgent is recommended. Use CodeAgent when you need custom logic like filtering, batching, or conditional processing.
Is CodeAgent safe to use with email sending?
MultiMail's oversight operates at the API level, so even code generated by CodeAgent goes through gated_send approval. The agent can write code that calls send_email, but the email won't deliver until a human approves it in the MultiMail dashboard. This makes CodeAgent safe for email tasks despite its code execution capabilities.
Can I use open-source models from HuggingFace Hub?
Yes. smolagents works with any model on HuggingFace Hub or via API. Models like Llama 3, Qwen, and Mistral work well for email tasks. For code generation with CodeAgent, code-specialized models like Qwen2.5-Coder tend to produce better results.
How do I handle email attachments with smolagents?
Create an additional Tool subclass that handles attachment encoding. The tool can accept a file path, encode it as base64, and include it in the MultiMail API request body. Both ToolCallingAgent and CodeAgent can use this tool alongside send_email for attachment support.
Can smolagents handle long-running email workflows?
smolagents is designed for single-turn agent interactions. For long-running workflows that span human approval delays, implement a polling loop that creates a new agent.run() call to check pending status. For more complex multi-step workflows, consider pairing smolagents with a workflow orchestrator like LangGraph or ControlFlow.

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.