Skip to content

hira299/AI-Autonomous-Email-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Autonomous Stateful Email Agent

Stateful, event-driven AI agent that autonomously handles incoming email conversations. Maintains full conversation memory per thread, generates context-aware replies via a Groq-powered LLM, and responds directly to the sender with zero manual input.

Infrastructure cost: $0


Demo

▶ Loom Walkthrough


The Problem

Manual email screening and Tier-1 inquiry resolution creates high operational overhead and response latency — especially across time zones. Responding consistently to every recruiter or client email is time-consuming and does not scale.


Architecture

Gmail Trigger (every 60s)
  → Extract Email Fields
  → Load Chat History (Google Sheets — by thread ID)
  → Build Messages Array (system prompt + history + new message)
  → AI Response Generator (Groq / Llama 3)
  → Send Gmail Reply          → Save AI Reply (Google Sheets)
  → Mark Message as Read      → Save User Message (Google Sheets)

Engineering Highlights

Stateful conversation persistence Every exchange is stored in Google Sheets keyed by Gmail thread ID. The full conversation history is loaded and prepended to every LLM call — the agent never loses context across multiple emails in the same thread.

Idempotent execution Each workflow run loads, processes, and persists atomically. Re-runs on the same message do not produce duplicate replies or duplicate records.

Parallel persistence User message and AI reply are written to Google Sheets simultaneously after each exchange using parallel output branches — not sequentially.

Zero-cost infrastructure Self-hosted n8n exposed via Cloudflare Tunnel. No paid API gateway, no cloud functions, no container hosting costs.

Polling tradeoff Gmail is polled every 60 seconds. This introduces a maximum 60-second response latency in exchange for simplicity and reliability over webhook-based triggering, which requires persistent public endpoints.


Memory Schema

Conversation history is stored in Google Sheets with the following structure:

Column Description
thread_id Gmail thread ID — groups all emails in a conversation
role user or assistant
content Full message body
timestamp ISO 8601 timestamp
sender Sender email address

Failure Handling

Failure Behaviour
Google Sheets API timeout Workflow retries on next poll cycle; no duplicate reply is sent as message remains unread
Groq API rate limit n8n error handler catches the failure; message remains unread for retry
Empty reply from LLM Conditional check before send step; reply is skipped, message flagged for review

Setup

Prerequisites

  • n8n instance (self-hosted via Docker recommended)
  • Gmail account with OAuth2 credentials configured in n8n
  • Google Sheets with the memory schema above
  • Groq API key (free tier sufficient)
  • Cloudflare Tunnel (optional — for public webhook access)

Steps

# 1. Import workflow
# Import AI_Email_Agent.json into your n8n instance

# 2. Connect credentials
# Gmail OAuth2, Google Sheets, Groq API

# 3. Configure
# Update the Google Sheets node to your sheet ID
# Replace the system prompt in Build Messages Array with your own context

# 4. Activate
# Enable the workflow in n8n

.env.example

GROQ_API_KEY=your_key_here

Stack

  • Orchestration: n8n (self-hosted)
  • LLM: Groq / Llama 3
  • Memory: Google Sheets
  • Email: Gmail API (OAuth2)
  • Tunnel: Cloudflare Tunnel

License

MIT

System Prompt

The system prompt lives inside the Build Messages Array code node. Replace it with your own background, experience, and instructions to make the agent respond as you.

About

Stateful AI email agent that autonomously handles incoming conversations. Maintains full thread memory in Google Sheets, generates context-aware replies via Groq/Llama 3, and responds with zero manual input. Self-hosted on n8n. Infrastructure cost: $0.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors