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
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.
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)
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.
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 | 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 |
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
- Orchestration: n8n (self-hosted)
- LLM: Groq / Llama 3
- Memory: Google Sheets
- Email: Gmail API (OAuth2)
- Tunnel: Cloudflare Tunnel
MIT
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.