{
  "id": "S2fxW8jbFIetu8Vi",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI-Powered n8n Workflow Architecture Decision Engine",
  "tags": [],
  "nodes": [
    {
      "id": "d5997a32-7d1a-404a-a6c2-83fc4ff3bdaa",
      "name": "Receive Problem Description via POST",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -384,
        544
      ],
      "parameters": {
        "path": "af08511e-f5d9-44e3-8a2f-2482b5c3e4a0",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2.1
    },
    {
      "id": "b709e130-d9f0-4542-974f-f1c65b43e116",
      "name": "Extract Request Body",
      "type": "n8n-nodes-base.code",
      "position": [
        -176,
        544
      ],
      "parameters": {
        "jsCode": "// Get the first incoming item\nconst inputData = $input.first().json;\n\n// Return only the body\nreturn [\n  {\n    json: inputData.body\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "8217efaf-8a7f-4e10-8f31-ca8415405eb6",
      "name": "Multi-Agent Architecture Decision Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        32,
        544
      ],
      "parameters": {
        "text": "=Analyze the following problem description and decide whether it requires a multi-agent workflow in n8n.\n\nIf it requires multi-agent architecture, design the agents with node types and reasons.\n\nIf it does not require multi-agent architecture, clearly state that and provide a simpler workflow instead.\n\nProblem Description:\n{{ $json.description }}",
        "options": {
          "systemMessage": "=You are an n8n Multi-Agent Workflow Architect.\n\nYour job is to analyze a given problem description and decide whether it requires a multi-agent workflow or not.\n\nDecision Rules:\n1. If the problem involves multiple independent reasoning steps (classification, extraction, validation, risk scoring, decision making, routing, follow-ups), then design a multi-agent workflow.\n2. If the problem is simple (single-step logic, direct transformation, or basic automation), DO NOT design a multi-agent workflow.\n\nIf Multi-Agent is Required:\n- Clearly define:\n  - Agent Name\n  - Purpose\n  - n8n Node Type\n  - Why it is required\n- Keep explanations short and precise.\n- Provide logical flow order.\n\nIf Multi-Agent is NOT Required:\n- Clearly state: \"This does not require a multi-agent workflow.\"\n- Provide a simpler workflow logic.\n- List minimal required nodes.\n- Keep it concise.\n\nOutput Format:\n\nDecision: (Multi-Agent Required / Not Required)\n\nIf Required:\n1. Agent Name\n   - Purpose:\n   - Node:\n   - Reason:\n\nWorkflow Flow:\nStep 1 \u2192\nStep 2 \u2192\nStep 3 \u2192\n\nIf Not Required:\nSimplified Workflow:\n- Node 1:\n- Node 2:\n- Node 3:\n\nBe precise. No long explanations."
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "9b3114b1-6179-45de-9f0c-ac9ced81ed5c",
      "name": "Azure OpenAI GPT-4o-mini",
      "type": "@n8n/n8n-nodes-langchain.lmChatAzureOpenAi",
      "position": [
        32,
        976
      ],
      "parameters": {
        "model": "gpt-4o-mini",
        "options": {}
      },
      "credentials": {
        "azureOpenAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f368874a-c30a-4a57-85ae-b66ccf877a0a",
      "name": "Parse Decision, Agents & Steps",
      "type": "n8n-nodes-base.code",
      "position": [
        464,
        544
      ],
      "parameters": {
        "jsCode": "const items = $input.all();\n\nreturn items.map(item => {\n\n  const rawText = item.json.output || \"\";\n\n  // Extract Decision\n  const decisionMatch = rawText.match(/Decision:\\s*(.*)/);\n  const decision = decisionMatch ? decisionMatch[1].trim() : null;\n\n  // Extract Agents\n  const agentRegex = /Agent Name:\\s*(.*?)\\n\\s*- Purpose:\\s*(.*?)\\n\\s*- Node:\\s*(.*?)\\n\\s*- Reason:\\s*(.*?)(?=\\n\\n|\\n\\d+\\. Agent Name:|$)/gs;\n\n  let agents = [];\n  let match;\n\n  while ((match = agentRegex.exec(rawText)) !== null) {\n    agents.push({\n      name: match[1]?.trim() || \"\",\n      purpose: match[2]?.trim() || \"\",\n      node: match[3]?.trim() || \"\",\n      reason: match[4]?.trim() || \"\",\n    });\n  }\n\n  // Extract Workflow Steps\n  const stepsRegex = /Step \\d+:\\s*(.*?)\\s*\u2192/g;\n  let steps = [];\n  let stepMatch;\n\n  while ((stepMatch = stepsRegex.exec(rawText)) !== null) {\n    steps.push(stepMatch[1]?.trim() || \"\");\n  }\n\n  return {\n    json: {\n      decision,\n      agents,\n      workflow_flow: steps\n    }\n  };\n});"
      },
      "typeVersion": 2
    },
    {
      "id": "7344bc04-e81b-47c1-8809-7563dad58a7b",
      "name": "Build HTML Architecture Report",
      "type": "n8n-nodes-base.code",
      "position": [
        672,
        544
      ],
      "parameters": {
        "jsCode": "const items = $input.all();\n\nreturn items.map(item => {\n\n  const data = item.json;\n\n  const decisionClass =\n    data.decision === \"Multi-Agent Required\"\n      ? \"multi\"\n      : \"simple\";\n\n  const agentsHTML = Array.isArray(data.agents)\n    ? data.agents.map(agent => `\n        <div class=\"agent-card\">\n          <div class=\"agent-name\">${agent.name || \"\"}</div>\n\n          <div class=\"label\">Purpose</div>\n          <div>${agent.purpose || \"\"}</div>\n\n          <div class=\"label\">Node</div>\n          <div>${agent.node || \"\"}</div>\n\n          <div class=\"label\">Reason</div>\n          <div>${agent.reason || \"\"}</div>\n        </div>\n      `).join(\"\")\n    : \"\";\n\n  const stepsHTML = Array.isArray(data.workflow_flow)\n    ? data.workflow_flow.map(step =>\n        `<div class=\"step\">${step}</div>`\n      ).join(\"\")\n    : \"\";\n\n  const html = `\n  <html>\n  <head>\n    <style>\n      body {\n        font-family: 'Segoe UI', sans-serif;\n        background: #f4f7fb;\n        padding: 40px;\n      }\n\n      .card {\n        background: white;\n        border-radius: 18px;\n        padding: 28px;\n        margin-bottom: 30px;\n        box-shadow: 0 15px 40px rgba(0,0,0,0.06);\n      }\n\n      .decision {\n        font-size: 22px;\n        font-weight: 600;\n        padding: 14px 22px;\n        border-radius: 12px;\n        display: inline-block;\n      }\n\n      .multi {\n        background: linear-gradient(135deg, #e8f5e9, #c8e6c9);\n        color: #1b5e20;\n      }\n\n      .simple {\n        background: linear-gradient(135deg, #fff3e0, #ffe0b2);\n        color: #e65100;\n      }\n\n      .title {\n        font-size: 24px;\n        font-weight: 600;\n        margin-bottom: 25px;\n      }\n\n      .agent-grid {\n        display: grid;\n        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));\n        gap: 22px;\n      }\n\n      .agent-card {\n        background: #f9fbff;\n        border-radius: 16px;\n        padding: 22px;\n        border: 1px solid #e6ecf5;\n        transition: 0.3s ease;\n      }\n\n      .agent-card:hover {\n        transform: translateY(-6px);\n        box-shadow: 0 10px 25px rgba(0,0,0,0.08);\n      }\n\n      .agent-name {\n        font-size: 18px;\n        font-weight: 600;\n        margin-bottom: 12px;\n        color: #2c3e50;\n      }\n\n      .label {\n        font-size: 13px;\n        font-weight: 600;\n        margin-top: 12px;\n        color: #7b8ca8;\n      }\n\n      .steps {\n        display: flex;\n        flex-wrap: wrap;\n        gap: 14px;\n      }\n\n      .step {\n        background: #eef3ff;\n        padding: 12px 18px;\n        border-radius: 30px;\n        font-size: 14px;\n        color: #3f51b5;\n      }\n    </style>\n  </head>\n\n  <body>\n\n    <div class=\"card\">\n      <div class=\"decision ${decisionClass}\">\n        ${data.decision || \"No Decision\"}\n      </div>\n    </div>\n\n    ${agentsHTML ? `\n    <div class=\"card\">\n      <div class=\"title\">Agents</div>\n      <div class=\"agent-grid\">\n        ${agentsHTML}\n      </div>\n    </div>\n    ` : \"\"}\n\n    ${stepsHTML ? `\n    <div class=\"card\">\n      <div class=\"title\">Workflow Flow</div>\n      <div class=\"steps\">\n        ${stepsHTML}\n      </div>\n    </div>\n    ` : \"\"}\n\n  </body>\n  </html>\n  `;\n\n  return {\n    json: {\n      html\n    }\n  };\n});"
      },
      "typeVersion": 2
    },
    {
      "id": "5f52eae8-6d9a-4be0-b2a2-6aeac4af1ba0",
      "name": "Return HTML Report to Caller",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        880,
        544
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{$node['Build HTML Architecture Report'].json['html']}}"
      },
      "typeVersion": 1.5
    },
    {
      "id": "26c1a118-683d-4cbd-a8b1-308b8b28312d",
      "name": "Sticky Note - Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        384
      ],
      "parameters": {
        "width": 540,
        "height": 384,
        "content": "## \ud83d\udfe1 Workflow Overview\n\n### How it works\nThis workflow serves as an **AI-powered n8n Multi-Agent Architecture Advisor**. It accepts a plain-text problem description via a POST webhook and uses an Azure OpenAI-backed AI Agent to decide whether the problem warrants a multi-agent workflow or a simpler single-flow automation.\n\nThe AI Agent applies structured decision rules: if the problem involves multiple independent reasoning steps \u2014 such as classification, validation, routing, or risk scoring \u2014 it designs a full multi-agent architecture with named agents, n8n node types, and logical flow. If the problem is simple, it recommends a minimal node list instead.\n\nThe raw AI output is then parsed to extract the decision, agent definitions, and workflow steps. Finally, a styled HTML report is generated and returned directly in the webhook response as a visual card-based dashboard.\n\n### Setup steps\n1. **Activate the workflow** in n8n to register the Webhook URL.\n2. **Configure Azure OpenAI credentials** \u2014 ensure the `Azure Open AI account` credential is valid and the `gpt-4o-mini` deployment is active in your Azure resource.\n3. **Send a POST request** to the webhook URL with body: `{ \"description\": \"Your problem description here\" }`\n4. The response will be a styled HTML page with the architecture decision and agent breakdown.\n\n### Customization\n- Replace `gpt-4o-mini` with `gpt-4o` for higher reasoning accuracy on complex problems.\n- Modify the AI Agent system prompt to add new decision rules or output formats.\n- Update card styles in the **Build HTML Architecture Report** node to match your brand."
      },
      "typeVersion": 1
    },
    {
      "id": "8fef77e7-2fd2-4ae8-9455-083f6bfe0588",
      "name": "Sticky Note - Input Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        384
      ],
      "parameters": {
        "color": 7,
        "width": 388,
        "height": 326,
        "content": "## \ud83d\udce5 Input & Extraction\nReceives a POST request with a `description` field and extracts the raw request body for downstream processing."
      },
      "typeVersion": 1
    },
    {
      "id": "25685a95-fea8-49f1-8ccf-ff9d4130e7b1",
      "name": "Sticky Note - AI Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        384
      ],
      "parameters": {
        "color": 7,
        "width": 432,
        "height": 374,
        "content": "## \ud83e\udd16 AI Architecture Decision\nThe AI Agent evaluates the problem description and decides if a multi-agent workflow is needed. It returns structured agent definitions, node types, and a logical workflow flow."
      },
      "typeVersion": 1
    },
    {
      "id": "6e185fad-3128-41e4-9342-ba1a9dde7a87",
      "name": "Sticky Note - Output Section",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        448,
        384
      ],
      "parameters": {
        "color": 7,
        "width": 592,
        "height": 374,
        "content": "## \ud83d\udcca Parse, Build & Respond\nParses the AI output into structured decision, agents, and steps. Renders a styled HTML dashboard and returns it as the final webhook response."
      },
      "typeVersion": 1
    },
    {
      "id": "6e67700a-9b33-4794-891a-1df42143db3b",
      "name": "Sticky Note - Azure Warning",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -192,
        816
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 300,
        "content": "\u26a0\ufe0f **Azure OpenAI Credentials Required**\nThis node requires a valid `azureOpenAiApi` credential with an active `gpt-4o-mini` deployment. Missing or misconfigured credentials will cause all AI analysis to fail."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8f430c84-ba4f-4cc6-a871-ded693d19507",
  "connections": {
    "Extract Request Body": {
      "main": [
        [
          {
            "node": "Multi-Agent Architecture Decision Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Azure OpenAI GPT-4o-mini": {
      "ai_languageModel": [
        [
          {
            "node": "Multi-Agent Architecture Decision Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Build HTML Architecture Report": {
      "main": [
        [
          {
            "node": "Return HTML Report to Caller",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Decision, Agents & Steps": {
      "main": [
        [
          {
            "node": "Build HTML Architecture Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive Problem Description via POST": {
      "main": [
        [
          {
            "node": "Extract Request Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Multi-Agent Architecture Decision Agent": {
      "main": [
        [
          {
            "node": "Parse Decision, Agents & Steps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}