G-Mind: AI Email Automation Engine & Gmail Pipeline by Serhii LukashG-Mind: AI Email Automation Engine & Gmail Pipeline by Serhii Lukash

G-Mind: AI Email Automation Engine & Gmail Pipeline

Serhii Lukash

Serhii Lukash

The Problem

Knowledge workers spend 2–3 hours daily processing email — reading, triaging, and drafting responses. Generic AI tools bolt onto this workflow as a chat window. They don't integrate with your inbox, don't handle API failures, and certainly don't create the draft for you. The result: expensive context-switching that kills deep work.
The Cost: 40–60 hours per month per professional lost to low-value email triage.

What I Built

A production-grade Gmail automation engine that processes every incoming email end-to-end without human involvement. The moment a new message hits your inbox, it's classified by an LLM, a context-aware reply is generated, and a ready-to-send draft appears in Gmail — all in seconds, with zero polling, zero blocking, and a full fault-tolerance stack protecting every step.

Business Impact & ROI

Webhook response time: Immediate (204 ACK, async processing)
Retry logic: 5 attempts with exponential backoff
LLM fault tolerance: Dual-provider: GPT-4 primary + Claude 3 fallback
Dead-letter queue: Failed tasks stored and auto-retried hourly
Deduplication: 100% — duplicate emails never processed twice
Observability: Full Grafana stack: latency, queue depth, classification distribution

How It Works (The Value Pipeline)

1. Real-Time Webhook Trigger — Zero Polling

Rather than polling every N minutes, the system uses Gmail's watch() API with Google Cloud Pub/Sub. Every new email fires a push notification to the FastAPI webhook instantly. The handler verifies the OIDC token, deduplicates by Gmail message ID, and dispatches the processing task in under 500ms — then immediately returns 204 to acknowledge.

2. Three-Stage Celery Pipeline

Processing happens across three independent, horizontally scalable workers: classify_email (LLM categorizes as needs_reply, informational, or spam) → generate_ai_reply (context-aware draft generation) → create_gmail_draft (writes the reply directly to Gmail Drafts via API). Each stage is a separate Celery task with its own retry budget.
G-Mind Architecture Diagram
G-Mind Architecture Diagram

3. Dual-LLM Circuit Breaker

GPT-4 handles all primary classification and generation. On API failure or rate limit, a circuit breaker automatically routes to Anthropic Claude 3 — no manual intervention, no downtime. Claude handles under 5% of traffic in normal operation, but guarantees the pipeline never halts.

4. Dead-Letter Queue & Full Fault Tolerance

Every failure is caught, logged with a structured correlation ID, and written to a failed_tasks table. A Celery Beat scheduler retries failed tasks hourly and renews the Gmail watch subscription every 6 days before it expires — the system heals itself.

5. Production Observability

A full Grafana dashboard tracks API request latency (p99), Celery queue depth, task success/failure rate, PostgreSQL connection pool health, and email classification distribution — all with live Loki log streaming and structured JSON logging across every service.
Grafana Dashboard
Grafana Dashboard

Technical Foundation

API Layer: FastAPI + Uvicorn (async), Pydantic validation, OAuth 2.0 + OIDC token verification
Task Processing: Celery 5.3+ with Redis broker — separate queues: default, priority, dead_letter; Celery Beat for scheduled jobs
Database: PostgreSQL 15+ via SQLAlchemy 2.0 asyncpg — 4 tables with full indexing, ACID transactions, Alembic migrations
AI Stack: OpenAI GPT-4 (primary) + Anthropic Claude 3 (circuit-breaker fallback)
Gmail Integration: Gmail API watch() + Google Cloud Pub/Sub push subscriptions — real-time, not polling
Observability: Prometheus metrics, Grafana dashboards, Loki log aggregation, Promtail, Alertmanager
Security: Rate limiting, HTML sanitization (BeautifulSoup), input validation, OIDC token verification, Redis AOF persistence
Code Quality: Ruff lint + format, mypy type checking, pre-commit hooks, >80% test coverage

Who Benefits From This System

Executives & Founders: Eliminate inbox triage entirely. Every email is classified and a draft reply is waiting before you even open your inbox.
SaaS Platforms: Embed intelligent email processing as a first-class feature — support ticket triage, lead response automation, customer onboarding follow-ups.
Agencies & Consultants: Productize client communication workflows with a reliable, auditable AI pipeline that scales to any inbox volume.
Like this project

Posted Jun 5, 2026

Gmail automation engine: real-time webhooks, 3-stage Celery pipeline, dual-LLM circuit breaker (GPT-4 + Claude), dead-letter queue, full Grafana observability.