Skip to main content

Improve AI support email drafts with Gmail, OpenAI and PostgreSQL

Workflow preview

Workflow preview
100%
Improve AI support email drafts with Gmail, OpenAI and PostgreSQL preview
Open on n8n.io

1. Workflow Overview

Self learning feedback loop for AI customer support email drafts with Gmail, OpenAI and PostgreSQL Automatically compare AI generated email drafts against what your support team actually sent, lear...

Best for

  • Ticket Management automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.scheduletrigger, n8n-nodes-base.postgres, n8n-nodes-base.code, n8n-nodes-base.gmail, n8n-nodes-base.splitinbatches, n8n-nodes-base.if, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.lmchatopenai

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Vivekanand M.

Original n8n.io source

1.1 Workflow description

Title
Improve AI support email drafts with Gmail, OpenAI and PostgreSQL
Workflow name
Improve AI support email drafts with Gmail, OpenAI and PostgreSQL

Self-learning feedback loop for AI customer support email drafts with Gmail, OpenAI and PostgreSQL

Automatically compare AI-generated email drafts against what your support team actually sent, learn from the differences, and improve future drafts over time — without any model fine-tuning.


What this workflow does

This is the second workflow in a two-part customer support automation system. The first workflow generates AI draft replies for incoming support emails. This workflow closes the loop — it runs every 3 hours, checks which drafts were reviewed and sent, compares them against the original AI output, and stores the human-edited versions as training examples.

The more this workflow runs, the smarter the first workflow becomes. When generating future drafts, the similarity search surfaces past human-approved responses — so the AI progressively learns what good answers look like for your specific support context.


How it works

Step 1 — Watermark and scheduling Every run starts by fetching the last_processed_sent_at timestamp from the previous completed run. Only Gmail Sent emails newer than this timestamp are fetched, so nothing gets processed twice. On the first-ever run it defaults to 7 days ago.

Step 2 — Fetch and loop Sent emails are fetched from Gmail and processed one at a time. For each email, the full message body is retrieved via the Gmail API (the list endpoint only returns a preview snippet). The sent email's thread ID is matched against the ai_drafts table to find the corresponding AI draft.

Step 3 — Match and skip logic Three things skip an email without processing: no matching AI draft found (the team sent something manually), the draft was already processed in a previous run, or the fetch returns no results. Only genuine unprocessed matches continue.

Step 4 — AI comparison GPT-4o-mini compares the AI draft text against the human-sent text and returns a structured analysis: whether it was approved unchanged, the type of edit made (minor edits vs major rewrite), a plain English summary of what changed, and whether the edit implies missing or incorrect information in the knowledge base.

Step 5 — Store the correction If the human made any edits, the pair (original email + human response) is embedded using OpenAI text-embedding-3-small and saved to the corrections table. This table is what the first workflow searches using vector cosine similarity when assembling future draft prompts.

Step 6 — KB auto-update If the AI comparison flags that the human edit contained new information, the most relevant knowledge base entry for that category is fetched and rewritten by GPT-4o-mini to incorporate the new information. The previous answer is preserved in the previous_answer column for auditing.

Step 7 — Run log Each run is logged to feedback_run_log with counts of emails checked, corrections saved, KB updates made and any errors. This log also serves as the watermark source for the next run.


Setup steps

Prerequisites

  • Gmail account (same support inbox used by the main email workflow)
  • OpenAI API key
  • PostgreSQL database with pgvector extension and the full schema from Workflow 1 already applied
  • The main email automation workflow (Workflow 1) must be active and generating drafts

1. Apply the DB migration

Run the following against your existing database to add the columns this workflow needs:

ALTER TABLE ai_drafts
 ADD COLUMN IF NOT EXISTS email_embedding vector(1536),
 ADD COLUMN IF NOT EXISTS feedback_processed_at TIMESTAMPTZ,
 ADD COLUMN IF NOT EXISTS was_approved_as_is BOOLEAN DEFAULT FALSE;

ALTER TABLE corrections
 ADD COLUMN IF NOT EXISTS source TEXT DEFAULT 'feedback_loop',
 ADD COLUMN IF NOT EXISTS kb_updated BOOLEAN DEFAULT FALSE;

ALTER TABLE kb_data
 ADD COLUMN IF NOT EXISTS updated_by TEXT DEFAULT 'manual',
 ADD COLUMN IF NOT EXISTS previous_answer TEXT;

CREATE TABLE IF NOT EXISTS feedback_run_log (
 id SERIAL PRIMARY KEY,
 run_started_at TIMESTAMPTZ DEFAULT NOW(),
 run_completed_at TIMESTAMPTZ,
 last_processed_sent_at TIMESTAMPTZ,
 emails_checked INTEGER DEFAULT 0,
 approved_as_is INTEGER DEFAULT 0,
 corrections_saved INTEGER DEFAULT 0,
 kb_updates INTEGER DEFAULT 0,
 errors INTEGER DEFAULT 0,
 status TEXT DEFAULT 'running'
);

2. Configure credentials

Node Credential needed
Gmail - Fetch Sent Emails Gmail OAuth2
Gmail - Fetch Full Message Gmail OAuth2 (HTTP Request with OAuth)
All DB nodes PostgreSQL
OpenAI Chat Model - Compare OpenAI API
AI - Rewrite KB Answer OpenAI API
Generate Embedding - Human Sent OpenAI API

3. Check node connections

The splitInBatches loop node has two outputs — make sure they are connected correctly:

  • Output 0 (loop)DB - Match Thread ID
  • Output 1 (done)DB - Complete Run Log

All branch dead-ends (approved as-is, no KB update, KB updated) should feed back into the loop node's input to advance to the next item.

4. Activate

Toggle the workflow to active. It will run automatically on the 3-hour schedule. You can also trigger it manually to test.


How it connects to Workflow 1

Once corrections start accumulating in the corrections table, Workflow 1's similarity search (which queries this table using vector cosine distance) will begin surfacing relevant past human-approved responses when assembling draft prompts. No changes to Workflow 1 are needed — it queries the same table this workflow writes to.


Tech stack

  • n8n — workflow automation and scheduling
  • Gmail API — sent folder monitoring and full message fetch
  • OpenAI GPT-4o-mini — draft comparison and KB rewriting
  • OpenAI text-embedding-3-small — vector embedding for similarity search
  • PostgreSQL + pgvector — storing corrections and running cosine similarity queries

Who this is for

  • Teams already running an AI email draft workflow who want it to improve over time
  • Support operations that want human edits to automatically become training data
  • Anyone who wants a self-improving system without model fine-tuning or external ML infrastructure

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - ⏰ Schedule - Every 3 Hours

Type / Role
n8n-nodes-base.scheduleTrigger - scheduleTrigger
Config choices
Version 1.2

Block 2 - 🗄️ DB - Get Last Watermark

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 3 - ⚙️ Set Watermark

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 4 - 🗄️ DB - Start Run Log

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 5 - ⚙️ Carry Run Context

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 6 - 📧 Gmail - Fetch Sent Emails

Type / Role
n8n-nodes-base.gmail - gmail
Config choices
Version 2.2

Block 7 - 🔄 Loop - Sent Emails

Type / Role
n8n-nodes-base.splitInBatches - splitInBatches
Config choices
Version 3

Block 8 - 🗄️ DB - Match Thread ID

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 9 - ❓ IF - Draft Match Found?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 10 - ❓ IF - Already Processed?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 11 - 🤖 AI - Compare Draft vs Sent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3.1

Block 12 - OpenAI Chat Model - Compare

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.3

Block 13 - ⚙️ Parse AI Comparison

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 14 - ❓ IF - Approved As-Is?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 15 - 🗄️ DB - Mark Approved As-Is

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 16 - 🔢 Generate Embedding - Human Sent

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.4

Block 17 - ⚙️ Extract Embedding

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 18 - 🗄️ DB - Save Correction

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 19 - 🗄️ DB - Mark Draft Processed

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 20 - ❓ IF - KB Update Needed?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 21 - 🗄️ DB - Fetch KB Entry to Update

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 22 - 🤖 AI - Rewrite KB Answer

Type / Role
@n8n/n8n-nodes-langchain.openAi - openAi
Config choices
Version 2.1

Block 23 - 🗄️ DB - Update KB Entry

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 24 - 🗄️ DB - Mark KB Updated

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Showing the first 24 of 32 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Improve AI support email drafts with Gmail, OpenAI and PostgreSQL
Complexity advanced
Nodes 32
Categories Ticket Management, AI RAG
Author Vivekanand M
Published 10 Mar 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/13978/13978.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Improve AI support email drafts with Gmail, OpenAI and PostgreSQL do?

Self learning feedback loop for AI customer support email drafts with Gmail, OpenAI and PostgreSQL Automatically compare AI generated email drafts against what your support team actually sent, lear...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Ticket Management, AI RAG use case.