{
  "name": "Nodbot - OpenAI n8n production AI workflow with validation and fallback",
  "nodes": [
    {
      "parameters": {
        "path": "integration-openai-n8n",
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "id": "a017d44d-27b8-4935-a741-90ea7f241d76",
      "name": "Webhook input",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        260
      ]
    },
    {
      "parameters": {
        "jsCode": "const result = $json;\nconst allowed = new Set($json.allowed_labels ?? ['incident','billing','feature_request','spam']);\nlet ai = result.ai_output ?? result.output ?? result;\nif (typeof ai === 'string') {\n  try { ai = JSON.parse(ai); } catch (e) { throw new Error('OpenAI returned non-JSON output'); }\n}\nif (!allowed.has(ai.label)) throw new Error(`Invalid AI label: ${ai.label}`);\nconst confidence = Number(ai.confidence ?? 0);\nif (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) throw new Error('Invalid confidence');\nconst needsApproval = confidence < 0.82 || result.requires_human_approval === true || ai.risk === 'high';\nreturn [{ json: {\n  ticket_id: result.ticket_id,\n  label: ai.label,\n  confidence,\n  summary: String(ai.summary ?? '').slice(0, 600),\n  next_action: ai.next_action ?? 'review',\n  needs_human_approval: needsApproval,\n  cost_guard_ok: Number(result.estimated_cost_usd ?? 0) <= Number(result.max_cost_usd ?? 0.05),\n  audit: { model: result.model ?? 'openai', validated_at: new Date().toISOString() }\n}}];"
      },
      "id": "f1dd5075-ccb1-43fe-97a3-3b7f65537fd8",
      "name": "Prepare prompt and trim",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        260
      ]
    },
    {
      "parameters": {},
      "id": "cb4c12bc-02ba-4aa1-a76c-4a95f09bb28d",
      "name": "OpenAI request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        560,
        260
      ]
    },
    {
      "parameters": {
        "jsCode": "const result = $json;\nconst allowed = new Set($json.allowed_labels ?? ['incident','billing','feature_request','spam']);\nlet ai = result.ai_output ?? result.output ?? result;\nif (typeof ai === 'string') {\n  try { ai = JSON.parse(ai); } catch (e) { throw new Error('OpenAI returned non-JSON output'); }\n}\nif (!allowed.has(ai.label)) throw new Error(`Invalid AI label: ${ai.label}`);\nconst confidence = Number(ai.confidence ?? 0);\nif (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) throw new Error('Invalid confidence');\nconst needsApproval = confidence < 0.82 || result.requires_human_approval === true || ai.risk === 'high';\nreturn [{ json: {\n  ticket_id: result.ticket_id,\n  label: ai.label,\n  confidence,\n  summary: String(ai.summary ?? '').slice(0, 600),\n  next_action: ai.next_action ?? 'review',\n  needs_human_approval: needsApproval,\n  cost_guard_ok: Number(result.estimated_cost_usd ?? 0) <= Number(result.max_cost_usd ?? 0.05),\n  audit: { model: result.model ?? 'openai', validated_at: new Date().toISOString() }\n}}];"
      },
      "id": "ae24f969-5a21-4f6c-a768-377c3a0414a3",
      "name": "Validate AI output",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        840,
        260
      ]
    },
    {
      "parameters": {},
      "id": "eb6b374d-fa3d-460b-91a3-bb4a54d1f083",
      "name": "Human approval gate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1120,
        260
      ]
    },
    {
      "parameters": {},
      "id": "82ab53a7-84bb-4c94-899f-8209226890d4",
      "name": "Business action",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1400,
        260
      ]
    }
  ],
  "connections": {
    "Webhook input": {
      "main": [
        [
          {
            "node": "Prepare prompt and trim",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare prompt and trim": {
      "main": [
        [
          {
            "node": "OpenAI request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI request": {
      "main": [
        [
          {
            "node": "Validate AI output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate AI output": {
      "main": [
        [
          {
            "node": "Human approval gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Human approval gate": {
      "main": [
        [
          {
            "node": "Business action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    "nodbot",
    "integration",
    "production"
  ],
  "triggerCount": 1,
  "updatedAt": "2026-05-30T00:00:00.000Z",
  "versionId": "f6fd3ed4-f013-41a1-ba35-7ef06a551b1a",
  "meta": {
    "templateCredsSetupCompleted": false
  }
}