Gmail Email Classification and Auto-Labeling with AI Agents - n8n Workflow

Use this powerful n8n workflow to classify and auto-label your incoming Gmail messages. It uses an AI Agent and Gmail tools to check for prior conversation history, ensuring accurate categorization (Marketing, Notification, To Respond) using specific n8n templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Overwhelmed Email Users: Individuals receiving hundreds of emails daily who need better organization.
Automation Specialists: Users seeking advanced examples of using the n8n AI Agent node with custom tools.
Technical Users: Those needing to implement sophisticated email routing logic using a robust n8n workflow.
n8n Users: Anyone looking for high-value n8n templates focusing on productivity and AI integration.

Overview

Managing a high volume of email is a major productivity drain. Standard filtering often fails to capture the nuance needed to distinguish between a critical service 'Notification', a 'To Respond' actionable item, and general 'Marketing' junk. This n8n workflow solves this by integrating advanced AI capabilities directly into your email management process.

This sophisticated n8n workflow utilizes an AI Agent configured with specific context and tools to analyze the email content, subject, and crucial header information. Critically, the AI Agent is empowered by the Gmail Tool n8n node to check for prior correspondence history with the sender, allowing it to accurately determine if an email is a cold pitch or part of an existing conversation. This context-aware classification ensures highly accurate labeling, saving significant time by ensuring that only truly actionable emails land in your priority folders. This specific n8n template provides a robust and customizable starting point for intelligent email filtering.

How it Works

This n8n workflow operates entirely in the background, performing the following sequence of operations:


  1. Start with the n8n Trigger: The process begins with the Gmail Trigger n8n node, which constantly polls your inbox for new incoming messages.

  2. Retrieve Full Context: The initial trigger only provides metadata. A subsequent Gmail n8n node uses the message ID to retrieve the complete email content, including all headers, plain text body, and recipient details.

  3. AI Orchestration (The Core Logic): The full email data is passed to the AI Agent n8n node. This agent is configured with the Anthropic Chat Model (using Claude) and a detailed system prompt outlining classification rules (To Respond, FYI, Notification, Marketing, etc.) and their corresponding Gmail Label IDs.

  4. Historical Context Check: The AI Agent leverages two embedded Gmail Tool n8n nodes (Get Email and Check Sent) to determine if there is any existing email history between the user and the sender. This step is vital for correctly classifying cold outreach versus active conversations.

  5. Structured Output: The AI Agent, using a Structured Output Parser, returns a clean JSON object containing the identified Label ID.

  6. Apply Final Label: The last Gmail n8n node receives the determined Label ID and uses the addLabels operation to immediately categorize the original incoming email in your Gmail account, completing the execution of the n8n workflow.

Installation Guide

To deploy this powerful n8n workflow, follow these steps:


  1. Import: Copy the provided JSON code and paste it into your n8n instance via the 'Import from JSON' function.

  2. Gmail Credentials Setup: The workflow requires Gmail OAuth2 credentials for the Gmail Trigger, the initial Gmail node, and the final Gmail1 node. Ensure these credentials have full read/write access to your mailbox.

  3. AI Service Credentials: You must configure the Anthropic Chat Model n8n node with valid Anthropic API credentials. Ensure the account can access the specified model (Claude Sonnet 4).

  4. Customize Labels (Optional): Review the detailed system prompt within the AI Agent n8n node. If your Gmail Label IDs differ from those defined in the prompt (e.g., Label_5151750749488724401 for 'To Respond'), update the prompt with your specific IDs.

  5. Activate: Save the n8n workflow and activate the Gmail Trigger to start processing incoming emails automatically. This robust n8n template is now live.

Node Details

Gmail Trigger (n8n node): Functioning as the starting n8n trigger, this polls Gmail every minute for new emails that require processing. It initiates the entire n8n workflow.
Gmail (Get): Fetches the complete metadata, headers, and body content of the email identified by the n8n trigger, providing comprehensive data for the AI analysis.
Anthropic Chat Model (n8n node): Serves as the Large Language Model backbone for the AI Agent, utilizing the 'claude-sonnet-4-20250514' model to perform the classification task based on the detailed rules provided in the system prompt.
AI Agent (n8n node): The core orchestrator. This specialized n8n node interprets the email data, determines the best course of action (classification), and uses the integrated GmailTool nodes (Get Email, Check Sent) to gather necessary contextual information regarding prior correspondence history with the sender.
Structured Output Parser (n8n node): Ensures that the output from the AI Agent is strictly formatted as a JSON object containing the required 'label ID', making the data reliably usable by the final node in this n8n workflow.
Gmail1 (Add Labels): The final action n8n node. It executes the addLabels operation, applying the specific Gmail Label ID determined by the AI Agent back to the original message in your inbox.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by

Featured*