RAG-Powered Email Agents with LlamaIndex

Combine LlamaIndex's retrieval-augmented generation with MultiMail's email infrastructure to build agents that send context-aware, data-grounded emails.


LlamaIndex is the leading data framework for connecting LLMs to external data sources. It specializes in indexing, retrieval, and RAG patterns, making it ideal for agents that need to reference documents, knowledge bases, or prior emails when composing responses. MultiMail provides the email infrastructure layer that LlamaIndex agents need to actually send, receive, and manage messages.

By integrating MultiMail with LlamaIndex, your agents can retrieve relevant context from your data sources, compose informed emails, and send them through managed mailboxes — all while respecting human oversight boundaries. The monitored mode is particularly useful for RAG-powered auto-replies where you want visibility into what the agent sends without blocking every message.

Connect LlamaIndex to MultiMail by defining tool specs that wrap the REST API, or use the @multimail/mcp-server for automatic tool discovery. Both approaches integrate cleanly with LlamaIndex's ReAct agent pattern.

Built for LlamaIndex developers

Data-Grounded Email Responses

LlamaIndex retrieves relevant context from your knowledge base before composing emails. MultiMail handles the delivery infrastructure, ensuring that RAG-powered responses reach recipients reliably.

Oversight for AI-Generated Content

RAG-powered emails may contain hallucinated or incorrect information. MultiMail's gated_send mode lets humans review AI-generated emails before delivery, catching factual errors that retrieval alone cannot prevent.

Email as a Data Source

Use LlamaIndex to index your email history from MultiMail's API, then query it for context when composing new messages. Past email threads become part of your retrieval corpus for more contextual replies.

ReAct Agent Integration

LlamaIndex's ReAct agent can use MultiMail tools alongside retrieval tools. The agent reasons about when to search your knowledge base and when to send an email, combining both capabilities in a single workflow.

Contact-Aware Retrieval

Combine MultiMail's search_contacts and add_contact tools with LlamaIndex's retrieval to build agents that know who to email and what context is relevant for each contact.


Get started in minutes

Define MultiMail Tools for LlamaIndex
python
import requests
from llama_index.core.tools import FunctionTool

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

def send_email(to: str, subject: str, body: str, mailbox_id: str) -> str:
    """Send an email through MultiMail. In gated_send mode, this queues for human approval."""
    resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
        "mailbox_id": mailbox_id, "to": to,
        "subject": subject, "body": body
    })
    return str(resp.json())

def check_inbox(mailbox_id: str, limit: int = 10) -> str:
    """Check the inbox for recent messages."""
    resp = requests.get(
        f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
        headers=HEADERS, params={"limit": limit}
    )
    return str(resp.json())

def reply_email(message_id: str, body: str) -> str:
    """Reply to an email by message ID, maintaining thread context."""
    resp = requests.post(f"{MULTIMAIL_API}/reply", headers=HEADERS, json={
        "message_id": message_id, "body": body
    })
    return str(resp.json())

send_tool = FunctionTool.from_defaults(fn=send_email)
inbox_tool = FunctionTool.from_defaults(fn=check_inbox)
reply_tool = FunctionTool.from_defaults(fn=reply_email)

Create LlamaIndex FunctionTool wrappers around MultiMail API endpoints.

Build a RAG-Powered Email Agent
python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI

"cm"># Index your knowledge base
documents = SimpleDirectoryReader("./knowledge_base").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

"cm"># Create a query tool for context retrieval
from llama_index.core.tools import QueryEngineTool
knowledge_tool = QueryEngineTool.from_defaults(
    query_engine=query_engine,
    name="knowledge_base",
    description="Search the knowledge base for information to include in emails."
)

"cm"># Build the agent with both retrieval and email tools
llm = OpenAI(model="gpt-4o")
agent = ReActAgent.from_tools(
    tools=[knowledge_tool, send_tool, inbox_tool, reply_tool],
    llm=llm,
    verbose=True,
    system_prompt="You are an email assistant. Search the knowledge base for "
    "context before composing emails. The mailbox uses gated_send mode, so "
    "sent emails require human approval before delivery."
)

response = agent.chat("Reply to the latest email about pricing with our current rates")

Create a LlamaIndex ReAct agent that retrieves context from a knowledge base before composing and sending emails.

Index Email History for Retrieval
python
from llama_index.core import Document, VectorStoreIndex

def fetch_email_history(mailbox_id: str, limit: int = 100) -> list:
    """Fetch email history from MultiMail and create LlamaIndex documents."""
    resp = requests.get(
        f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
        headers=HEADERS, params={"limit": limit}
    )
    emails = resp.json().get("emails", [])
    documents = []
    for email in emails:
        doc = Document(
            text=f"From: {email[&"cm">#039;from']}\nTo: {email['to']}\n"
                 f"Subject: {email[&"cm">#039;subject']}\n\n{email['body']}",
            metadata={
                "message_id": email["message_id"],
                "date": email["date"],
                "from": email["from"]
            }
        )
        documents.append(doc)
    return documents

# Index email history
email_docs = fetch_email_history("your_mailbox_id")
email_index = VectorStoreIndex.from_documents(email_docs)
email_query = email_index.as_query_engine()

email_context_tool = QueryEngineTool.from_defaults(
    query_engine=email_query,
    name="email_history",
    description="Search past email conversations for context and prior commitments."
)

Fetch email history from MultiMail and index it with LlamaIndex for context-aware future replies.

Connect via MCP Server
python
import subprocess
import json

"cm"># The MCP server can be used alongside LlamaIndex tools
"cm"># Start the MCP server process
process = subprocess.Popen(
    ["npx", "-y", "@multimail/mcp-server"],
    env={"MULTIMAIL_API_KEY": "mm_live_your_api_key"},
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE
)

"cm"># For LlamaIndex, the recommended approach is FunctionTool wrappers
"cm"># around the REST API, as shown in the previous examples.
"cm"># The MCP server is ideal for MCP-native clients.

"cm"># Combine both approaches: MCP for discovery, FunctionTool for execution
agent = ReActAgent.from_tools(
    tools=[send_tool, inbox_tool, reply_tool, knowledge_tool],
    llm=llm,
    verbose=True
)

response = agent.chat("Check my inbox and draft replies using knowledge base context")

Use the MultiMail MCP server with LlamaIndex for automatic tool discovery.


Step by step

1

Create a MultiMail Account and API Key

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

2

Install Dependencies

Install LlamaIndex core, an LLM provider, and requests for calling the MultiMail API.

bash
pip install llama-index llama-index-llms-openai requests
3

Create MultiMail FunctionTools

Wrap MultiMail API endpoints as LlamaIndex FunctionTool objects for send_email, check_inbox, and reply_email.

4

Build Your Agent

Create a ReActAgent with your email tools and optionally a knowledge base query tool for RAG-powered email composition.

bash
agent = ReActAgent.from_tools(
    tools=[send_tool, inbox_tool, reply_tool],
    llm=OpenAI(model="gpt-4o"), verbose=True
)
5

Approve Pending Emails

If your mailbox uses gated_send mode (the default), review and approve pending emails in the MultiMail dashboard before they are delivered.


Common questions

Can I use LlamaIndex to index my MultiMail email history?
Yes. Fetch emails via the MultiMail REST API and create LlamaIndex Document objects from the results. Index them in a VectorStoreIndex for semantic search over your email history. This enables context-aware replies where the agent references prior conversations.
What oversight mode works best for RAG-powered auto-replies?
The monitored mode is often ideal for RAG-powered auto-replies. Emails send immediately but are visible in the dashboard for review. If your RAG pipeline is less mature, start with gated_send to review every reply before delivery and build confidence in the output quality.
How do I prevent hallucinated content in AI-generated emails?
LlamaIndex's retrieval grounds the agent in your actual data, reducing hallucination. MultiMail's gated_send mode adds a human review step as a safety net. Together, retrieval-augmented generation plus human oversight significantly reduces the risk of sending inaccurate information.
Does LlamaIndex have a native MultiMail integration?
There is no official llamaindex-multimail package in production. Integrate MultiMail by wrapping REST API calls as LlamaIndex FunctionTool objects, which integrate seamlessly with the ReAct agent pattern. You can also use the @multimail/mcp-server alongside LlamaIndex tools.
Can I combine email tools with other LlamaIndex data connectors?
Absolutely. LlamaIndex supports dozens of data connectors for databases, APIs, documents, and more. Your agent can query a knowledge base, search Notion, pull from a database, and then compose an email that synthesizes information from all these sources — all within a single ReAct agent loop.

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.