This workflow corresponds to n8n.io template #6031 — we link there as the canonical source.
This workflow follows the Agent → Gmail recipe pattern — see all workflows that pair these two integrations.
The workflow JSON
Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →
{
"meta": {
"templateCredsSetupCompleted": true
},
"nodes": [
{
"id": "a31a7280-34ee-44bf-80e4-0a366ab0e649",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-560,
1392
],
"parameters": {
"color": 7,
"width": 736,
"height": 972,
"content": "## 1. Manual Trigger + Gmail Fetch + Label Checker\n[Read more about Manual Trigger node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.manualworkflowtrigger/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.manualTrigger)\n[Read more about Gmail Get node](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.gmail/draft-operations/#get-many-drafts)\n[Read more about Filter node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.filter/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.filter)\n\n\ud83d\uddb1\ufe0f Manual inbox processing\n\nThis section starts the workflow manually with a click, allowing you to process a batch of existing emails already in your Gmail inbox.\nIt fetches a defined number of messages (default: 50) and uses a Filter node to skip emails that already have labels \u2014 ensuring only unlabeled emails are passed to AI.\nYou can adjust the number of emails to fetch by changing the limit in the Gmail node."
},
"typeVersion": 1
},
{
"id": "69df5c49-b33d-4d71-a687-b60650f17297",
"name": "Sticky Note11",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1184,
1392
],
"parameters": {
"width": 588,
"height": 1628,
"content": "## \ud83d\udce8 Categorize and Label Existing Gmail Emails Automatically with GPT-4o mini\n\n## \ud83d\udc65 Who's it for\n\nThis workflow is perfect for individuals or teams who want to sort and label existing emails in their Gmail inbox \ud83d\uddc3\ufe0f using AI. Ideal for cleaning up unlabeled emails in bulk \u2014 no coding required!\n\nFor sorting incoming emails messages in your gmail inbox, please use this free workflow: [Categorize and Label Incoming Gmail Emails Automatically with GPT-4o mini](https://n8n.io/workflows/5595-categorize-and-label-gmail-emails-automatically-with-gpt-4o-mini/)\n\n## \ud83e\udd16 What it does\n\nIt manually processes a selected number of existing Gmail emails, skips those that already have labels, sends the content to an AI Agent powered by GPT-4o mini \ud83e\udde0, and applies a relevant Gmail label based on the email content. All labels must already exist in Gmail.\n\n## \u2699\ufe0f How it works\n\n\u25b6\ufe0f Manual Trigger \u2013 The workflow starts manually when you click \"Execute Workflow\".\n\n\ud83d\udce5 Gmail Get Many Messages \u2013 Pulls a batch of existing inbox emails (default: 50).\n\n\ud83d\udeab Filter \u2013 Skips emails that already have one or more labels.\n\n\ud83e\udde0 AI Agent (GPT-4o mini) \u2013 Analyzes the content and assigns a category.\n\n\ud83e\uddfe Structured Output Parser \u2013 Converts the AI output into structured JSON.\n\n\ud83d\udd00 Switch Node \u2013 Routes each email to the right label based on the AI result.\n\n\ud83c\udff7\ufe0f Gmail Nodes \u2013 Apply the correct Gmail label to the email.\n\n## \ud83d\udccb Requirements\n\nGmail account connected to n8n\n\nGmail labels must be manually created in your inbox beforehand\n\nLabels must exactly match the category names defined in the AI prompt\n\nOpenAI credentials with GPT-4o mini access\n\nn8n's AI Agent & Structured Output Parser nodes\n\n## \ud83d\udee0\ufe0f How to set up\n\nIn your Gmail account, create all the labels you want to use for categorizing emails\n\nOpen the workflow and adjust the email fetch limit in the Gmail node (e.g., 50, 100)\n\nConfirm that the Filter skips emails that already have labels\n\nDefine your categories in the AI Agent prompt \u2014 these must match the Gmail labels exactly\n\nIn the Switch Node, create a condition for each label/category\n\nEnsure each Gmail Label Node applies the correct existing label\n\nSave the workflow and run it manually whenever you want to organize your inbox \u2705\n\n## \ud83c\udfa8 How to customize the workflow\nAdd or remove categories in the AI prompt & Switch Node\n\nAdjust the batch size of emails to process more or fewer per run\n\nFine-tune the AI prompt to suit your inbox type (e.g., work, personal, client support)\n\n"
},
"typeVersion": 1
},
{
"id": "fd2816b9-05f1-4717-8a48-eee68b34e9ed",
"name": "Sticky Note12",
"type": "n8n-nodes-base.stickyNote",
"position": [
208,
1392
],
"parameters": {
"color": 7,
"width": 560,
"height": 972,
"content": "## 2. AI Categorization + Structured Output\n\n[Read more about AI Agent node](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.agent)\n[Read more about Structured Output Parser node](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.outputparserstructured/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=%40n8n%2Fn8n-nodes-langchain.outputParserStructured)\n\n\ud83e\udde0 Analyze and extract label\n\nThis section uses an AI Agent powered by GPT-4o mini to analyze the subject and body of each email and determine the appropriate label based on your custom prompt.\nThe AI's response is parsed by the Structured Output Parser, which converts it into clean JSON like:\n{ \"email_label\": \"work\" }\nMake sure the categories returned match exactly with the Gmail labels you've already created."
},
"typeVersion": 1
},
{
"id": "f36f9faf-daa7-47ac-8508-46dda10483e9",
"name": "Sticky Note13",
"type": "n8n-nodes-base.stickyNote",
"position": [
800,
1392
],
"parameters": {
"color": 7,
"width": 760,
"height": 1620,
"content": "## 3. Apply labels based on AI output\n[Read more about Switch node editing](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.switch/?utm_source=n8n_app&utm_medium=node_settings_modal-credential_link&utm_campaign=n8n-nodes-base.switch)\n[Read more about Gmail node editing](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.gmail/message-operations/#add-label-to-a-message)\n\n\ud83c\udff7\ufe0f Apply labels based on AI output\nThe Switch node uses email_label from the AI response to route each email to the correct label path.\nEach branch corresponds to one category.\nThen, each Gmail node applies the label that matches the category.\n\u26a0\ufe0f Make sure each route and label name matches exactly."
},
"typeVersion": 1
},
{
"id": "23ffde1f-a7ec-47e8-b4bb-6f1741e32074",
"name": "Give a Label AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
336,
1792
],
"parameters": {
"text": "=Topic: {{ $json.subject }}\nDescription: {{ $json.text }}\nSender: {{ $json.from.text }}",
"options": {
"systemMessage": "=**Your Role**\n\nYou are an intelligent email assistant responsible for sorting incoming messages. Your task is to analyze the content, subject, and sender of each email and assign it to the appropriate category.\n\n**Categories**\n\nCategories for sorting:\n\n-Work: Emails related to jobs, coworkers, meetings, projects, or professional tasks.\n\n-Personal: Messages from friends, family, or general personal life content.\n\n-Finance: Bank emails, invoices, receipts, bills, or payment confirmations.\n\n-Shopping: Order confirmations, promotions, receipts, or product updates from stores.\n\n-Travel: Booking confirmations, tickets, hotel or car rental details, itineraries.\n\n-Newsletters: Recurring marketing or informational emails from subscriptions or blogs.\n\n-Others: Relevant emails that don\u2019t clearly fit into any of the categories above.\n\n**Instructions**\n\n-For each email:\n\n-Analyze the content, subject, and sender\n\n-Assign it to the appropriate category\n\n-Add a label with the category\n\n-If you are unsure, do not label the message\n\n**Response Format**\n\nAlways return the result in JSON format:\n\n{\n \"email_label\": \"Shopping\"\n}\n\nIMPORTANT \u2013 the response must use the exact label format as listed (capitalized with spaces).\n"
},
"promptType": "define",
"hasOutputParser": true
},
"typeVersion": 1.8
},
{
"id": "1bd62e9b-2b93-4e79-99c0-f993a04079b3",
"name": "Label Checker Filter",
"type": "n8n-nodes-base.filter",
"position": [
0,
1792
],
"parameters": {
"options": {},
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "6c287523-4ae2-43c4-8f9a-faf756f01855",
"operator": {
"type": "string",
"operation": "notContains"
},
"leftValue": "={{ $json.labelIds[0] }}",
"rightValue": "Label"
}
]
}
},
"typeVersion": 2.2
},
{
"id": "c2d50354-0427-41bf-89f8-fb931bfcaefa",
"name": "Switch",
"type": "n8n-nodes-base.switch",
"position": [
832,
2096
],
"parameters": {
"rules": {
"values": [
{
"outputKey": "Work",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "10893f03-231e-4dba-bfdf-69530f908b4d",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Work"
}
]
},
"renameOutput": true
},
{
"outputKey": "Personal",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "23e430c2-ac8b-4fb5-bb5a-1fafb9e41b09",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Personal"
}
]
},
"renameOutput": true
},
{
"outputKey": "Finance",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "53698e8e-f038-405d-a1d3-274dba7fb2a2",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Finance"
}
]
},
"renameOutput": true
},
{
"outputKey": "Shopping",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "b3b7ffc6-d046-402f-8c9d-2e19d00b49a7",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Shopping"
}
]
},
"renameOutput": true
},
{
"outputKey": "Travel",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "4e0a670b-4017-4d83-b01a-78a79c55daf2",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Travel"
}
]
},
"renameOutput": true
},
{
"outputKey": "Newsletters",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "f0079792-cf4b-4c3e-8aed-48e2dddb4e05",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Newsletters"
}
]
},
"renameOutput": true
},
{
"outputKey": "Others",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "a0c39fdd-3312-4a34-8cd4-41a1a10f9149",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.output.email_label }}",
"rightValue": "Others"
}
]
},
"renameOutput": true
}
]
},
"options": {}
},
"typeVersion": 3.2
},
{
"id": "1300cdf3-5b22-45ee-af81-5d94723c10e3",
"name": "Work",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
1712
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "cb4ea5c6-a638-425d-8b45-a8621a8a0e33",
"name": "Personal",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
1872
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "1db6e7ff-6770-4a66-8f05-3fae33b597f0",
"name": "Finance",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
2032
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "19233ac1-848d-4d3b-abca-4022f3ffa17c",
"name": "Shopping",
"type": "n8n-nodes-base.gmail",
"position": [
1152,
2176
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "99b15563-82da-4321-8ff0-60c21861f764",
"name": "travel",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
2288
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "bfa96076-0b6f-4c39-8f75-e2e6312d2831",
"name": "Newsletters",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
2464
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "2f2937a3-22b8-4a71-920d-1393056eee25",
"name": "Others",
"type": "n8n-nodes-base.gmail",
"position": [
1312,
2656
],
"parameters": {
"messageId": "={{ $('Gmail Trigger').item.json.id }}",
"operation": "addLabels"
},
"typeVersion": 2.1
},
{
"id": "bf124f1b-0167-479c-a91f-0d759f3f7e60",
"name": "When clicking \u2018Execute workflow\u2019",
"type": "n8n-nodes-base.manualTrigger",
"position": [
-496,
1792
],
"parameters": {},
"typeVersion": 1
},
{
"id": "3adb0acd-3a61-451b-b93a-bebf60b9c213",
"name": "Get many messages",
"type": "n8n-nodes-base.gmail",
"position": [
-256,
1792
],
"parameters": {
"filters": {},
"operation": "getAll"
},
"credentials": {
"gmailOAuth2": {
"name": "<your credential>"
}
},
"typeVersion": 2.1
},
{
"id": "12bd7049-1364-4339-af20-f50a765fda3b",
"name": "Structured Output Parser",
"type": "@n8n/n8n-nodes-langchain.outputParserStructured",
"position": [
544,
2032
],
"parameters": {
"jsonSchemaExample": "{\n \"email_label\": \"business\"\n}"
},
"typeVersion": 1.2
},
{
"id": "84d8bff0-4363-4fd1-b830-68c23952d7d0",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
288,
2032
],
"parameters": {
"model": {
"__rl": true,
"mode": "list",
"value": "gpt-4o-mini",
"cachedResultName": "gpt-4o-mini"
},
"options": {}
},
"typeVersion": 1.2
}
],
"connections": {
"Switch": {
"main": [
[
{
"node": "Work",
"type": "main",
"index": 0
}
],
[
{
"node": "Personal",
"type": "main",
"index": 0
}
],
[
{
"node": "Finance",
"type": "main",
"index": 0
}
],
[
{
"node": "Shopping",
"type": "main",
"index": 0
}
],
[
{
"node": "travel",
"type": "main",
"index": 0
}
],
[
{
"node": "Newsletters",
"type": "main",
"index": 0
}
],
[
{
"node": "Others",
"type": "main",
"index": 0
}
]
]
},
"Get many messages": {
"main": [
[
{
"node": "Label Checker Filter",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "Give a Label AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Label Checker Filter": {
"main": [
[
{
"node": "Give a Label AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Give a Label AI Agent": {
"main": [
[
{
"node": "Switch",
"type": "main",
"index": 0
}
]
]
},
"Structured Output Parser": {
"ai_outputParser": [
[
{
"node": "Give a Label AI Agent",
"type": "ai_outputParser",
"index": 0
}
]
]
},
"When clicking \u2018Execute workflow\u2019": {
"main": [
[
{
"node": "Get many messages",
"type": "main",
"index": 0
}
]
]
}
}
}
Credentials you'll need
Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.
gmailOAuth2
For the full experience including quality scoring and batch install features for each workflow upgrade to Pro
About this workflow
This workflow is perfect for individuals or teams who want to sort and label existing emails in their Gmail inbox 🗃️ using AI. Ideal for cleaning up unlabeled emails in bulk — no coding required!
Source: https://n8n.io/workflows/6031/ — original creator credit. Request a take-down →
Related workflows
Workflows that share integrations, category, or trigger type with this one. All free to copy and import.
Typeform IA - YT. Uses typeformTrigger, agent, lmChatOpenAi, toolWorkflow. Event-driven trigger; 75 nodes.
Transcript Evalu8r V2 is a robust browser-based transcript analysis tool powered by Deepgram’s speech-to-text API and built into an n8n workflow template. This release introduces full in-browser audio
Transcript Evalu8r is an AI-powered transcript analysis workflow that automates the processing, visualization, and evaluation of transcribed conversations. This n8n workflow template is designed to he
Automated Book Generation System. Uses googleSheetsTrigger, gmail, lmChatOpenAi, outputParserStructured. Event-driven trigger; 43 nodes.
Generate personalized sales leads, ready-to-send, HTML-formatted emails, and send them automatically. This workflow is ideal for sales professionals, marketers, and business development teams aiming t