{
  "name": "M12 Triage with AI log",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "subject:\"[S12 test]\" -subject:\"Re:\" -subject:\"Approve this reply\""
        },
        "options": {}
      },
      "id": "e6fc7039-879a-48e1-b52b-187c04091d7b",
      "name": "New email",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "570d61cc-9895-4612-ad79-fa8c37d37449",
              "name": "started_at",
              "value": "={{ Date.now() }}",
              "type": "number"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "ceff4d35-71ef-4073-9bbd-7845836ab73b",
      "name": "Start timer",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You classify incoming customer emails for a small software company.\nRead the email below and reply with exactly one label from this list:\nbilling, technical, sales, unsure\nRules:\n- billing: invoices, payments, refunds, subscription changes\n- technical: errors, login problems, how-to questions about the product\n- sales: pricing, demos, new licences, partnerships\n- unsure: anything else, or if two labels fit equally well\nReply with the label, a vertical bar, and a confidence from 1 (a guess) to 5 (certain), for example: billing|4\nNothing else: no punctuation, no explanation.\nEmail subject: {{ $json.subject }}\nEmail body: {{ $json.text }}",
        "batching": {}
      },
      "id": "a3f4d1ca-ab0c-40ef-a9b2-0afb98d02028",
      "name": "Classify email",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.7,
      "position": [
        440,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-flash",
        "options": {
          "temperature": 0
        }
      },
      "id": "0e6ebc6d-cb9e-4153-b06b-2b7d731cf920",
      "name": "Gemini (flash)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        700,
        260
      ],
      "credentials": {
        "googlePalmApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ed33db16-339f-4c40-880b-efb75ee3571c",
              "name": "raw",
              "value": "={{ $json.text }}",
              "type": "string"
            },
            {
              "id": "047a02ac-37d8-42e5-a50e-6ed5dddba8b6",
              "name": "label",
              "value": "={{ ($json.text || '').split('|')[0].trim().toLowerCase() }}",
              "type": "string"
            },
            {
              "id": "f6672f10-56e9-4a23-856f-506c6f997899",
              "name": "confidence",
              "value": "={{ (($json.text || '').split('|')[1] || '').trim() }}",
              "type": "string"
            },
            {
              "id": "f9a3dcfc-d21f-484e-bed1-344b8f5f936a",
              "name": "latency_ms",
              "value": "={{ Date.now() - $('Start timer').item.json.started_at }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "f480d9b7-a465-43ea-88e6-e9716b4ea938",
      "name": "Clean label",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        760,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-M12-MONITORING-WORKBOOK"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "AI log"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "date": "={{ $now.toFormat('yyyy-MM-dd') }}",
            "workflow": "={{ $workflow.name }}",
            "run_id": "={{ $execution.id }}",
            "step": "Classify email",
            "model": "gemini-2.5-flash",
            "input_200": "={{ ('Subject: ' + $('New email').item.json.subject + ' | ' + $('New email').item.json.text).replace(/[\\w.+-]+@[\\w-]+\\.[\\w.]+/g, '[email]').replace(/\\+?\\d[\\d \\-]{7,}\\d/g, '[phone]').slice(0, 200) }}",
            "output": "={{ $('Clean label').item.json.label }}",
            "confidence": "={{ $('Clean label').item.json.confidence }}",
            "tokens_in": "={{ Math.ceil((576 + ($('New email').item.json.subject + $('New email').item.json.text).length) / 4) }}",
            "tokens_out": "={{ Math.ceil($('Clean label').item.json.raw.length / 4) }}",
            "cost_usd": "={{ ((Math.ceil((576 + ($('New email').item.json.subject + $('New email').item.json.text).length) / 4)) * 0.3 / 1e6 + (Math.ceil($('Clean label').item.json.raw.length / 4)) * 2.5 / 1e6).toFixed(6) }}",
            "latency_ms": "={{ $('Clean label').item.json.latency_ms }}",
            "human_decision": "",
            "status": "ok"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "displayName": "date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow",
              "displayName": "workflow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "run_id",
              "displayName": "run_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "model",
              "displayName": "model",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "input_200",
              "displayName": "input_200",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "output",
              "displayName": "output",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "confidence",
              "displayName": "confidence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tokens_in",
              "displayName": "tokens_in",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tokens_out",
              "displayName": "tokens_out",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "cost_usd",
              "displayName": "cost_usd",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "latency_ms",
              "displayName": "latency_ms",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "human_decision",
              "displayName": "human_decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "fb3a4276-811b-44d9-bc1f-8f1256a309ae",
      "name": "Log AI step (classify)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1000,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You draft replies to customer emails for the support team of a small software company.\nWrite a short, polite reply (under 120 words) to the email below. Category: {{ $('Clean label').item.json.label }}.\nDo not promise refunds, discounts, dates or fixes; say what happens next and that a colleague will follow up if needed.\nSign off as \"Customer Support\". Reply with the email body only: no subject line, no commentary.\nEmail subject: {{ $('New email').item.json.subject }}\nEmail body: {{ $('New email').item.json.text }}",
        "batching": {}
      },
      "id": "c3a5e051-9218-4780-8587-e338326c2c2f",
      "name": "Draft reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.7,
      "position": [
        1240,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "07479c66-8723-4e78-8a55-f47ca461e627",
              "name": "message_id",
              "value": "={{ $('New email').item.json.id }}",
              "type": "string"
            },
            {
              "id": "9f9952fe-84e6-4e57-9c7e-60ab925ef390",
              "name": "to",
              "value": "={{ $('New email').item.json.from.text }}",
              "type": "string"
            },
            {
              "id": "29ae1c2e-c08e-4d89-b28e-3edc3bb4eed7",
              "name": "subject",
              "value": "=Re: {{ $('New email').item.json.subject }}",
              "type": "string"
            },
            {
              "id": "f3ae4573-cf1c-4122-ad5a-d20a453f0834",
              "name": "label",
              "value": "={{ $('Clean label').item.json.label }}",
              "type": "string"
            },
            {
              "id": "90e0025a-abf0-436c-85af-4dcb36eef861",
              "name": "reply",
              "value": "={{ $json.text.trim() }}",
              "type": "string"
            },
            {
              "id": "d6745936-d83f-43e2-8d2e-25068f110314",
              "name": "approver",
              "value": "PASTE-THE-APPROVER-GMAIL-ADDRESS-HERE",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "5f6f7339-175f-4dc8-b5d6-24e2e70f4075",
      "name": "Keep reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1560,
        0
      ]
    },
    {
      "parameters": {
        "operation": "sendAndWait",
        "sendTo": "={{ $json.approver }}",
        "subject": "=Approve this reply? {{ $json.subject }}",
        "message": "=The triage workflow wants to send this reply. Nothing is sent until you approve.\n\nTo: {{ $json.to }}\nSubject: {{ $json.subject }}\nCategory: {{ $json.label }}\n\n--- reply ---\n{{ $json.reply }}\n--- end ---\n\nNo answer within 24 hours: nothing is sent and the e-mail is labelled needs-human.",
        "responseType": "approval",
        "approvalOptions": {
          "values": {
            "approvalType": "double",
            "approveLabel": "Approve and send",
            "disapproveLabel": "Reject"
          }
        },
        "options": {
          "limitWaitTime": {
            "values": {
              "limitType": "afterTimeInterval",
              "resumeAmount": 24,
              "resumeUnit": "hours"
            }
          },
          "appendAttribution": false
        }
      },
      "id": "edd7009e-edc6-4eec-9ef6-ed597edbb1dd",
      "name": "Ask for approval",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1800,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "0e02e80c-3b3f-41f4-a890-8d41329af6e0",
              "name": "decision",
              "value": "={{ $json.data?.approved === true ? 'approved' : ($json.data?.approved === false ? 'rejected' : 'timed out') }}",
              "type": "string"
            },
            {
              "id": "1d678a9a-ae6e-492d-acb9-cd03a45a28b1",
              "name": "message_id",
              "value": "={{ $('Keep reply').item.json.message_id }}",
              "type": "string"
            },
            {
              "id": "e068ba89-706e-440b-8945-5af12159a5aa",
              "name": "to",
              "value": "={{ $('Keep reply').item.json.to }}",
              "type": "string"
            },
            {
              "id": "55c72397-353b-472c-a6b5-26494e60fd00",
              "name": "subject",
              "value": "={{ $('Keep reply').item.json.subject }}",
              "type": "string"
            },
            {
              "id": "54e92294-7ec9-4956-ba6b-3ad0910a38b8",
              "name": "reply",
              "value": "={{ $('Keep reply').item.json.reply }}",
              "type": "string"
            },
            {
              "id": "c4f33e38-f4c1-4fc9-af9b-42f4d5e47585",
              "name": "approver",
              "value": "={{ $json.data?.approved === undefined ? 'nobody (timed out)' : $('Keep reply').item.json.approver }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "782d8b05-1335-40f8-a3e7-e6fc1fecf0b8",
      "name": "Decision",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2040,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-M12-MONITORING-WORKBOOK"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "AI log"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "date": "={{ $now.toFormat('yyyy-MM-dd') }}",
            "workflow": "={{ $workflow.name }}",
            "run_id": "={{ $execution.id }}",
            "step": "Draft reply",
            "model": "gemini-2.5-flash",
            "input_200": "={{ ('Subject: ' + $('New email').item.json.subject + ' | ' + $('New email').item.json.text).replace(/[\\w.+-]+@[\\w-]+\\.[\\w.]+/g, '[email]').replace(/\\+?\\d[\\d \\-]{7,}\\d/g, '[phone]').slice(0, 200) }}",
            "output": "={{ $('Decision').item.json.reply.slice(0, 120) }}",
            "confidence": "",
            "tokens_in": "={{ Math.ceil((520 + ($('New email').item.json.subject + $('New email').item.json.text).length) / 4) }}",
            "tokens_out": "={{ Math.ceil($('Keep reply').item.json.reply.length / 4) }}",
            "cost_usd": "={{ ((Math.ceil((520 + ($('New email').item.json.subject + $('New email').item.json.text).length) / 4)) * 0.3 / 1e6 + (Math.ceil($('Keep reply').item.json.reply.length / 4)) * 2.5 / 1e6).toFixed(6) }}",
            "latency_ms": "",
            "human_decision": "={{ $('Decision').item.json.decision + ' by ' + $('Decision').item.json.approver }}",
            "status": "ok"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "displayName": "date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow",
              "displayName": "workflow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "run_id",
              "displayName": "run_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "model",
              "displayName": "model",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "input_200",
              "displayName": "input_200",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "output",
              "displayName": "output",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "confidence",
              "displayName": "confidence",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tokens_in",
              "displayName": "tokens_in",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tokens_out",
              "displayName": "tokens_out",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "cost_usd",
              "displayName": "cost_usd",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "latency_ms",
              "displayName": "latency_ms",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "human_decision",
              "displayName": "human_decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "4bc035b5-a3fc-40a7-ae88-d0af5de29228",
      "name": "Log AI step (draft)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        2280,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "82e0b628-b36d-4779-ab25-2f65d4fc8872",
              "leftValue": "={{ $('Decision').item.json.decision }}",
              "rightValue": "approved",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "144c91ef-2c19-4390-bf47-7d49df40471a",
      "name": "Approved?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2520,
        0
      ]
    },
    {
      "parameters": {
        "operation": "reply",
        "messageId": "={{ $('Decision').item.json.message_id }}",
        "emailType": "text",
        "message": "={{ $('Decision').item.json.reply }}",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "96a72f89-89a2-443b-9cda-edb582e68fb3",
      "name": "Send reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2760,
        -120
      ]
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Decision').item.json.message_id }}",
        "labelIds": [
          "PICK-LABEL-needs-human"
        ]
      },
      "id": "c273c80e-a55f-4555-a538-02ce068cf0af",
      "name": "Label needs human",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2760,
        140
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-M12-MONITORING-WORKBOOK"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Approval log"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "workflow": "={{ $workflow.name }}",
            "run_id": "={{ $execution.id }}",
            "action_proposed": "=Reply to {{ $('Decision').item.json.to }}: {{ $('Decision').item.json.subject }}",
            "tier": "4",
            "approver": "={{ $('Decision').item.json.approver }}",
            "decision": "={{ $('Decision').item.json.decision }}",
            "final_content": "={{ $('Decision').item.json.reply }}",
            "notes": "sent as approved"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow",
              "displayName": "workflow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "run_id",
              "displayName": "run_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "action_proposed",
              "displayName": "action_proposed",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tier",
              "displayName": "tier",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "approver",
              "displayName": "approver",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "decision",
              "displayName": "decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "final_content",
              "displayName": "final_content",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "notes",
              "displayName": "notes",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "259b9ff2-f2ca-4062-b146-49aafae389bd",
      "name": "Log sent",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        3000,
        -120
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-M12-MONITORING-WORKBOOK"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Approval log"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "workflow": "={{ $workflow.name }}",
            "run_id": "={{ $execution.id }}",
            "action_proposed": "=Reply to {{ $('Decision').item.json.to }}: {{ $('Decision').item.json.subject }}",
            "tier": "4",
            "approver": "={{ $('Decision').item.json.approver }}",
            "decision": "={{ $('Decision').item.json.decision }}",
            "final_content": "",
            "notes": "=not sent; labelled needs-human ({{ $('Decision').item.json.decision }})"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow",
              "displayName": "workflow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "run_id",
              "displayName": "run_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "action_proposed",
              "displayName": "action_proposed",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "tier",
              "displayName": "tier",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "approver",
              "displayName": "approver",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "decision",
              "displayName": "decision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "final_content",
              "displayName": "final_content",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "notes",
              "displayName": "notes",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "a23d79bb-3540-4694-91a1-068d3ed9bb89",
      "name": "Log not sent",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        3000,
        140
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-M12-MONITORING-WORKBOOK"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Needs human"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
            "date": "={{ $now.toFormat('yyyy-MM-dd') }}",
            "workflow": "={{ $workflow.name }}",
            "run_id": "={{ $execution.id }}",
            "step": "={{ $prevNode.name }}",
            "subject": "={{ $('New email').item.json.subject }}",
            "from": "={{ $('New email').item.json.from.text }}",
            "error": "={{ ($json.error && ($json.error.message || $json.error.description)) || $json.error || 'see Executions' }}"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "date",
              "displayName": "date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "workflow",
              "displayName": "workflow",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "run_id",
              "displayName": "run_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "step",
              "displayName": "step",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "subject",
              "displayName": "subject",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "from",
              "displayName": "from",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "error",
              "displayName": "error",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "f2c0c6ab-d33b-45a1-b658-baf442d96160",
      "name": "Needs human (error)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('New email').item.json.id }}",
        "labelIds": [
          "PICK-LABEL-needs-human"
        ]
      },
      "id": "f1de51a4-f705-42ba-9f8e-27688910b8be",
      "name": "Label needs human (error)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1240,
        300
      ]
    },
    {
      "parameters": {
        "content": "## M12 Triage with AI log\nThe Module 10 gated triage flow, instrumented (Module 12, lab parts B and D).\n\n**Start timer** stamps the input. **Classify email** now answers `label|confidence`. **Log AI step (classify)** appends one row to the **AI log** tab: date, run id, step, model, the redacted input (200 chars, e-mails and phones replaced), output, confidence, tokens, cost, latency. After the gate, **Log AI step (draft)** writes the second AI call with the human decision.\n\nBoth AI steps: Settings, Retry On Fail (3 tries, 5 s: n8n caps the wait at 5 s) and On Error = **Continue (using error output)**: the red output goes to **Needs human (error)** + the needs-human label. Nothing is retried after the gate: a send may already have happened.",
        "width": 820,
        "height": 300,
        "color": 5
      },
      "id": "20f06193-953d-4316-8aa5-0f2d0883e0d3",
      "name": "Note cfb4",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -470
      ]
    },
    {
      "parameters": {
        "content": "## Tokens and cost\nThe LLM Chain outputs only `text`; the exact token count sits in Executions, Gemini node, `tokenUsage`. The log row estimates tokens as characters / 4 and prices them at the Gemini 2.5 Flash list price (USD 0.30 in, 2.50 out per million). Good enough to see a trend; use the vendor bill for the budget.",
        "width": 520,
        "height": 190,
        "color": 4
      },
      "id": "b057f7c5-f5e2-478a-8600-79da5a55056d",
      "name": "Note 7b67",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1000,
        -300
      ]
    },
    {
      "parameters": {
        "content": "## Force a failure (demo)\nOpen **Gemini (flash)**, change the model name to a model that does not exist, send a test e-mail. Watch Classify retry three times, then the error output: a **Needs human** row and the label. Restore the model name.",
        "width": 520,
        "height": 170,
        "color": 3
      },
      "id": "e968c5f6-b94c-4ef6-8734-a66845a96707",
      "name": "Note 1193",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1560,
        -300
      ]
    }
  ],
  "connections": {
    "New email": {
      "main": [
        [
          {
            "node": "Start timer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start timer": {
      "main": [
        [
          {
            "node": "Classify email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini (flash)": {
      "ai_languageModel": [
        [
          {
            "node": "Classify email",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Draft reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Classify email": {
      "main": [
        [
          {
            "node": "Clean label",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Needs human (error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean label": {
      "main": [
        [
          {
            "node": "Log AI step (classify)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log AI step (classify)": {
      "main": [
        [
          {
            "node": "Draft reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft reply": {
      "main": [
        [
          {
            "node": "Keep reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Needs human (error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Keep reply": {
      "main": [
        [
          {
            "node": "Ask for approval",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ask for approval": {
      "main": [
        [
          {
            "node": "Decision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decision": {
      "main": [
        [
          {
            "node": "Log AI step (draft)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log AI step (draft)": {
      "main": [
        [
          {
            "node": "Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Approved?": {
      "main": [
        [
          {
            "node": "Send reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label needs human",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send reply": {
      "main": [
        [
          {
            "node": "Log sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label needs human": {
      "main": [
        [
          {
            "node": "Log not sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Needs human (error)": {
      "main": [
        [
          {
            "node": "Label needs human (error)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Amman"
  },
  "meta": {}
}