Skip to main content

Smart Gmail auto-labeler with Gemini AI & sender history

Workflow preview

Workflow preview
100%
Smart Gmail auto-labeler with Gemini AI & sender history preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

n8n Gmail AI Auto Labeler An intelligent n8n workflow that automatically classifies and labels Gmail emails using Google Gemini AI , keeping your inbox organized with zero manual effort. This ...

Best for

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

Tools used

n8n-nodes-base.gmailtrigger, n8n-nodes-base.splitinbatches, @n8n/n8n-nodes-langchain.lmchatgooglegemini, n8n-nodes-base.gmailtool, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.outputparserstructured, n8n-nodes-base.gmail, n8n-nodes-base.code

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Muhammad Anas Farooq.

Original n8n.io source

1.1 Workflow description

Title
Smart Gmail auto-labeler with Gemini AI & sender history
Workflow name
Smart Gmail auto-labeler with Gemini AI & sender history

n8n Gmail AI Auto-Labeler

> An intelligent n8n workflow that automatically classifies and labels Gmail emails using Google Gemini AI, keeping your inbox organized with zero manual effort.

This workflow uses AI-powered classification to analyze email content, learn from sender patterns, and automatically apply appropriate labels while archiving processed emails.


How It Works

  1. Trigger: The workflow runs automatically every minute to check for new unread emails (or manually for bulk processing).
  2. Check for Existing Labels: Before processing, it verifies if the email already has an AI-assigned label to avoid duplicate processing.
  3. AI Classification: If unlabeled, the AI agent analyzes the email using:
    • Sender History Tool - Fetches up to 10 previous emails from the same sender to identify patterns
    • 80% Majority Rule - If 80%+ of sender's past emails have the same label, strongly prefers that category
    • Label Examples Tool - When uncertain, compares the email with existing examples from suspected categories
  4. Smart Decision: The AI returns a structured JSON response:
    {
      "label": "Category Name"
    }
    
    Or "None" if no category fits.
  5. Apply & Archive:
    • Label Applied → The workflow adds the appropriate Gmail label to the thread.
    • Auto-Archive → Removes the email from INBOX (archives it) to maintain zero-inbox.
  6. Loop: Processes the next email in the batch, ensuring all new emails are classified.

Requirements

  • Gmail OAuth2 Credentials - Connected Gmail account with API access.
  • Google Gemini API Key - Get it here
    • Free tier: 15 requests/minute
  • Gmail Labels - Must be created in Gmail exactly as listed:
    • Meetings
    • Income
    • Inquiries
    • Notify / Verify
    • Expenses
    • Orders / Deliveries
    • Trash Likely

How to Use

  1. Import the Workflow:

    • Copy the provided JSON file.
    • In your n8n instance → click Import Workflow → select the JSON file.
  2. Create Gmail Labels:

    • Open Gmail → Settings → Labels → Create new labels.
    • Use the exact names listed above (case-sensitive).
  3. Get Your Label IDs:

    • In the workflow, click "When clicking 'Execute workflow'" manual trigger.
    • Execute the "Get Labels Info" node only.
    • Copy each label's ID (format: Label_1234567890123456789).
  4. Update Code Nodes with Your Label IDs:

    Node 1: "Check Label Existence"

    const labelMap = {
      "Label_YOUR_ID_HERE": "Meetings",
      "Label_YOUR_ID_HERE": "Inquiries",
      "Label_YOUR_ID_HERE": "Notify / Verify",
      "Label_YOUR_ID_HERE": "Expenses",
      "Label_YOUR_ID_HERE": "Orders / Deliveries",
      "Label_YOUR_ID_HERE": "Trash Likely"
    };
    

    Node 2: "Convert Label to Label ID"

    const labelToId = {
      "Meetings": "Label_YOUR_ID_HERE",
      "Inquiries": "Label_YOUR_ID_HERE",
      "Notify / Verify": "Label_YOUR_ID_HERE",
      "Expenses": "Label_YOUR_ID_HERE",
      "Orders / Deliveries": "Label_YOUR_ID_HERE",
      "Trash Likely": "Label_YOUR_ID_HERE"
    };
    
  5. Set Up Credentials:

    • Gmail OAuth2 → Authorize your Gmail account in n8n.
    • Google Gemini API → Add your API key in n8n credentials.
  6. Test the Workflow:

    • Send yourself test emails with clear content (e.g., invoice, meeting invite).
    • Use the manual trigger to process them.
    • Verify labels are applied correctly.
  7. Activate for Auto Mode:

    • Toggle the workflow to Active.
    • New unread emails will be processed automatically every minute.

Notes

  • Dual Execution Modes:

    • Auto Mode - Gmail Trigger polls inbox every minute for unread emails (real-time processing).
    • Manual Mode - Use the manual trigger to bulk process existing emails (adjust limit in "Get many messages" node).
  • AI Learning from Patterns:

    • The workflow applies an 80% majority rule - if 80% or more of a sender's historical emails share the same label, the AI strongly prefers that category for new emails from that sender.
    • This creates intelligent sender-based routing over time.
  • Skip Already Labeled Emails:

    • The "Check Label Existence" node prevents re-processing emails that already have an AI-assigned label.
    • Ensures efficient execution and avoids duplicate work.
  • Structured AI Output:

    • Uses a Structured Output Parser to ensure the AI always returns valid JSON: { "label": "Category" }.
    • If uncertain, returns { "label": "None" } and the email stays in inbox.
  • Background Archiving:

    • After labeling, emails are automatically removed from INBOX (archived).
    • Maintains a zero-inbox workflow while preserving emails under their labels.
  • Rate Limits:

    • Google Gemini free tier: 15 requests/minute.
    • Adjust polling frequency if hitting limits.

Example Behavior

  • Minute 1: New invoice email arrives → AI fetches sender history → 85% were labeled "Expenses" → applies "Expenses" label → archives email.
  • Minute 2: Meeting invite arrives → No sender history → AI analyzes content (Zoom link, time) → applies "Meetings" label → archives email.
  • Minute 3: Promotional email arrives → AI compares with "Trash Likely" examples → applies label → archives email.
  • Minute 4: Already-labeled email detected → skipped silently.

Label Categories

Label Description
Meetings Calendar invites, Zoom/Meet links, appointments, scheduled events
Expenses Bills, invoices, receipts, payment reminders, subscription renewals
Income Payments received, payouts, deposits, earnings notifications
Notify / Verify Verification codes, login alerts, 2FA codes, account notifications
Orders / Deliveries Order confirmations, shipping updates, tracking numbers, deliveries
Inquiries Business outreach, sales proposals, partnerships, cold emails
Trash Likely Spam, newsletters, promotions, marketing blasts, ads

> If no category fits clearly, the email returns "None" and remains in the inbox.


Customization

  • Change Polling Frequency: Edit the "Gmail Trigger" node → pollTimesmode (e.g., every5Minutes).
  • Adjust Email Limit: Modify limit: 10 in "Get many messages" node for manual bulk processing.
  • Add Custom Labels: Create in Gmail → Get ID → Update both Code nodes + AI system prompt.
  • Modify 80% Rule: Edit the AI agent's system message to adjust the majority threshold.
  • Increase Tool Limits: Change limit: 10 in Gmail tool nodes to fetch more historical data.

Author: Muhammad Anas Farooq

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 - Gmail Trigger

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

Block 2 - Loop Over Items

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

Block 3 - Google Gemini Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 4 - Get Emails By Sender Email

Type / Role
n8n-nodes-base.gmailTool - gmailTool
Config choices
Version 2.1

Block 5 - Get Emails By Label

Type / Role
n8n-nodes-base.gmailTool - gmailTool
Config choices
Version 2.1

Block 6 - Email Classifier Agent

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

Block 7 - Structured Output Parser

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

Block 8 - Add label to thread

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

Block 9 - Check Label Existence

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

Block 10 - If Not Assigned

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

Block 11 - If not None

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

Block 12 - Get many messages

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

Block 13 - When clicking ‘Execute workflow’

Type / Role
n8n-nodes-base.manualTrigger - manualTrigger
Config choices
Version 1

Block 14 - Get Labels Info

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

Block 15 - Convert Label to Label ID

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

Block 16 - Remove Inbox label from thread

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

Block 17 - Workflow Overview

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 18 - Setup Instructions

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 19 - Gmail Trigger Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 20 - Label Check Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - AI Agent Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sender Tool Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 23 - Label Tool Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 24 - Label Application Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

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

3. Summary Table

Workflow Smart Gmail auto-labeler with Gemini AI & sender history
Complexity advanced
Nodes 25
Categories Ticket Management, AI Summarization
Author Muhammad Anas Farooq
Published 05 Dec 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/11518/11518.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 Smart Gmail auto-labeler with Gemini AI & sender history do?

n8n Gmail AI Auto Labeler An intelligent n8n workflow that automatically classifies and labels Gmail emails using Google Gemini AI , keeping your inbox organized with zero manual effort. This ...

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 Summarization use case.