Gmail Auto-Labeling Workflow

1. Use Case Description

Problem: Email inbox chaos - hundreds of emails piling up without organization, making it difficult to find important messages and reducing productivity.

Solution: An AI-powered workflow that automatically categorizes emails by:

  1. Reading email content

  2. Analyzing with AI (GPT-4o-mini)

  3. Automatically applying the appropriate Gmail label

Source: This workflow was ported from N8N to AgenticFlow.


2. Goal

  • βœ… Automatically categorize emails based on content

  • βœ… Use AI to determine the most appropriate label

  • βœ… Apply existing Gmail labels

  • βœ… Keep inbox clean and organized


3. Use Cases & Triggers

When to Use?

Use Case
Description

Manual

Run manually when you want to label a specific email

Batch

Label multiple emails at once

Auto (Webhook)

Automatically trigger when new email arrives (requires Pub/Sub setup)

Trigger Options

Option 1: Manual Run

  • Open workflow β†’ Enter email subject β†’ Click Run

Option 2: Webhook + Gmail Push (Auto)

  • Gmail receives new email β†’ Pub/Sub β†’ Webhook β†’ Workflow runs automatically

  • See Webhook setup instructions below


4. Workflow Architecture


AgenticFlow Workflow: πŸ‘‰ https://agenticflow.ai/app/workspaces/30f2b04d-294f-4110-bf7e-4d405f2bea0c/workflows/6e17510a-16e5-4d43-9595-7b703620d38b/runarrow-up-right


6. How to Use

Step 1: Open Workflow

Access the workflow link above

Step 2: Enter Email Subject

  1. Open Gmail β†’ Find the email you want to label

  2. Copy the subject of that email

  3. Paste into the "Email Subject" field in the workflow

Example:

Step 3: Run Workflow

Click the "Run" button

Step 4: Check Results

  • View workflow output

  • Open Gmail β†’ Verify the email has been labeled


7. N8N Workflow JSON (Source)

Link: https://n8n.io/workflows/2740-basic-automatic-gmail-email-labelling-with-openai-and-gmail-api/arrow-up-right


8. Node Mapping: N8N β†’ AgenticFlow

N8N Node
AgenticFlow Equivalent

Gmail Trigger

Webhook Trigger + Pub/Sub (or Manual)

Gmail - read labels

MCP: gmail-list-labels

Gmail - get message

MCP: gmail-search-emails

OpenAI Chat Model

Ask AI (llm) node - GPT-4o-mini

Gmail - add label to message

MCP: gmail-add-label-to-email

Gmail - create label

(Not implemented)


9. Debugging Guide

Error 1: "Authentication failed: OAuth token expired"

Cause: Gmail MCP connection has expired

Solution:

  1. Go to AgenticFlow β†’ Connections

  2. Find your Gmail connection

  3. Click "Reconnect" or "Re-authorize"

  4. Sign in again with your Google account


Error 2: "Invalid id value" or "Thread ID invalid"

Cause: Using wrong type of ID

Solution:

  • Use Email Subject instead of Message ID

  • Workflow has been updated to use gmail-search-emails with subject


Error 3: "Error parsing arguments: instruction required"

Cause: MCP action missing instruction field

Solution:

  • Ensure each MCP action has instruction field in input_params

  • Example:


Error 4: Email not labeled

Possible causes:

  • Subject doesn't match exactly

  • Label ID doesn't exist

Solution:

  1. Check Step 2 output - was the email found?

  2. Check Step 3 output - what label_id did AI return?

  3. Verify label_id exists in Step 1 output


Error 5: Workflow doesn't auto-trigger

Cause: Webhook + Gmail Push not configured

Solution: See "Auto Trigger Setup" section below


10. Auto Trigger Setup (Optional)

To make the workflow run automatically when new emails arrive:

Step 1: Create Google Cloud Project

  1. Create a new project

  2. Enable Gmail API and Pub/Sub API

Step 2: Create Pub/Sub Topic

  1. Go to Pub/Sub β†’ Create Topic: gmail-notifications

  2. Add permission for [email protected] with role Pub/Sub Publisher

Step 3: Create Webhook Trigger in AgenticFlow

  1. Open workflow β†’ "Triggers" tab

  2. Create Trigger β†’ Webhook

  3. Copy the Webhook URL

Step 4: Create Push Subscription

  1. Go to Pub/Sub β†’ Create Subscription

  2. Select topic gmail-notifications

  3. Delivery type: Push

  4. Endpoint URL: Paste AgenticFlow Webhook URL

Step 5: Activate Gmail Watch

  1. Authorize Gmail API scope: https://mail.google.com/

  2. Send POST request:

⚠️ Note: Gmail Watch expires after 7 days and needs periodic renewal.


11. Output Schema


12. Summary

Item
Value

Workflow Name

Gmail Auto-Labeling Agent

Source

N8N Template #2740

AI Model

GPT-4o-mini

Input

Email Subject

Output

Label applied to email

MCP Connection

Gmail ()

Setup Time

~5 min (manual) / ~30 min (auto trigger)


Last updated