{
  "id": "fpbH3RZcx84GoEIF",
  "name": "AI Email Automation & Intelligent Response Management",
  "tags": [],
  "nodes": [
    {
      "id": "0ca4e5fe-de44-466f-b166-5e67d9c1cdec",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        32
      ],
      "parameters": {
        "width": 480,
        "height": 896,
        "content": "## AI Email Automation & Intelligent Response Management\n\n### How it works\n\nThis workflow monitors Gmail for new messages, fetches the full email, and uses an AI agent to classify what kind of response is needed. A switch routes each email to either an automated reply, a generated draft for human review, or no action. The reply and draft paths use AI generation with shared knowledge-base and model resources before sending or flagging the result in Gmail.\n\n### Setup steps\n\n- Connect Gmail credentials for the trigger, email fetch, reply, and human-review flag actions.\n- Configure the Gmail Trigger with the mailbox, labels, or search criteria that should start the automation.\n- Add OpenRouter credentials and choose the chat models used by the classification, reply-generation, and draft-generation agents.\n- Connect Google Docs credentials and point the knowledge-base tools to the documents that should inform generated responses.\n- Review the switch rules so the classification output maps correctly to reply, draft, or no-action branches.\n\n### Customization\n\nCustomize the AI prompts, classification categories, switch routing rules, Gmail labels, and Google Docs knowledge sources to match the organization\u2019s email handling policy."
      },
      "typeVersion": 1
    },
    {
      "id": "35e02ef5-731b-4e8b-9ddf-b4a17a63fbda",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        688,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 848,
        "height": 656,
        "content": "## Receive and classify email\n\nStarts from the Gmail trigger, fetches the incoming message, and classifies it with an AI agent supported by its OpenRouter chat model. These nodes form the left-side intake and understanding cluster."
      },
      "typeVersion": 1
    },
    {
      "id": "8a57ce10-2a0c-4d4a-82ac-45a7d337f446",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1616,
        208
      ],
      "parameters": {
        "color": 7,
        "height": 784,
        "content": "## Route response decision\n\nBranches the classified email into reply, draft, or no-action paths. The no-operation outcome sits in the same central routing area and handles emails that require nothing further."
      },
      "typeVersion": 1
    },
    {
      "id": "60ef6643-11cb-48c2-8135-98a0c089e7b5",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1936,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 1056,
        "height": 1264,
        "content": "## Generate and send reply\n\nCreates an AI-written response for emails approved for automatic handling, then sends the reply through Gmail. This is the upper-right execution branch."
      },
      "typeVersion": 1
    },
    {
      "id": "e15279b9-354b-43b6-a3c4-4c75ea1ad476",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1936,
        1328
      ],
      "parameters": {
        "color": 7,
        "width": 832,
        "height": 656,
        "content": "## Draft for human review\n\nGenerates an AI-composed email for cases that need review, then flags or prepares it in Gmail for a human to inspect. This is the lower-right execution branch."
      },
      "typeVersion": 1
    },
    {
      "id": "806ddf3a-bb2c-49a6-bb9a-af570321c857",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3024,
        576
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 704,
        "content": "## Shared generation resources\n\nContains the Google Docs knowledge-base tools and OpenRouter chat models used by the reply and draft generation agents. Although they support different branches, they are visually grouped together on the far-right lower canvas."
      },
      "typeVersion": 1
    },
    {
      "id": "e168135e-5b63-4f5a-84f0-1acfdd6ee434",
      "name": "Send Email for Review",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2112,
        1456
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {
          "sendTo": "={{ $('Retrieve Gmail Email').item.json.from.value[0].address }}",
          "threadId": "={{ $('Retrieve Gmail Email').item.json.threadId }}"
        },
        "subject": "={{ $('Retrieve Gmail Email').item.json.subject }}",
        "resource": "draft"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "e2bae5f2-2f6f-4f01-9fd2-e97b4aff609b",
      "name": "When Gmail Receives Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        736,
        464
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "e7cddc87-c4c0-47b7-a7cb-d8fa94e7a234",
      "name": "Skip Action",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1680,
        832
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "79529a5b-83ee-47ac-b4ea-6f8c3dd4dcb7",
      "name": "Route Email Decision",
      "type": "n8n-nodes-base.switch",
      "position": [
        1664,
        448
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "68b68c40-f028-4431-9940-66543b04578a",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Reply"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7df0c8a2-3616-4e84-9af3-14a2e637cb7c",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Draft"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "799f30a4-15c6-45cb-8617-d9e2fe148536",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Nothing"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "36dce78f-3749-44c3-b513-83bdfb5bdc3b",
      "name": "Send Reply Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2112,
        160
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {
          "appendAttribution": false,
          "replyToSenderOnly": false
        },
        "emailType": "text",
        "messageId": "={{ $('Retrieve Gmail Email').item.json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "a54874a0-154e-48c9-adeb-b35210814dac",
      "name": "Fetch Knowledge Base Data",
      "type": "n8n-nodes-base.googleDocsTool",
      "position": [
        3072,
        1472
      ],
      "parameters": {
        "simple": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Simplify', ``, 'boolean') }}",
        "operation": "get",
        "documentURL": "https://docs.google.com/document/d/1cA7t2VczablyjqiMlRPa5mC6FPFkrMEMJJBTEh6zkNA/edit?tab=t.0"
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "cc00ed86-386f-4d7a-a10b-b035d0f1dcc3",
      "name": "Get Knowledge for Reply",
      "type": "n8n-nodes-base.googleDocsTool",
      "position": [
        3280,
        784
      ],
      "parameters": {
        "simple": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Simplify', ``, 'boolean') }}",
        "operation": "get",
        "documentURL": "https://docs.google.com/document/d/1IwqNuOMKin0H05rGXI3CceQtp9o5753goqQY5GtF_sA/edit?tab=t.0"
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "306fb983-6c85-41c6-9eed-2032faa16d83",
      "name": "Retrieve Gmail Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        960,
        464
      ],
      "parameters": {
        "simple": false,
        "options": {},
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "1d596cce-868d-4edf-8a00-3f6b45ef3386",
      "name": "Email Classification Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1232,
        464
      ],
      "parameters": {
        "text": "=You are an AI assistant that drafts helpful email replies. For each incoming email, you will draft a reply and classify whether the email is 'simple' (can be auto-replied) or 'complex' (requires human review) or needs nothing to do.\n\nRules for classification:\n\n\nIf the email contains any sensitive topic (e.g: personal data such as biometric data, health records, racial or ethnic origin, religious or philosophical beliefs, political opinions, and sexual orientation, financial data (account numbers, credit card information), government identifiers (Social Security Numbers), and company-specific data such as trade secrets and strategic plans, etc.). classify it as \"draft\".\n\nAnd if the email does not contain any of the sensitive information and is a normal email and needs a reply, classify it as \"Reply\".\n\nAnd if an email does not contain any of the topic or information that does need reply (e.g, marketing or spam, automated notifications like \"do not reply\" emails or pickup alerts, transactional messages such as purchase receipts or account verification, and emails with no clear call to action that could bury important messages for recipients. In general, if a message is purely informational, doesn't ask for action, or is from an unknown source.) Classify it as \"Nothing\".\n\nProvide the Category Only: Respond with the category name and nothing else. Do not add any extra text, explanations, or commentary. Your response should be a single word. and you have 3 categories: \n\n1. Draft\n2. Reply\n3. Nothing\n\nHere is the Email:\n\n{{ $json.text }}",
        "options": {
          "batching": {
            "batchSize": 1,
            "delayBetweenBatches": 0
          },
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "0311967c-b3da-49ba-96d7-e571ea51e582",
      "name": "Create Reply with AI",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1984,
        320
      ],
      "parameters": {
        "text": "=Reply to the email I give you in a warm, polite, and professional tone. Write like a thoughtful human\u2014use natural phrasing, gentle transitions, and avoid robotic or overly formal wording. Address all points clearly, keep it concise, and maintain a friendly, respectful feel. Reply must be easy to read. and don't output subject line, only output the reply. My name is Muhammad Mustafa. must keep an eye on one thing, don't add in the reply anything like this \"[Your Company Name]\" etc. its a final reply so don't add anything that needs any manual addition or editing. Output the final reply.\n\nHere is the Subject line: {{ $('Retrieve Gmail Email').item.json.headers.subject }}\n\nHere is the Email:\n\n{{ $('Retrieve Gmail Email').item.json.text }}",
        "options": {
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "d012afdb-fdc9-4d2d-89fe-70b642cf245c",
      "name": "Draft New Email with AI",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1984,
        1616
      ],
      "parameters": {
        "text": "=Reply to the email I give you in a warm, polite, and professional tone. Write like a thoughtful human\u2014use natural phrasing, gentle transitions, and avoid robotic or overly formal wording. Address all points clearly, keep it concise, and maintain a friendly, respectful feel. Reply must be easy to read. and don't output subject line, only output the reply. My name is Muhammad Mustafa. must keep an eye on one thing, don't add in the reply anything like this \"[Your Company Name]\" etc. its a final reply so don't add anything that that needs any manual addition or editing. Output the final reply.\n\nHere is the Subject line: {{ $('Retrieve Gmail Email').item.json.headers.subject }}\n\nHere is the Email:\n\n{{ $('Retrieve Gmail Email').item.json.text }}",
        "options": {
          "batching": {
            "batchSize": 1,
            "delayBetweenBatches": 0
          },
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "8a79b61e-a7b6-46ab-bc99-ff4e9ee2c5ab",
      "name": "OpenRouter Model Classify",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        1232,
        816
      ],
      "parameters": {
        "model": "qwen/qwen-2.5-7b-instruct",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "dc094853-1abb-4e60-a3f4-7cca718a3a29",
      "name": "OpenRouter Model Email Draft",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        3088,
        1824
      ],
      "parameters": {
        "model": "qwen/qwen-2.5-7b-instruct",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c7cac66d-fb90-4fc6-96bd-fd9cfa774f20",
      "name": "OpenRouter Model for Reply",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        3312,
        1136
      ],
      "parameters": {
        "model": "qwen/qwen-2.5-7b-instruct",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "d290be37-f255-471a-903c-84e4ebaace07",
  "nodeGroups": [],
  "connections": {
    "Create Reply with AI": {
      "main": [
        [
          {
            "node": "Send Reply Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retrieve Gmail Email": {
      "main": [
        [
          {
            "node": "Email Classification Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Email Decision": {
      "main": [
        [
          {
            "node": "Create Reply with AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Draft New Email with AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Skip Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft New Email with AI": {
      "main": [
        [
          {
            "node": "Send Email for Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Knowledge for Reply": {
      "ai_tool": [
        [
          {
            "node": "Create Reply with AI",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Knowledge Base Data": {
      "ai_tool": [
        [
          {
            "node": "Draft New Email with AI",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Model Classify": {
      "ai_languageModel": [
        [
          {
            "node": "Email Classification Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When Gmail Receives Email": {
      "main": [
        [
          {
            "node": "Retrieve Gmail Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Classification Agent": {
      "main": [
        [
          {
            "node": "Route Email Decision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Model for Reply": {
      "ai_languageModel": [
        [
          {
            "node": "Create Reply with AI",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Model Email Draft": {
      "ai_languageModel": [
        [
          {
            "node": "Draft New Email with AI",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}