Smart Gmail Email Auto-Classification with Gemini AI - n8n Workflow

Use this advanced n8n workflow to automatically classify, label, and archive unread Gmail messages using the Gemini AI agent. Achieve true zero-inbox automation.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Users aiming for Zero-Inbox email management.
Automation specialists looking for advanced AI Agent n8n templates.
Businesses handling high volumes of segmented email traffic (invoices, meetings, inquiries).
Individuals wanting to leverage Google Gemini for complex data classification within an n8n node structure.

Overview

This sophisticated n8n workflow solves the problem of manual email sorting and labeling. By utilizing Google Gemini as an intelligent classification agent, this n8n template goes beyond simple keyword filtering. It analyzes the subject and body of new emails and, critically, uses Gmail tool nodes to access the sender's history and previously assigned labels. This historical context allows for highly accurate, pattern-based labeling (e.g., the 80% Majority Rule Bias defined in the system message).

This entire n8n automation ensures that once an email is classified and labeled, it is automatically archived, enabling a truly automated zero-inbox system. This specialized n8n node setup requires updating Gmail Label IDs within two code nodes, making the n8n workflow fully customizable for specific organizational categories.

How it Works

The n8n trigger starts the process by polling Gmail every minute for unread emails.


  1. Initial Check: The n8n workflow first verifies if the incoming email thread has already been processed and labeled by the AI (using the Check Label Existence n8n node). This prevents unnecessary processing.

  2. Conditional Flow: If unlabeled, the workflow continues to the AI Agent using the If Not Assigned n8n node.

  3. AI Agent Activation: The Email Classifier Agent (using the Gemini Chat Model) receives the email content and strict classification rules.

  4. Contextual Analysis (Tools): The agent first calls the Get Emails By Sender Email n8n tool to analyze past communication patterns and labels from that sender. If insufficient information is available, it uses the Get Emails By Label n8n tool to pull sample emails from potential categories for comparison.

  5. Classification & Output: Based on its decision, the agent outputs the final label as a structured JSON object. The If not None n8n node checks if a valid category was chosen.

  6. Label Mapping: The output is routed through a Code n8n node (Convert Label to Label ID) which translates the human-readable label name (e.g., "Expenses") into the specific Gmail label ID required by the API.

  7. Final Action: The Add label to thread n8n node applies the identified label. Finally, the Remove Inbox label from thread n8n node archives the email, completing the zero-inbox goal of this n8n automation.

Installation Guide


  1. Import: Import this n8n workflow JSON into your n8n instance.

  2. Credentials: Set up two primary credentials:

Gmail OAuth2: Required for the Gmail Trigger n8n node and the Gmail Tool nodes to read, write, and manage labels.
Google Gemini API (or Google PaLM API): Required for the Google Gemini Chat Model n8n node to power the AI classification agent.

  1. Create Labels: Create the mandatory six labels in your Gmail account: Meetings, Inquiries, Notify / Verify, Expenses, Orders / Deliveries, and Trash Likely.

  2. Retrieve Label IDs: Use the provided manual trigger (When clicking ‘Execute workflow’) connected to the Get Labels Info n8n node to retrieve the unique ID for each label you created (e.g., Label_1234567890).

  3. Update Code Nodes (CRITICAL): Paste these label IDs into the mapping objects within the Check Label Existence n8n node and the Convert Label to Label ID n8n node. This ensures the n8n workflow correctly references your Gmail account structure.

  4. Activate: Set the main Gmail Trigger n8n node to active to start polling your inbox.

Node Details

Gmail Trigger: The primary n8n trigger. Configured to poll every minute specifically for unread emails, ensuring the n8n workflow only targets new items.
Check Label Existence (Code n8n node): A critical control point. It uses custom JavaScript to verify if the email thread already contains one of the AI-managed labels, allowing the n8n workflow to skip re-classification if already processed.
Email Classifier Agent (LangChain Agent): The intelligence core of this n8n template. It uses the Google Gemini Chat Model and sophisticated system prompts detailing classification rules, prioritizing the use of Gmail tools to gather context.
Get Emails By Sender Email (Gmail Tool n8n node): This node is exposed as a tool to the AI Agent. It fetches historical communication from the sender, allowing the AI to leverage pattern recognition (like the 80% rule).
Structured Output Parser: Ensures the AI agent's output is clean and strictly formatted as JSON, guaranteeing reliable data for the subsequent n8n nodes.
Convert Label to Label ID (Code n8n node): This essential mapping node translates the human-readable label output from the AI (e.g., "Inquiries") into the unique Gmail ID required for API operations.
Add label to thread (Gmail n8n node): Applies the AI-determined label.
Remove Inbox label from thread (Gmail n8n node): Archives the email by removing the "INBOX" label, fulfilling the zero-inbox requirement of this n8n workflow.

Related n8n Workflows

Free

Nodes: 11 Nodes
Updated: December 26 2025
View all

Featured*