{
  "name": "02 Gmail Triage - Classify, Label, Draft Reply",
  "nodes": [
    {
      "parameters": {
        "content": "Classifies each new e-mail (lead/support/spam/invoice) with a structured LLM output, applies a Gmail label and drafts a reply for leads. Replace the Label_* IDs with your own Gmail label IDs.",
        "width": 520,
        "height": 200
      },
      "id": "8f20da07-ff0d-509a-8d4e-4ee385b900ba",
      "name": "Setup Notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -200,
        -260
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "labelIds": [
            "INBOX"
          ]
        },
        "options": {}
      },
      "id": "535410c9-597e-5335-aea6-22e30cd537a4",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=From: {{ $json.from.value[0].address }}\nSubject: {{ $json.subject }}\n\n{{ ($json.text || '').slice(0, 3000) }}",
        "messages": {
          "messageValues": [
            {
              "message": "Classify the e-mail into exactly one category: lead (prospective customer asking about services), support (existing customer needs help), invoice (bill, receipt, payment), spam (unsolicited or irrelevant). Return the category, a confidence 0-1 and a one-line summary."
            }
          ]
        },
        "hasOutputParser": true
      },
      "id": "cd7a4036-5d2e-5741-b720-7c6f645e7657",
      "name": "Classify Email",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "id": "51803583-4fdb-57d9-a638-7ab65224b95d",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        240,
        220
      ],
      "credentials": {
        "openAiApi": {
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "schemaType": "fromJson",
        "jsonSchemaExample": "{\n  \"category\": \"lead\",\n  \"confidence\": 0.93,\n  \"summary\": \"Asks for a quote for a chatbot\"\n}"
      },
      "id": "e110b6c9-6801-5694-bb4c-4befd2bb9c1a",
      "name": "Category Schema",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [
        420,
        220
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "98d59aa5-727d-5185-8e3e-abdd3f2c3a2d",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "lead",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "lead"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "73c87ed4-76db-59f5-acba-0523bbb34c86",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "support",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "support"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "9e74b935-496a-5e0d-993b-a5172c6614d2",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "invoice",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "invoice"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": false,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "6300d97d-b1c4-52f3-9eb3-326051fa0c61",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "spam",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "spam"
            }
          ]
        },
        "options": {}
      },
      "id": "9594e0d3-1ee1-5bfa-a3bc-5fb39f7f6f59",
      "name": "Route by Category",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        500,
        0
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "labelIds": [
          "Label_LEAD"
        ]
      },
      "id": "207c9a0b-ecec-5f8d-90a4-3b94521723f6",
      "name": "Label Lead",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        760,
        -60
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "labelIds": [
          "Label_SUPPORT"
        ]
      },
      "id": "71cfc7f2-dcee-55ad-b7f0-72cd20a8a1a8",
      "name": "Label Support",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        760,
        100
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "labelIds": [
          "Label_INVOICE"
        ]
      },
      "id": "57e898d2-ad31-533f-aacd-4c59f8ca1870",
      "name": "Label Invoice",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        760,
        260
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "labelIds": [
          "Label_SPAM"
        ]
      },
      "id": "6217a03b-ba64-57ad-830c-6f8a53f4fc3a",
      "name": "Label Spam",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        760,
        420
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Write a reply to this e-mail.\nFrom: {{ $('Gmail Trigger').item.json.from.value[0].name }}\nSubject: {{ $('Gmail Trigger').item.json.subject }}\n\n{{ ($('Gmail Trigger').item.json.text || '').slice(0, 3000) }}",
        "messages": {
          "messageValues": [
            {
              "message": "You write short, warm, professional replies for a service business. Thank them, answer what you can, propose a 15-minute call, sign off with the business name. Max 6 sentences, same language as the incoming e-mail. Plain text only."
            }
          ]
        }
      },
      "id": "7a2d7ad2-4957-5d94-a38e-b167e5c210dc",
      "name": "Draft Lead Reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1020,
        -60
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "id": "7f8ae647-3e0d-5af9-9284-752fdb7be515",
      "name": "OpenAI Chat Model (Reply)",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        1020,
        160
      ],
      "credentials": {
        "openAiApi": {
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=Re: {{ $('Gmail Trigger').item.json.subject }}",
        "message": "={{ $json.text }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}"
        }
      },
      "id": "5c638650-09f3-5072-89df-c33da1da7b5f",
      "name": "Create Reply Draft",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1280,
        -60
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Classify Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Category Schema": {
      "ai_outputParser": [
        [
          {
            "node": "Classify Email",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Route by Category": {
      "main": [
        [
          {
            "node": "Label Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label Support",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label Invoice",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label Spam",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model (Reply)": {
      "ai_languageModel": [
        [
          {
            "node": "Draft Lead Reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Classify Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Email": {
      "main": [
        [
          {
            "node": "Route by Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label Lead": {
      "main": [
        [
          {
            "node": "Draft Lead Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft Lead Reply": {
      "main": [
        [
          {
            "node": "Create Reply Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}