top of page
Search

📩 Intelligent Email Automation using LangGraph, FastAPI & Automation Anywhere

  • Writer: ARUN .N.K
    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:

  1. Categorize emails

  2. Retrieve relevant information

  3. Draft professional responses

  4. Proofread and validate the content

  5. 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

    ree


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:

  1. Use email automation package to fetch new emails

  2. Extract subject and body

  3. Use the REST Web Service action to call http://<your-server>/Email

  4. 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

    ree


    ree

    Result 2: Email Category - Customer_feedback

    ree

    Result 3: Email Category - Unrelated

    ree

    🧩 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

🚀 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


Bots-Hub

  • Linkedin
  • PCpXdqvUWfCW1mXhH1Y_98yBpgsWxuTSTofy3NGMo9yBTATDyzVkqU580bfSln50bFU
  • Instagram
  • Facebook
bottom of page