{
  "name": "Gmail with AI worklow",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {}
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        -416,
        -48
      ],
      "id": "3a7435fb-8642-4186-a0e3-45a5d5ebf9ad",
      "name": "New Mail"
    },
    {
      "parameters": {
        "operation": "get",
        "messageId": "={{ $json.id }}"
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -176,
        -112
      ],
      "id": "4981bc31-e199-492e-be43-f72d9eb61324",
      "name": "Get a message",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are a email responding agent.\n\nBelow are the content of latest email I got.\n\nSubject : {{ $json.Subject }}\n\nBody : {{ $json.snippet }}\n\nYou have to do 2 things.\n\n1. specify the label based on the email.\nUse the tool to get all the labels and there Id and mention both of them properly.\n\nlabel can be one of below : \n\ncourse,\ndoubts,\nsponsorship\n\n2. As a smart assistant please draft the response for me so that I can reply to my email. with subject and body\n\nMake sure that reply is very respectable and wherever required please explain student doubt in depth with example and alsp make sure that reply follow a strict HTML format so it is easy to read.\nYou do not have to add any css styling just give in simple html and add Your Instructor as best regard",
        "hasOutputParser": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        32,
        -160
      ],
      "id": "34b273dd-c20d-4973-abe7-e163164c3fe3",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": "deepseek/deepseek-chat-v3.1:free",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        -96,
        96
      ],
      "id": "b546d7bb-c88d-4b0b-9829-896677d58790",
      "name": "OpenRouter Chat Model"
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\"label\":\"doubt\",\n  \"label_id\":\"12423512\",\n    \"subject\":\"doubt_answer\",\n    \"body\":\"Hey Thanks for reaching out\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        304,
        64
      ],
      "id": "803e65b3-3476-4167-b3f2-9acbcadfa0bc",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "resource": "label",
        "returnAll": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Return_All', ``, 'boolean') }}"
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        160,
        48
      ],
      "id": "bc2aaa2b-a8dd-40c8-9366-1b4068ddaf61",
      "name": "Get many labels in Gmail",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Get a message').item.json.id }}",
        "labelIds": "={{ $json.output.label_id }}"
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        384,
        -160
      ],
      "id": "194514f3-c350-4fdd-8549-337165b77932",
      "name": "Add label to message",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ $('AI Agent').item.json.output.subject }}",
        "emailType": "html",
        "message": "={{ $('AI Agent').item.json.output.body }}",
        "options": {
          "threadId": "={{ $json.threadId }}",
          "sendTo": "={{ $('Get a message').item.json.From }}"
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        592,
        -160
      ],
      "id": "d402ba32-cfe0-4c22-b79d-1e2cdbd37cd7",
      "name": "Create a draft",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "New Mail": {
      "main": [
        [
          {
            "node": "Get a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a message": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Get many labels in Gmail": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Add label to message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add label to message": {
      "main": [
        [
          {
            "node": "Create a draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0d4306b0-d84a-453a-a6bc-7564ad986b07",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "SX7VLbkjIlv9CH6e",
  "tags": [
    {
      "createdAt": "2025-10-03T16:35:45.143Z",
      "updatedAt": "2025-10-03T16:35:45.143Z",
      "id": "Ie1T8I5F5dfoBo9S",
      "name": "learning"
    },
    {
      "createdAt": "2025-10-05T04:27:36.751Z",
      "updatedAt": "2025-10-05T04:27:36.751Z",
      "id": "SNO8LfARlW1bOFyo",
      "name": "learnn8n"
    },
    {
      "createdAt": "2025-10-06T08:22:28.282Z",
      "updatedAt": "2025-10-06T08:22:28.282Z",
      "id": "xykOHjGYcJWPo9Mc",
      "name": "gmail"
    }
  ]
}