{
  "name": "Support agent with human handoff",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "support-chat",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "95e2a20c-0983-4bce-8130-b5cb2184a9f0",
      "name": "Chat message in",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "webhookId": "bb0a76e4-76a6-43c6-9685-82cfae66cbcf"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.body.message }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "You answer questions about our programme using only the knowledge base tool. Return JSON that matches the schema. Set escalate to true for account-specific, sensitive or out-of-scope questions."
        }
      },
      "id": "2ab178db-ea45-4136-b879-5c5f185033d2",
      "name": "Support agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        260,
        300
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1-mini",
          "mode": "list",
          "cachedResultName": "gpt-4.1-mini"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "id": "4104b475-9d95-436f-8904-d6fd3fd698d2",
      "name": "OpenAI chat model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        120,
        560
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.body.session_id }}",
        "contextWindowLength": 6
      },
      "id": "f46123a8-ea93-4a55-bbf7-80b54904804c",
      "name": "Session memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        300,
        560
      ]
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolName": "knowledge_base",
        "toolDescription": "Programme rules, partners, redemption steps and policies.",
        "pineconeIndex": {
          "__rl": true,
          "value": "support-kb",
          "mode": "list",
          "cachedResultName": "support-kb"
        },
        "options": {}
      },
      "id": "177cc15d-28d9-42cf-a7dd-b0da005356f4",
      "name": "Knowledge base",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
      "typeVersion": 1.1,
      "position": [
        480,
        560
      ]
    },
    {
      "parameters": {
        "model": "text-embedding-3-large",
        "options": {}
      },
      "id": "daa66573-4c71-4317-9be0-659d267fb790",
      "name": "OpenAI embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        500,
        760
      ]
    },
    {
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"answer\": { \"type\": \"string\" },\n    \"confidence\": { \"type\": \"number\" },\n    \"escalate\": { \"type\": \"boolean\" },\n    \"escalation_reason\": { \"type\": \"string\" },\n    \"category\": { \"type\": \"string\" }\n  },\n  \"required\": [\"answer\", \"confidence\", \"escalate\", \"category\"]\n}"
      },
      "id": "1f5c824c-d41a-440e-bca0-4d58a59b91e7",
      "name": "Structured output",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [
        780,
        560
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "5eefa195-70c2-465c-9417-03fb90a7ab87",
              "leftValue": "={{ $json.output.escalate || $json.output.confidence < 0.7 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "cc140d28-f192-4df7-9fbf-518e6303baa6",
      "name": "Escalate?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        620,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const o = $json.output;\nreturn [{ json: { summary: o.answer, reason: o.escalation_reason, category: o.category } }];"
      },
      "id": "67d52532-656c-459f-9383-b3e85a1b6f8a",
      "name": "Build handoff packet",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        180
      ]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#support-handoff",
          "mode": "name"
        },
        "text": "=New handoff: {{ $json.category }} - {{ $json.reason }}",
        "otherOptions": {}
      },
      "id": "520a65b7-e7c9-4868-96b2-c86582e4fa5e",
      "name": "Post to #support-handoff",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        1120,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://helpdesk.example.com/api/tickets",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "86efb035-c788-491f-9e27-7835de029bea",
      "name": "Create helpdesk ticket",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        260
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"reply\": \"I'm passing this to a colleague who can see your account. They'll pick it up from here.\", \"handoff\": true }",
        "options": {}
      },
      "id": "ebed5488-8e9c-43dd-b3b8-512a622e63d5",
      "name": "Reply: a person will take over",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1360,
        180
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"reply\": {{ JSON.stringify($json.output.answer) }}, \"handoff\": false }",
        "options": {}
      },
      "id": "ebdd63cb-b6a3-44ef-a9f3-a7018c567af4",
      "name": "Reply with answer",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        420
      ]
    },
    {
      "parameters": {
        "tableId": "support_logs",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "category",
              "fieldValue": "={{ $('Support agent').item.json.output.category }}"
            },
            {
              "fieldId": "confidence",
              "fieldValue": "={{ $('Support agent').item.json.output.confidence }}"
            },
            {
              "fieldId": "escalated",
              "fieldValue": "={{ $('Support agent').item.json.output.escalate }}"
            }
          ]
        }
      },
      "id": "c877a8fb-34d6-4c67-8ef1-14bc94b3b0b2",
      "name": "Log conversation",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1600,
        300
      ]
    }
  ],
  "connections": {
    "Chat message in": {
      "main": [
        [
          {
            "node": "Support agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI chat model": {
      "ai_languageModel": [
        [
          {
            "node": "Support agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Session memory": {
      "ai_memory": [
        [
          {
            "node": "Support agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Knowledge base": {
      "ai_tool": [
        [
          {
            "node": "Support agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI embeddings": {
      "ai_embedding": [
        [
          {
            "node": "Knowledge base",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Structured output": {
      "ai_outputParser": [
        [
          {
            "node": "Support agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Support agent": {
      "main": [
        [
          {
            "node": "Escalate?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Escalate?": {
      "main": [
        [
          {
            "node": "Build handoff packet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reply with answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build handoff packet": {
      "main": [
        [
          {
            "node": "Post to #support-handoff",
            "type": "main",
            "index": 0
          },
          {
            "node": "Create helpdesk ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create helpdesk ticket": {
      "main": [
        [
          {
            "node": "Reply: a person will take over",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply: a person will take over": {
      "main": [
        [
          {
            "node": "Log conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply with answer": {
      "main": [
        [
          {
            "node": "Log conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}