This n8n workflow builds a self-improving AI agent for handling email responses. It integrates Gmail for incoming messages, uses an AI agent with a Supabase vector store for knowledge retrieval, drafts replies based on dynamic prompts, and evaluates confidence scores. High-confidence drafts are sent automatically, while low-confidence ones are routed to Google Sheets for human review. Feedback from Sheets triggers updates to the prompt or knowledge base, enabling the agent to learn and refine its responses over time. The workflow supports OpenAI and OpenRouter models, with structured output parsing and conditional routing for efficiency.
• Triggers on new Gmail emails, extracts content, and uses an AI agent with company knowledge from Supabase to draft a response.
• Assesses confidence; auto-sends high-confidence replies or saves drafts to Google Sheets for human feedback.
• Monitors Sheets for updates, extracts feedback via AI, and applies changes—either refining the dynamic prompt or adding new info to the vector store.
• Loops back improvements to enhance future responses without manual reconfiguration.
Setup takes about 20-30 minutes, focused on credentials and basic resource creation (e.g., Google Sheet and Supabase table). Detailed node-level guidance is in the workflow's sticky notes—clone the JSON for plug-and-play nodes, then edit only essentials like credentials and IDs. No need to rebuild nodes; they're pre-connected and ready.
Gmail Setup
OpenAI API Key
gpt-4o-mini
for cost efficiency; adjust temperature in options for response creativity.OpenRouter API Key
gpt-4o-mini
in the node for consistency.Supabase Connection
documents
with vector support (enable pgvector extension).Google Sheets Setup
1YDzwYd5LdTnSQlFM5YrDhhbCnNH7oHVvNg6x3mYu8I4
) or create one with tabs: "Dynamic Var" (for prompts) and "Feedback" (columns: Id, Original Email, Draft, Human Feedback, Status). Replace the documentId in nodes with your sheet's ID.Clone the provided JSON directly into n8n—all nodes (triggers, agents, parsers, switches) are pre-wired and functional. No re-setup required beyond the steps above. Key editable fields (detailed in sticky notes):
AI Agent Node: System Message
Customize the prompt template for response style (e.g., add rules for tone or specific facts). Default handles factual, concise replies.
Structured Output Parser Node: JSON Schema
Adjust if adding output fields (e.g., include "suggested_followup" alongside "final_response" and "resolution_score").
If Node: Conditions
Tweak the confidence threshold (default: <4 routes to review) based on your risk tolerance.
Information Extractor Node: Prompt
Refine the feedback analysis (e.g., prioritize certain feedback types like "add facts" vs. "update prompt").
Test with a sample email: Send a query to your Gmail, review in Sheets if low-confidence, provide feedback, and watch the agent improve on the next run.
This workflow turns email handling into an adaptive, low-maintenance system—ideal for support teams or busy inboxes. For tweaks, reference the sticky notes or experiment in a test clone!