DEV Community

Cover image for πŸ“© Email Automation Agent β€” A Multi-Agent System to Read, Categorize, Extract & Auto-Escalate Emails
Nambirajan R S
Nambirajan R S

Posted on

πŸ“© Email Automation Agent β€” A Multi-Agent System to Read, Categorize, Extract & Auto-Escalate Emails

🧠 Reflection β€” What I Learned from the AI Agents Intensive

Before the AI Agents Intensive, I mostly saw LLMs as β€œsmart prediction models.” The course completely changed my perspective. I learned how agentic systems break complex workflows into smaller, specialized roles, how to design autonomous pipelines, and how to use structured prompting to create deterministic, production-ready systems.

The biggest mindset shift for me:

β€œDon’t build monolithic LLM flows. Build specialized agents that communicate.”

The course helped me understand:

  • How role-based agents dramatically improve accuracy
  • Why handoffs and memory matter for reliability
  • How to design for traceability, safety, and auditability
  • When to combine LLM extraction with regex + validation
  • How multi-agent workflows reduce hallucinations

This reflection directly influenced my project design below.

πŸš€** Project β€” Email Automation Agent**

A multi-agent system that processes incoming emails, extracts important details, and identifies urgent cases that require escalation β€” helping automate business communication workflows.

🎯 Project Goals

This agent performs three core tasks:

  1. Read & Categorize incoming emails

(e.g., Billing, Support, HR, Sales, Other)

  1. Extract key details
  • Invoice numbers
  • Amounts
  • Dates / due dates
  • Vendor / sender names
  • Action items
  • Attachments
  1. Detect urgent / escalated emails

(Overdue payments, angry tone, deadlines, SLA breaches)

All outputs are structured in machine-readable JSON, making the agent suitable for real automation systems.

Multi-Agent Architecture

Below is the architecture of the system β€” completely agent-driven:

+-----------+ +-------------+ +--------------+ +---------------+ +-------------+
| Email | ---> | Fetcher | ---> | Classifier | ---> | Extractor | ---> | Urgency |
| Provider | | Agent | | Agent | | Agent | | Detector |
| (Gmail) | +-------------+ +--------------+ +---------------+ +-------------+
| |
v v
+----------------+ +----------------+
| Orchestrator | <----> | Audit / Logger |
| / Router | +----------------+
+----------------+
|
decisions/actions: escalate, route, ticket

πŸ—οΈ Agents & Their Responsibilities
πŸ”Ή Fetcher Agent

Connects to Gmail/Outlook

Downloads unread emails

Normalizes into a standard message object

πŸ”Ή Classifier Agent

Labels the email: Billing, Support, HR, Sales, Other

Assigns a confidence score

Extracts intent keywords

πŸ”Ή Extractor Agent

Extracts structured fields:

invoice number

amount (value + currency)

due date

vendor name

action items

attachments

πŸ”Ή Urgency Detector Agent

Computes urgency (low/medium/high)

Detects deadlines, tone, escalation triggers

Gives reasons + a 0–1 score

πŸ”Ή Orchestrator Agent

Decides action based on the above agents:

escalate

create ticket

route to department

send human-review request

πŸ”Ή Audit / Logger Agent

Every decision is logged for:

Traceability

Debugging

Compliance

πŸ—‚οΈ Example JSON Output (Final Email Analysis)
{
"email_id": "msg_abc123",
"from": "supplier@example.com",
"subject": "Invoice #INV-2025-1104 β€” Payment overdue",
"category": {
"label": "Billing",
"confidence": 0.94
},
"extracted": {
"invoice_number": "INV-2025-1104",
"amount": { "value": 45200, "currency": "INR" },
"due_date": "2025-11-30",
"vendor_name": "ABC Supplies Pvt Ltd",
"action_items": [
{ "type": "payment_followup", "text": "Confirm payment schedule" }
]
},
"urgency": {
"level": "high",
"score": 0.92,
"reasons": ["overdue payment", "tone: urgent"]
},
"suggested_action": "escalate_to_finance"
}

πŸ“Œ Prompts Used (Production-Ready)
πŸ“ Classifier Prompt
You are a mail classifier. Analyze the email subject + body.
Return JSON:
{
"label": "",
"confidence": <0.0 to 1.0>,
"intent": [""]
}

πŸ“ Extractor Prompt
Extract: invoice_number, amount, due_date, vendor_name, attachments_present, action_items.
Return strict JSON. Use ISO date format (YYYY-MM-DD).
If unknown, use null.

πŸ“ Urgency Prompt
Classify urgency based on tone, deadlines, overdue items, complaints.
Return JSON:
{ "level": "low|medium|high", "score": 0.0-1.0, "reasons": [] }

πŸ§ͺ Evaluation Plan
Metrics used:
Component Metric Target
Classifier Accuracy β‰₯ 88%
Extraction F1 score β‰₯ 0.85
Urgency Precision β‰₯ 0.90
End-to-end Automation rate β‰₯ 70%
Example Results
Classification accuracy: 91.2%
Extraction F1: invoice_number 0.93, amount 0.89, due_date 0.86
Urgency precision: 0.91
Automation rate: 72.4%

πŸ–₯️ Implementation (Simplified Code Snippets)
πŸ”Έ Fetcher (Python)
def fetch_unread_messages(service):
results = service.users().messages().list(userId='me', q='is:unread').execute()
for m in results.get('messages', []):
msg = service.users().messages().get(userId='me', id=m['id'], format='raw').execute()
yield parse_raw(msg)

πŸ”Έ Orchestrator
def process_email(email_msg):
cls = classify_email(email_msg)
extracted = extract_fields(email_msg)
urgency = detect_urgency(email_msg, extracted)
action = decide_action(cls, urgency)
log_all(email_msg, cls, extracted, urgency, action)
return action

πŸ”Έ Action Logic
def decide_action(cls, urgency):
if urgency["level"] == "high" and urgency["score"] > 0.9:
return "escalate_to_finance"
if cls["label"] == "Support":
return "create_support_ticket"
return "route_to_ops"

πŸ§ͺ Test Emails (Used for Evaluation)
Test 1 β€” Urgent Billing
Subject: Payment overdue β€” invoice INV-2025-1104
Body: This invoice is overdue since Nov 30. Please treat as urgent.

Test 2 β€” Support Issue
Subject: App error when submitting form
Body: User getting 500 error while saving profile.

πŸ› οΈ Tools Used

Python

OpenAI GPT-4 / GPT-4.1

LangChain-style orchestration

Gmail API

Chroma DB (optional)

Docker

πŸ” Safety & Human Review

Low classifier confidence (< 0.6)

Missing mandatory fields

Ambiguous urgency
β†’ Routed to manual review to ensure safety.

πŸš€ Future Improvements

Add memory for vendor history

Add learning loop from human review corrections

Add sentiment + conflict detection

Add auto-reply with supervised templates

** Conclusion**

This project showed me the real power of multi-agent systems β€” clarity, modularity, interpretability, and reliability. Instead of a large monolithic LLM call, the pipeline becomes transparent and controllable.

The Email Automation Agent is a practical, production-ready workflow that can be used in finance, operations, HR, ticketing, and customer success teams to automate email-heavy processes.

Top comments (0)