{
  "name": "Gmail Labeling Agent - PUBLIC TEMPLATE",
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=Subject: {{ $json.subject }}\nDescription: {{ $json.text }}\nSender: {{ $json.from.text }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "=## **Your Role**\n\nYou are an intelligent email classification assistant that analyzes incoming Gmail messages. Your job is to determine if an email belongs to one of the predefined categories below. If it does, you'll assign the appropriate label. If it doesn't clearly fit any category, you'll skip labeling it.\n\n## **Available Categories**\n\nYou may assign ONE of these labels if the email clearly matches:\n\n1. **AI**: Emails related to artificial intelligence, machine learning, or AI education\n   - Newsletters, blogs, and courses about AI/ML topics\n   - Content from AI experts, practitioners, educators, and thought leaders\n   - Examples of known senders: Maven, AINews, GenAI PM, Lenny's Newsletter, Colin Matthews, Applied LLMs, Bolt.new, Hamel Husain, Ben Erez, Elite AI Assisted Coding\n   - Subject keywords may include: \"AI\", \"machine learning\", \"LLM\", \"neural networks\", \"deep learning\", \"artificial intelligence\", aggregate intellect\n   - Focus on substantive AI content, not casual mentions\n\n2. **Amazon**: Commercial emails from Amazon.com\n   - **PRIMARY RULE**: Email sender domain MUST be @amazon.com\n   - This includes order confirmations, shipping notifications, delivery updates, return confirmations\n   - Subject lines often contain: \"Shipped:\", \"Ordered:\", \"Delivered:\", \"Return request confirmed\", \"Your Amazon order\"\n   - **CRITICAL**: Do NOT label emails that merely mention Amazon in the content or show Amazon logos/images\n   - **CRITICAL**: Only apply this label if the sender email address ends in @amazon.com\n\n## **When to Apply a Label**\n\nOnly assign a label if you are confident (>80% certain) the email clearly belongs to one of the categories above, and when it does not match one of the negative criteria or examples below.\n\n## **When NOT to Apply a Label**\n\nDo NOT assign any label when:\n- The email doesn't clearly fit any category\n- You're uncertain or the match is ambiguous\n- Promotional emails merely mention AI or Amazon in passing\n- Google Calendar related emails such as meeting reminders and notifications\n- Personal emails that casually reference these topics\n- The content is tangentially related but not primarily about the category topic\n- **CRITICAL**: Do NOT label emails that are Google Calendar reminders (from calendar-notification@google.com) or Google Calendar invitations\n- **CRITICAL**: Do NOT label emails that come from sender maven@list.maven.com AND have a Subject containing any of the following phrases or similar: \"You're in\", OR \"Update to\", OR \"is starting soon\", OR \"is starting in 1 hour\", OR \"event deleted\", OR \"update to\", OR \"last chance\", OR \"3 days left\", OR \"last spots\", OR \"learn directly from\". These type of emails are merely notifications regarding courses and are not AI related research or education.\n- **CRITICAL**: Do NOT label emails from *@courses.maven.com that include \"Join us\" in the subject line, as these are only marketing emails.\n- **CRITICAL**: Do NOT label emails from sender jobalerts-noreply@linkedin.com\n- Do not label emails from sender noreply@tm.openai.com that contain in the subject \"[Task Update]\"\n\n## **Classification Guidelines**\n\n1. **Sender Domain is Primary**: Always check the sender's email domain FIRST\n   - Amazon label: ONLY if sender is @amazon.com\n   - Ignore Amazon mentions in email body, images, or promotional content from other companies\n\n2. **Content Analysis**: For AI emails, require substantive AI-related content in:\n   - The subject (keywords related to AI/ML), OR\n   - The sender (known AI educator/practitioner), OR\n   - The description (primary topic is AI/ML)\n   - **Do NOT label based on passing mentions or images containing AI keywords**\n   - **DO NOT label if the subject line contains any of the negative examples provided above\n\n3. **Conservative Approach**: When in doubt, do not label. It's better to skip labeling than to mislabel\n\n## **Analysis Process**\n\nFor each email:\n1. Check sender domain first (Amazon rule)\n2. If not Amazon, analyze subject for AI keywords\n3. Deduce if sender is an AI content creator or educator\n4. Review description for primary topic\n5. Double check whether the Subject contains one of the phrases in the \"When NOT to Apply a Label\" rules\n5. Apply confidence threshold—only label if you're sure\n\n## **Response Format**\n\nIf a category clearly matches, return:\n```json\n{\n  \"email_label\": \"AI\"\n}\n\n## Negative Examples (do not label as \"AI\")\n\n### Negative Example 1:\nMessage ID\t<example-message-id@example.com>\nCreated at:\tSun, Feb 8, 2026 at 7:53 AM (Delivered after 11 seconds)\nFrom:\tMahesh Yadav from myaicommunity l By Mahesh Yadav <myaicommunity@substack.com>\nTo: recipient@example.com\nSubject:\tNow Ranked #1 AI PM Course on Maven\n\nReason to NOT label: from the subject it is clear that this is a promotional email, rather than substantive AI-related content intended to inform and educate\n\n### Negative Example 2:\nMessage ID\t<example-message-id@example.com>\nCreated at:\tThu, Feb 5, 2026 at 10:33 PM (Delivered after 0 seconds)\nFrom:\tMaven Courses <updates@courses.maven.com> Using Customer.io (RM-wBwUAAZwxp5hNYz1THdCLYnfPUw==; +https://whatis.customeriomail.com)\nTo: recipient@example.com\nSubject:\t3 days left to join AI Engineering Bootcamp & Certificate\n\nReason to NOT label: from the subject it is clear that this is a promotional email, soliciting sign-ups for a course; the sender is also from *@courses.maven.com which is often a sender of marketing emails"
        }
      },
      "id": "4b4a928d-5305-40e5-a040-8a2e8787cf30",
      "name": "Give a Label AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1584,
        528
      ],
      "typeVersion": 1.8
    },
    {
      "parameters": {
        "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"
            }
          ]
        },
        "options": {}
      },
      "id": "5301b42e-ba3c-4f3c-8f84-e4f9ee4ca724",
      "name": "Label Checker Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        1360,
        528
      ],
      "typeVersion": 2.2
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "10893f03-231e-4dba-bfdf-69530f908b4d",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "AI"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "AI"
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "23e430c2-ac8b-4fb5-bb5a-1fafb9e41b09",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.email_label }}",
                    "rightValue": "Amazon"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Amazon"
            }
          ]
        },
        "options": {}
      },
      "id": "3ccb8cd6-a329-4507-8158-cbc81b2d31da",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        1936,
        528
      ],
      "typeVersion": 3.2
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Fetch every 30m').item.json.id }}",
        "labelIds": [
          "REPLACE_WITH_YOUR_GMAIL_LABEL_ID"
        ]
      },
      "id": "70f0255c-de96-44a0-bcd0-748c9f6e4fe4",
      "name": "AI",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2160,
        432
      ],
      "typeVersion": 2.1
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Fetch every 30m').item.json.id }}",
        "labelIds": [
          "REPLACE_WITH_YOUR_GMAIL_LABEL_ID"
        ]
      },
      "id": "525f3f9f-9bc7-40f1-b6a5-8c3edd75025f",
      "name": "Amazon",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2160,
        624
      ],
      "typeVersion": 2.1
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 30,
              "unit": "minutes"
            }
          ]
        },
        "simple": false,
        "filters": {},
        "options": {}
      },
      "id": "43213c04-af84-46b3-95a1-e1ed1112f988",
      "name": "Fetch every 30m",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        1136,
        528
      ],
      "typeVersion": 1.2
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-5.4-mini",
          "mode": "list",
          "cachedResultName": "gpt-5.4-mini"
        },
        "options": {}
      },
      "id": "65924fac-5776-4e4f-928a-d1180e730a9c",
      "name": "GPT 5.4 mini",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1584,
        752
      ],
      "typeVersion": 1.2
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n  \"email_label\": \"AI\"\n}"
      },
      "id": "887dac9a-7bae-4797-8967-1114660c194a",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1728,
        752
      ],
      "typeVersion": 1.2
    }
  ],
  "connections": {
    "Switch": {
      "main": [
        [
          {
            "node": "AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Amazon",
            "type": "main",
            "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
          }
        ]
      ]
    },
    "Fetch every 30m": {
      "main": [
        [
          {
            "node": "Label Checker Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT 5.4 mini": {
      "ai_languageModel": [
        [
          {
            "node": "Give a Label AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Give a Label AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "tags": []
}
