📩 Intelligent Email Automation using LangGraph, FastAPI & Automation Anywhere
- ARUN .N.K
- Aug 6
- 3 min read
Updated: Aug 7
In today's customer support landscape, handling large volumes of emails efficiently while maintaining personalization and quality is a major challenge. To solve this, I built a smart email automation system that leverages:
🧠 LangGraph from LangChain for dynamic agent workflows
⚡ FastAPI as a lightweight API server
🤖 Automation Anywhere to interact with emails and trigger REST APIs
In this blog, I'll walk you through the architecture, workflow, and implementation of the project. 🧱 Project Architecture
Here’s a high-level view of the components: 📧 Email Inbox (Gmail/O365)
⬇️
🤖 Automation Anywhere Bot
⬇️ REST API Call
🚀 FastAPI (Python Backend)
⬇️
🧠 LangGraph Workflow
⬇️
📤 Intelligent Email Response 💡 Why LangGraph?
LangGraph allows us to build stateful, multi-step workflows that can incorporate branching logic, loops, and memory — ideal for our email response generation use case. Instead of using a single LLM call, we're orchestrating a series of intelligent agents to:
Categorize emails
Retrieve relevant information
Draft professional responses
Proofread and validate the content
Send or retry based on quality
⚙️ Workflow Breakdown
1. Email Input via Automation Anywhere
Using Automation Anywhere, we created a bot that:
Connects to the email inbox
Reads new messages (subject + body)
Sends the email content to our FastAPI endpoint via a POST request
2. FastAPI: Gateway to the Workflow
@app.post("/Email")
async def process_email(request: Request):
data = await request.json()
subject = data.get("subject", "")
body = data.get("body", "")
state = {
"subject": subject,
"body": body,
...
}
graph_builder = Workflow(subject=subject, body=body)
result = graph_builder.app.invoke(state)
return {"data": result} This endpoint initializes the LangGraph workflow with the incoming email content and returns a structured response. 3. LangGraph Workflow Logic
LangGraph lets us model the process as a state machine. Each email moves through the following nodes:
✅ Initialize_emails: Wraps subject/body into a custom Email object
🧠 categorize_email: Classifies email into product_enquiry, feedback, complaint, or unrelated
🔍 construct_rag_queries: Builds RAG queries based on content
📚 retrieve_from_rag: Uses vector search to get relevant knowledge base data
✍️ email_writer: Drafts the response using retrieved information
👀 email_proofreader: Verifies clarity, tone, and completeness
🔁 Retry logic if the email doesn’t meet standards
📤 Generated_email: Final, sendable response

All nodes are backed by powerful LLM agents using LLama3 via Ollama, or any LLM of your choice. 🔄 Conditional Logic & Loops
LangGraph shines with conditional edges and looping: workflow.add_conditional_edges(
"email_proofreader",
nodes.must_rewrite,
{
"send": "Generated_email",
"rewrite": "email_writer",
"stop": "categorize_email"
}
) If the email is good → ✅ Send
If it needs improvements → 🔄 Rewrite
If we reach 3 trials → ⛔ Stop
This logic ensures quality control without manual intervention. 🧠 Agents (LLMs & RAG)
Here’s a quick overview of the agents:
Categorizer: Classifies intent using a prompt and structured output
RAG Query Generator: Translates email into 1–3 precise queries
Retriever: Queries a Chroma vector DB with nomic-embed-text
Email Writer: Drafts a polite, professional response
Proofreader: Verifies content and provides feedback
These are all built using LangChain's chaining and parsing tools, like PromptTemplate, ChatPromptTemplate, and with_structured_output. 🤖 Automation Anywhere Integration
Once the API is up and running, it's easy to plug in Automation Anywhere:
Use email automation package to fetch new emails
Extract subject and body
Use the REST Web Service action to call http://<your-server>/Email
Capture and handle the structured response (e.g., send it back to Gmail or log it)
This allows you to create a fully autonomous workflow without manual steps. 🔐 Security & Scaling
To make this production-ready:
✅ Use API authentication tokens
✅ Remove LLM key print() statements
🚀 Deploy with Gunicorn, Nginx, or serverless
📦 Use Docker for portability
📈 Add monitoring/logging (e.g., with Prometheus/Grafana) Sample Result
Here’s a simplified example of the output: Result 1: Email Category - Product_Enquiry


Result 2: Email Category - Customer_feedback

Result 3: Email Category - Unrelated

🧩 Technologies Used
Tech | Purpose |
LangGraph | Workflow management with LLMs |
FastAPI | API layer for external communication |
LangChain + Ollama | LLM orchestration and RAG |
Chroma DB | Vector store for internal documents |
Automation Anywhere | Email interaction & orchestration |
Code Repository
Github: https://github.com/nkarun/Intelligent-Email-Automation-using-LangGraph-FastAPI-Automation-Anywhere
Youtube:
🚀 Conclusion
By combining LangGraph's intelligent agent framework with FastAPI and Automation Anywhere, we built a powerful, modular, and scalable email automation system capable of reading, understanding, and replying to emails in a human-like, quality-controlled way.







Comments