LangChain

Give your LangChain agent its own email inbox

Getting started

LangChain is the most widely used framework for building LLM-powered agents. The langchain-agentmail package wraps the AgentMail SDK as standard LangChain tools, plus a document loader and a retriever — so a LangGraph agent can send, reply, draft, label, and search email through a real inbox without any glue code.

The full reference lives on the LangChain docs site: docs.langchain.com/oss/python/integrations/providers/agentmail.

Use cases

  • Give agents their own inboxes: Provision a dedicated email address per agent so it can send and receive mail independently.
  • Triage and reply: Read recent threads, summarize what’s new, and reply inside the same thread with the right In-Reply-To headers.
  • Stage and schedule sends: Use the draft tools to compose iteratively or schedule a delivery time via send_at.
  • RAG over email: Load messages as LangChain Documents and index them into a vector store for semantic search across the inbox.

Prerequisites

  1. An AgentMail account with an API key from the AgentMail Console.
  2. Python 3.10+ and a LangChain-compatible model provider (e.g. an OPENAI_API_KEY or ANTHROPIC_API_KEY).

Setup

Install the integration package:

$pip install langchain-agentmail

Set your API key:

$export AGENTMAIL_API_KEY="your-api-key"

Quickstart

Build a ReAct agent with the full toolkit in a few lines:

Python
1from langchain_openai import ChatOpenAI
2from langgraph.prebuilt import create_react_agent
3
4from langchain_agentmail import AgentMailToolkit
5
6toolkit = AgentMailToolkit.from_api_key()
7agent = create_react_agent(
8 ChatOpenAI(model="gpt-4o-mini"),
9 tools=toolkit.get_tools(),
10)
11
12result = agent.invoke(
13 {"messages": [("user", "Summarize my most recent email thread.")]}
14)
15print(result["messages"][-1].content)

You can also pull a single tool in if you don’t need the whole toolkit:

Python
1from langchain_agentmail import AgentMailClient, AgentMailSendTool
2
3send = AgentMailSendTool(client=AgentMailClient())
4send.invoke({
5 "inbox_id": "ib_...",
6 "to": "alice@example.com",
7 "subject": "Ping",
8 "text": "Hello from my agent.",
9})

Available tools

The toolkit exposes one tool per AgentMail operation.

Inbox and thread management

ToolDescription
agentmail_list_inboxesList inboxes the account owns
agentmail_create_inboxCreate a new inbox (random or custom username)
agentmail_list_threadsList threads across an inbox with label / time filters
agentmail_get_threadPull every message in a thread

Message operations

ToolDescription
agentmail_list_messagesList messages inside an inbox
agentmail_get_messageFetch one message with its full plain-text body
agentmail_send_messageSend a new email
agentmail_reply_to_messageReply inside an existing thread (with optional reply-all)
agentmail_update_message_labelsAdd or remove labels (archive, follow-up, etc.)
agentmail_get_attachmentGet a presigned URL to download a message attachment

Draft management

ToolDescription
agentmail_create_draftStage a draft (with optional send_at for scheduled delivery)
agentmail_update_draftRevise an existing draft
agentmail_send_draftSend a previously created draft
agentmail_delete_draftPermanently delete a draft

RAG over an inbox

AgentMailLoader streams messages as LangChain Documents — one per message, plain-text body as page_content, sender / subject / labels / thread / attachment metadata on metadata. Pair it with any vector store for semantic search:

Python
1from langchain_core.vectorstores import InMemoryVectorStore
2from langchain_openai import OpenAIEmbeddings
3
4from langchain_agentmail import AgentMailLoader
5
6docs = AgentMailLoader(inbox_id="ib_...", limit=200).load()
7store = InMemoryVectorStore.from_documents(docs, OpenAIEmbeddings())
8retriever = store.as_retriever(search_kwargs={"k": 5})
9
10retriever.invoke("Q3 invoice from acme")

For a quick keyword search without embeddings, use the bundled AgentMailRetriever instead.

Inbound email via webhooks

The webhooks extra ships a FastAPI router with svix-compatible signature verification so a LangGraph agent can react to inbound mail:

$pip install 'langchain-agentmail[webhooks]'
Python
1from fastapi import FastAPI
2from langchain_agentmail.webhooks import AgentMailEvent, create_fastapi_router
3
4async def on_event(event: AgentMailEvent) -> None:
5 if event.event_type == "message.received":
6 # drive your LangGraph agent here
7 ...
8
9app = FastAPI()
10app.include_router(
11 create_fastapi_router(on_event), # reads AGENTMAIL_WEBHOOK_SECRET
12 prefix="/agentmail",
13)

Resources