{
  "name": "Smart Gmail auto-labeler with Gemini AI & sender history",
  "nodes": [
    {
      "id": "2edf070f-9e4b-46eb-9b78-77a9bc05ea55",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -896,
        208
      ]
    },
    {
      "id": "8aa5c340-cff7-4714-ac66-90f1b6df2560",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -656,
        64
      ]
    },
    {
      "id": "5e9c9bd9-b9a7-4635-91ab-f7a021128c11",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -48,
        336
      ]
    },
    {
      "id": "f3503d1c-03b7-4204-8b90-34cc9b58d1d8",
      "name": "Get Emails By Sender Email",
      "type": "n8n-nodes-base.gmailTool",
      "position": [
        144,
        336
      ]
    },
    {
      "id": "4cd2fddd-e18a-4388-ac8f-9b69d73c172c",
      "name": "Get Emails By Label",
      "type": "n8n-nodes-base.gmailTool",
      "position": [
        304,
        336
      ]
    },
    {
      "id": "795a9f38-d015-45d8-bcd8-425fc6b5e004",
      "name": "Email Classifier Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        112,
        64
      ]
    },
    {
      "id": "e6acfbf9-5306-4091-8dee-dd0916cf0819",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        480,
        336
      ]
    },
    {
      "id": "9169d5e2-109a-4a43-bbab-0323450e976f",
      "name": "Add label to thread",
      "type": "n8n-nodes-base.gmail",
      "position": [
        928,
        48
      ]
    },
    {
      "id": "311ca8a2-3c7b-46f2-aa89-8c6f8e40beca",
      "name": "Check Label Existence",
      "type": "n8n-nodes-base.code",
      "position": [
        -400,
        80
      ]
    },
    {
      "id": "4bd26bec-57f3-45ce-a224-1c776bc8b60a",
      "name": "If Not Assigned",
      "type": "n8n-nodes-base.if",
      "position": [
        -192,
        80
      ]
    },
    {
      "id": "8bc95c18-b697-4710-9b79-86023a9c58f6",
      "name": "If not None",
      "type": "n8n-nodes-base.if",
      "position": [
        464,
        64
      ]
    },
    {
      "id": "49ab9a45-b23d-4a81-a4da-f5afca5dd84d",
      "name": "Get many messages",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -896,
        -48
      ]
    },
    {
      "id": "f7c194b0-a74b-4252-b418-4c9ebc936800",
      "name": "When clicking ‘Execute workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1104,
        -48
      ]
    },
    {
      "id": "2e244f4c-2b4e-41bb-a453-132e1bb0e5ab",
      "name": "Get Labels Info",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -656,
        608
      ]
    },
    {
      "id": "6de8b305-9f91-420e-9186-21c3b542e18e",
      "name": "Convert Label to Label ID",
      "type": "n8n-nodes-base.code",
      "position": [
        704,
        48
      ]
    },
    {
      "id": "90961646-64c4-4328-80f3-d8f39a5dc656",
      "name": "Remove Inbox label from thread",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1136,
        48
      ]
    },
    {
      "id": "dda08ae5-40a6-45cc-990c-1a0e5204e998",
      "name": "Workflow Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1664,
        -224
      ],
      "parameters": {
        "width": 372,
        "height": 290,
        "content": "## 🎯 WORKFLOW OVERVIEW\n\n**Auto-classifies & labels Gmail using AI**\n\n✅ Checks inbox every minute for unread emails\n✅ Uses Google Gemini AI to classify emails\n✅ Applies labels based on content + sender"
      }
    },
    {
      "id": "0e73909d-025c-4020-97d3-3f96ddaddcb3",
      "name": "Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1664,
        112
      ],
      "parameters": {
        "width": 400,
        "height": 684,
        "content": "## 🔧 SETUP STEPS (DO THIS FIRST!)\n\n### 1️⃣ Create Gmail Labels\nCreate these exact labels in Gmail:\n- Meetings\n- Inquiries  \n- Notify / Verify\n- Expenses\n- Orders / Deliveries\n- Trash Likely\n\n### 2️⃣ G"
      }
    },
    {
      "id": "6ab0101b-b93d-4332-842d-f1ab764c7632",
      "name": "Gmail Trigger Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1216,
        144
      ],
      "parameters": {
        "width": 280,
        "height": 220,
        "content": "## ⚡ TRIGGER\n\nPolls Gmail every 1 minute\nOnly fetches UNREAD emails\n\n⚙️ To change frequency:\nEdit pollTimes → mode"
      }
    },
    {
      "id": "061329fd-d9b1-4316-90af-003b201e3a5b",
      "name": "Label Check Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        -304
      ],
      "parameters": {
        "width": 380,
        "height": 340,
        "content": "## 🛡️ SKIP IF ALREADY LABELED\n\n**Check Label Existence** node:\n- Looks at email's current labels\n- Checks if any AI label already exists\n- Returns hasAILabelAssigned: true/false\n\n**If Not Assigned** n"
      }
    },
    {
      "id": "c1b365b3-792f-4739-b5df-498d74788665",
      "name": "AI Agent Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        32,
        -624
      ],
      "parameters": {
        "width": 420,
        "height": 632,
        "content": "## 🧠 AI CLASSIFICATION ENGINE\n\n**How it works:**\n\n1️⃣ **Get Emails By Sender** (always called first)\n   - Fetches sender's email history\n   - Checks previously assigned labels\n   \n2️⃣ **80% RULE** (Ma"
      }
    },
    {
      "id": "15599558-75c6-4717-803e-468b0161f405",
      "name": "Sender Tool Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        528
      ],
      "parameters": {
        "width": 300,
        "height": 272,
        "content": "## 🔍 TOOL: Get Sender History\n\nFetches up to 10 previous emails\nfrom the same sender address\n\nFilters:\n- Received BEFORE current email\n- From same sender\n- Includes subject, body, labels"
      }
    },
    {
      "id": "34cf4327-bbfa-4b9b-afff-a1e9f2b60c11",
      "name": "Label Tool Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        240,
        512
      ],
      "parameters": {
        "width": 300,
        "height": 288,
        "content": "## 🔍 TOOL: Get Label Examples\n\nFetches up to 10 emails\nfrom a specific label category\n\nUse when:\n- Sender has no history\n- Need to compare examples\n- Uncertain about classification"
      }
    },
    {
      "id": "5c914b27-389e-4c75-aae8-588324780722",
      "name": "Label Application Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        576,
        -336
      ],
      "parameters": {
        "width": 564,
        "height": 360,
        "content": "## ✅ LABEL APPLICATION\n\n**If not None** → Only continues if AI returned a category\n\n**Convert Label to Label ID:**\n- Maps AI label name → Gmail label ID\n- Example: \"Meetings\" → \"Label_2420158633459891"
      }
    },
    {
      "id": "44109984-8de2-495b-85f5-7c3e27b8d2b0",
      "name": "Get Label IDs",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -928,
        800
      ],
      "parameters": {
        "width": 400,
        "height": 444,
        "content": "## 🔑 HOW TO GET YOUR LABEL IDs\n\n### Use Manual Trigger\n1. Click \"When clicking 'Execute workflow'\"\n2. Click \"Execute Node\" on \"Get Labels Info\"\n3. View output - copy each label's ID\n\n### Label ID Form"
      }
    }
  ],
  "connections": {
    "If not None": {
      "main": [
        [
          {
            "node": "Convert Label to Label ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Not Assigned": {
      "main": [
        [
          {
            "node": "Email Classifier Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Check Label Existence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many messages": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add label to thread": {
      "main": [
        [
          {
            "node": "Remove Inbox label from thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Emails By Label": {
      "ai_tool": [
        [
          {
            "node": "Email Classifier Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Check Label Existence": {
      "main": [
        [
          {
            "node": "If Not Assigned",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Classifier Agent": {
      "main": [
        [
          {
            "node": "If not None",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Email Classifier Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Email Classifier Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Convert Label to Label ID": {
      "main": [
        [
          {
            "node": "Add label to thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Emails By Sender Email": {
      "ai_tool": [
        [
          {
            "node": "Email Classifier Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Remove Inbox label from thread": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Get many messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}