LangChain
Email Assistant with LangChain
Build an AI email assistant that searches your inbox, drafts replies, and manages email workflows — using LangChain.
emailautomationproductivityassistant
Working Code
from langchain_openai import ChatOpenAIfrom langchain_core.tools import tool
@tooldef search_emails(query: str, limit: int = 5) -> str: """Search the inbox for emails matching a query.""" results = email_client.search(query, max_results=limit) return "\n\n".join( f"From: {e.sender}\nSubject: {e.subject}\nDate: {e.date}\nPreview: {e.body[:200]}" for e in results )
@tooldef draft_email(to: str, subject: str, body: str) -> str: """Create an email draft.""" draft_id = email_client.create_draft(to=to, subject=subject, body=body) return f"Draft created (ID: {draft_id}). Review before sending."
model = ChatOpenAI(model="gpt-4o")model_with_tools = model.bind_tools([search_emails, draft_email])response = model_with_tools.invoke([ ("system", "You are an email assistant. Search emails to find context, then help draft professional replies. Always create drafts — never send directly."), ("user", "Find the latest email from the marketing team and draft a reply confirming the deadline"),])Step by Step
1
Install dependencies
Install LangChain and the required tools for this use case.
2
Define your tools
Create the domain-specific tool functions your agent will use to interact with external services.
3
Create the agent and run
Initialize the LangChain agent with your tools, set the system prompt, and execute a query.
Build with other frameworks
Ready to build with LangChain?
Generate a production-ready project with LangChain pre-configured — FastAPI + Next.js, auth, streaming, and more.
Get StartedReady to build your first production AI agent?
Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.