AI-Powered Automatic Email Labeling and Categorization - n8n Workflow

Automate Gmail organization using AI. This advanced n8n workflow uses an OpenAI agent to read, categorize, and label incoming emails, creating new labels as needed. Get this smart n8n template today.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Automation Specialists: Developers looking for complex, integrated n8n templates featuring AI Agents.

  • Busy Professionals: Individuals receiving a high volume of emails who require sophisticated, hands-free organization.

  • Technical Users: Anyone wanting to leverage the power of n8n and the Langchain Agent n8n node for advanced decision-making processes.

  • Gmail Power Users: Those needing an automated system to enforce strict folder and label structures in their inbox.

Overview

Maintaining a clean and organized Gmail inbox can be a challenge, especially when dealing with high email volume. This powerful n8n workflow solves this by utilizing a sophisticated AI Agent to intelligently categorize every incoming email.

The central logic of this n8n template ensures that as soon as an email arrives, it is analyzed for content, sender, and subject. The AI agent, powered by the n8n node for OpenAI, decides whether to use an existing Gmail label or whether to dynamically create a new, appropriately structured sub-label.

This automatic labeling system significantly reduces manual sorting time and ensures consistent application of organizational rules, even handling exceptions like removing the 'inbox' label for less important emails (e.g., promotional material or advertisements). This n8n solution is an excellent example of using AI and the n8n platform to elevate standard operational tasks.

How it Works

This smart n8n workflow operates via a defined sequence, starting with an active n8n trigger:


  1. Incoming Email Trigger: The flow is initiated by the Gmail Trigger n8n node, which polls your Gmail account every five minutes for new, unread messages.

  2. Processing Start: The new email data is passed through a brief Wait n8n node (set to 1 second) before engaging the AI.

  3. AI Agent Activation: The core component, the Gmail labelling agent (a Langchain Agent n8n node), receives the email metadata. This agent uses the provided instructions to determine the next steps, leveraging its available tools.

  4. Tool Utilization & Analysis: The agent communicates with the OpenAI Chat Model to perform complex reasoning. It calls the 'Gmail - read labels' tool to see all existing categorization options. It then calls 'Gmail - get message' to fetch the full content necessary for accurate categorization.

  5. Decision Making: Based on the analysis, the agent decides which action to take. It prioritizes assigning the email to the most relevant existing label using the 'Gmail - add label to message' tool. If no appropriate label is found, the agent uses the 'Gmail - create label' tool to dynamically generate a new sub-label, often nested under an 'AI' parent label for structure, before assigning the message to it.

  6. Completion: The Langchain Agent n8n node finalizes the action by ensuring the email is properly labeled, thereby completing the cycle for that specific n8n trigger event.

Installation Guide

To deploy this comprehensive n8n template, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and paste it directly into your n8n instance using the 'New' -> 'Import from JSON' function.

  2. Set Up Gmail Credentials: Locate the four Gmail n8n node tools ('Gmail Trigger', 'Gmail - read labels', 'Gmail - get message', 'Gmail - add label to message', 'Gmail - create label'). For each, configure the required Gmail OAuth2 credentials. Ensure your Gmail API access allows for reading, creating, and modifying labels and messages.

  3. Set Up OpenAI Credentials: Locate the 'OpenAI Chat Model1' and 'Gmail labelling agent' nodes. Configure the necessary OpenAI API credentials (API Key) within the n8n node settings.

  4. Activate the n8n Trigger: Once all credentials are set, save the n8n workflow. Activate the Gmail Trigger n8n node to start the polling process. The automation will begin analyzing new emails immediately upon reception.

Node Details

Gmail Trigger: This crucial n8n trigger initiates the entire process, polling the specified Gmail account every 5 minutes for new emails to ensure timely processing.
Wait: A simple flow control n8n node that introduces a brief one-second delay before moving the data to the complex AI segment.
OpenAI Chat Model1: The language model provider used by the Langchain Agent. It handles the natural language understanding and decision-making logic critical for this n8n workflow.
Window Buffer Memory: This n8n node is connected to the Agent to provide conversational memory, using the incoming email ID as a unique session key to manage context during the reasoning process.
Gmail labelling agent (Langchain Agent): The central decision-maker. This powerful n8n node orchestrates the entire labeling process. It uses strict system instructions to analyze the email, utilize its defined Gmail tools, and output the necessary actions (label ID, message ID, new label name).
Gmail - read labels (Tool): Called by the Agent to retrieve a list of all existing Gmail labels, allowing the AI to decide if a suitable match already exists.
Gmail - get message (Tool): Called by the Agent to fetch the full content of the email (using the ID passed from the initial n8n trigger) for in-depth analysis.
Gmail - create label (Tool): Used only when the Agent determines that no existing label is sufficient. It dynamically creates a new, properly nested label.


  • Gmail - add label to message (Tool): The final action n8n node, responsible for applying the specific label ID(s) chosen by the AI Agent back to the original Gmail message.

Related n8n Workflows

Free

Nodes: 7 Nodes
Updated: December 26 2025
View all
Created by
Martijn Kerver
Martijn Kerver

Featured*