{
  "nodes": [
    {
      "id": "ebb9a336-a28d-4896-b997-46c9fa963127",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        280,
        400
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f3099bfa-ff5d-42ea-88db-f4ac5206b21b",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        720,
        420
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "d05201ee-55d4-4e27-b31e-6c588ee224ae",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        940,
        640
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5",
          "cachedResultName": "gpt-5"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "1503f20c-7b39-4b47-8d80-ffcafea8d004",
      "name": "Add Label (High Priority)",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1440,
        260
      ],
      "parameters": {
        "labelIds": [
          "IMPORTANT"
        ],
        "resource": "thread",
        "threadId": "={{ $json.threadId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "1cf7e5f2-d95d-4716-92b8-25fb873c5279",
      "name": "Add Label (Personal)",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1440,
        460
      ],
      "parameters": {
        "labelIds": [
          "CATEGORY_PERSONAL"
        ],
        "resource": "thread",
        "threadId": "={{ $json.threadId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "c6bf0b6d-07b8-4db6-a23c-e016e922ae6a",
      "name": "Add Label (Promotions)",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1440,
        680
      ],
      "parameters": {
        "labelIds": [
          "CATEGORY_PROMOTIONS"
        ],
        "resource": "thread",
        "threadId": "={{ $json.threadId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "96a7c0e2-8cbc-4b6f-bad4-890c22f49c41",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -180,
        320
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "height": 300,
        "content": "## SETUP REQUIRED\n\nWorkflow Configurations:\n- Update the Text Classifier and adjust the labels based on your use case.\n- Add/Remove the Gmail addLabels node to match your use case.\n\nRequired Credentials:\n- Gmail Credential\n- OpenAI API Key"
      },
      "typeVersion": 1
    },
    {
      "id": "356a4f13-5e7e-4867-ae9d-e7b668fc000d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -180,
        960
      ],
      "parameters": {
        "color": 4,
        "width": 680,
        "height": 400,
        "content": "## \ud83d\udccb WORKFLOW PROCESS OVERVIEW\n\nStep 1: \u23f0 Schedule Trigger executes the workflow every 5 minutes automatically\nStep 2: \ud83d\udce8 Gmail Fetch retrieves up to 10 recent emails from the last 5 minutes using Gmail API\nStep 3: \ud83d\udd04 Loop Over Items processes each email individually through a batch splitting mechanism\nStep 4: \ud83e\udd16 Text Classifier (AI Agent) analyzes email subject and body content using OpenAI to categorize emails\nStep 5: \ud83c\udff7\ufe0f Add Labels applies the appropriate Gmail label based on classification: (below is an example only, adjust to your own use case)\n  - High Priority emails \u2192 \"IMPORTANT\" label\n  - Personal emails \u2192 \"CATEGORY_PERSONAL\" label  \n  - Promotional emails \u2192 \"CATEGORY_PROMOTIONS\" label\n\n\nStep 6: \u21a9\ufe0f Loop Back continues processing remaining emails until all are classified and labeled"
      },
      "typeVersion": 1
    },
    {
      "id": "caac34f4-9c3f-498c-851e-0f3c8aa47b59",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -180,
        660
      ],
      "parameters": {
        "color": 4,
        "width": 680,
        "height": 280,
        "content": "## \ud83d\udce7 Smart Gmail Labeling Automation with Text Classifier and GPT-5\n\nWhat This Template Does:\n\n- Runs every 5 minutes to automatically fetch recent emails from your Gmail inbox\n- Retrieves up to 10 emails received within the last 5 minutes\n- Uses OpenAI's Model to intelligently classify each email into predefined categories\n- Automatically applies appropriate Gmail labels based on AI classification results\n- Organizes your inbox by categorizing emails as High Priority, Personal, or Promotional content\n- Processes emails individually through a batch loop to ensure accurate classification"
      },
      "typeVersion": 1
    },
    {
      "id": "59f3ef25-86a1-4353-b52a-17ef8437d014",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1360,
        80
      ],
      "parameters": {
        "color": 4,
        "width": 280,
        "height": 860,
        "content": "## Add Label Nodes\n\nAdd Label Nodes based on your use case. For example, if you have 5 labels, then you can add 5 nodes and adjust the label names on each node.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "90022931-b5a3-459f-8007-0828ef195cc3",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        940,
        280
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 320,
        "content": "## Label Classifier\n\nAdjust the categories based on your use case. Also, add a description to each category for maximum results.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "f73aa2ef-317d-44bf-a04c-7cc40a823328",
      "name": "Label Classifier",
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "position": [
        960,
        460
      ],
      "parameters": {
        "options": {
          "fallback": "other"
        },
        "inputText": "=Email Subject:\n{{ $json.headers.subject }}\n\nEmail Body:\n{{ $json.text }}",
        "categories": {
          "categories": [
            {
              "category": "High Priority",
              "description": "Emails that require immediate attention or urgent action."
            },
            {
              "category": "Personal",
              "description": "Personal refers to activities or matters related to private life, self, or non-work interests."
            },
            {
              "category": "Promotions",
              "description": "Promotions refer to marketing messages or activities that advertise products, services, or offers"
            }
          ]
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": false
    },
    {
      "id": "c27d9b65-e31a-483e-beba-70af54e287fd",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        380,
        240
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 320,
        "content": "## Gmail - Get Emails\n\nGet all messages received within the last 5 minutes.  \nYou can adjust the time range and the email limit here.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "4b8a92f1-4894-43cd-a647-7efebd253e64",
      "name": "Gmail - Get Emails",
      "type": "n8n-nodes-base.gmail",
      "position": [
        480,
        400
      ],
      "parameters": {
        "limit": 10,
        "simple": false,
        "filters": {
          "receivedAfter": "={{ $now.minus({ minutes: 50000 }).toMillis() }}"
        },
        "options": {},
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "f8aeeec3-e0d7-44a9-ad67-6c2735a63a6e",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        120
      ],
      "parameters": {
        "color": 4,
        "width": 380,
        "height": 760,
        "content": "# \ud83d\udc4b Hi, I\u2019m Billy\n![My Photo](https://i.ibb.co/Gvn63Bzc/Billy-Christi-AI-Automation.jpg)\nI help businesses build **n8n workflows** & **AI automation projects**.  \nNeed help with n8n or AI Automation projects? \nContact me and let\u2019s build your automation together.\n\n\ud83d\udce9 **Email:** billychartanto@gmail.com  \n\ud83e\udd1d **n8n Creator:** [n8n.io/creators/billy](https://n8n.io/creators/billy/)\n\ud83c\udf10 **My n8n Projects:** [billychristi.com/n8n](https://www.billychristi.com/n8n)  \n\n\n\n---\n\ud83d\udca1 Feel free to get in touch if you\u2019d like help on your next automation project or if you have any feedback or thoughts to share.\n"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Label Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label Classifier": {
      "main": [
        [
          {
            "node": "Add Label (High Priority)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Label (Personal)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Label (Promotions)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Gmail - Get Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Label Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Gmail - Get Emails": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Label (Personal)": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Label (Promotions)": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Label (High Priority)": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}