This workflow corresponds to n8n.io template #13966 — we link there as the canonical source.
This workflow follows the Agent → OpenAI Chat recipe pattern — see all workflows that pair these two integrations.
The workflow JSON
Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →
{
"nodes": [
{
"id": "b316dd7b-3d2f-4365-a0f7-3389caaa6263",
"name": "Webhook Trigger",
"type": "n8n-nodes-base.webhook",
"position": [
-1392,
80
],
"parameters": {
"path": "5b4bf456-6536-4d2e-b505-80e58201a458",
"options": {},
"httpMethod": "POST"
},
"typeVersion": 2.1
},
{
"id": "48c43fc1-86dc-4454-a6ce-2cfa581f3e7b",
"name": "Workflow Configuration",
"type": "n8n-nodes-base.set",
"position": [
-1120,
80
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "id-1",
"name": "confidenceThreshold",
"type": "number",
"value": 0.7
},
{
"id": "id-2",
"name": "cheapModelCostPer1kTokens",
"type": "number",
"value": 0.00015
},
{
"id": "id-3",
"name": "expensiveModelCostPer1kTokens",
"type": "number",
"value": 0.005
}
]
},
"includeOtherFields": true
},
"typeVersion": 3.4
},
{
"id": "fc83230f-9772-4b50-835e-b394760bed2e",
"name": "Check Confidence Threshold",
"type": "n8n-nodes-base.if",
"position": [
-224,
80
],
"parameters": {
"options": {},
"conditions": {
"options": {
"leftValue": "",
"caseSensitive": true,
"typeValidation": "loose"
},
"combinator": "and",
"conditions": [
{
"id": "id-1",
"operator": {
"type": "boolean",
"operation": "true"
},
"leftValue": "={{ $json.should_escalate }}"
}
]
}
},
"typeVersion": 2.3
},
{
"id": "882dcd8e-b574-4cd3-8ff2-762a2a40533c",
"name": "Calculate Cost Difference",
"type": "n8n-nodes-base.code",
"position": [
400,
96
],
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "// Get data from previous nodes\nconst confidenceData = $('Confidence Evaluator').item.json;\nconst configData = $('Workflow Configuration').first().json;\nconst cheapModelData = $('Cheap Model (GPT-5-mini)').item.json;\n\n// Check if request was escalated\nconst wasEscalated = confidenceData.confidence < configData.confidence_threshold;\n\n// Get cost rates from configuration\nconst cheapModelCostPer1kTokens = configData.cheap_model_cost_per_1k_tokens || 0.00015;\nconst expensiveModelCostPer1kTokens = configData.expensive_model_cost_per_1k_tokens || 0.005;\n\nlet modelUsed, totalTokens, costUsd, costDifferenceUsd;\n\nif (wasEscalated) {\n // Request was escalated to expensive model\n const expensiveModelData = $('Expensive Model (GPT-5.4)').item.json;\n modelUsed = 'gpt-4o';\n totalTokens = expensiveModelData.usage?.total_tokens || 0;\n costUsd = (totalTokens / 1000) * expensiveModelCostPer1kTokens;\n \n // Calculate what it would have cost with cheap model\n const cheapModelTokens = cheapModelData.usage?.total_tokens || 0;\n const cheapModelCost = (cheapModelTokens / 1000) * cheapModelCostPer1kTokens;\n \n // Additional cost due to escalation (positive number)\n costDifferenceUsd = costUsd - cheapModelCost;\n} else {\n // Request stayed with cheap model\n modelUsed = 'gpt-4o-mini';\n totalTokens = cheapModelData.usage?.total_tokens || 0;\n costUsd = (totalTokens / 1000) * cheapModelCostPer1kTokens;\n \n // Calculate what it would have cost with expensive model\n const expensiveModelCost = (totalTokens / 1000) * expensiveModelCostPer1kTokens;\n \n // Cost savings (negative number indicates savings)\n costDifferenceUsd = costUsd - expensiveModelCost;\n}\n\n// Return the result\nreturn {\n model_used: modelUsed,\n total_tokens: totalTokens,\n cost_usd: parseFloat(costUsd.toFixed(6)),\n escalated: wasEscalated,\n cost_difference_usd: parseFloat(costDifferenceUsd.toFixed(6)),\n response: wasEscalated ? $('Expensive Model (GPT-5.4)').item.json.message?.content : cheapModelData.message?.content,\n confidence_score: confidenceData.confidence\n};"
},
"typeVersion": 2
},
{
"id": "095bccdd-3b90-424b-909e-122fe400dce1",
"name": "Format Final Response",
"type": "n8n-nodes-base.set",
"position": [
672,
96
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "id-1",
"name": "response",
"type": "string",
"value": "={{ $('Expensive Model (GPT-5.4)').item.json.message?.content || $('Cheap Model (GPT-5-mini)').item.json.message?.content }}"
},
{
"id": "id-2",
"name": "model_used",
"type": "string",
"value": "={{ $('Calculate Cost Difference').item.json.model_used }}"
},
{
"id": "id-3",
"name": "confidence_score",
"type": "number",
"value": "={{ $('Confidence Evaluator').item.json.output?.confidence_score }}"
},
{
"id": "id-4",
"name": "escalated",
"type": "boolean",
"value": "={{ $('Calculate Cost Difference').item.json.escalated }}"
},
{
"id": "id-5",
"name": "cost_usd",
"type": "number",
"value": "={{ $('Calculate Cost Difference').item.json.cost_usd }}"
},
{
"id": "id-6",
"name": "cost_analysis",
"type": "object",
"value": "={{ JSON.stringify($('Calculate Cost Difference').item.json.cost_analysis) }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "35c1daed-bcfb-41f6-9276-4dc6652e9205",
"name": "Confidence Evaluator",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
-592,
80
],
"parameters": {
"text": "=Original query: {{ $('Workflow Configuration').first().json.query }}\n\nCheap model response: {{ $json.message.content }}",
"options": {
"systemMessage": "You are a confidence evaluator. Analyze the quality and completeness of the AI response provided.\n\nEvaluate based on:\n1. Accuracy and correctness\n2. Completeness of the answer\n3. Clarity and coherence\n4. Relevance to the query\n\nReturn a confidence score between 0 and 1, where:\n- 0.9-1.0: Excellent response, highly confident\n- 0.7-0.89: Good response, confident\n- 0.5-0.69: Acceptable but could be better\n- Below 0.5: Poor response, needs improvement\n\nProvide a brief reason for your assessment and indicate whether escalation to a better model is recommended."
},
"promptType": "define",
"hasOutputParser": true
},
"typeVersion": 3
},
{
"id": "0338bd7b-58e9-4577-8288-d7bb4fcb03ca",
"name": "Parse Confidence JSON",
"type": "@n8n/n8n-nodes-langchain.outputParserStructured",
"position": [
-384,
320
],
"parameters": {
"jsonSchemaExample": "{\n\t\"confidence_score\": 0.85,\n\t\"reason\": \"Brief explanation of the confidence assessment\",\n\t\"should_escalate\": false\n}"
},
"typeVersion": 1.3
},
{
"id": "367f6833-da36-4b37-9b12-6cfb16ee9e93",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
-624,
320
],
"parameters": {
"model": {
"__rl": true,
"mode": "id",
"value": "gpt-4o-mini"
},
"options": {},
"builtInTools": {}
},
"typeVersion": 1.3
},
{
"id": "d5329d77-80d1-433d-bfe6-3a13b32db8c0",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
-320,
-64
],
"parameters": {
"color": 7,
"width": 272,
"height": 320,
"content": "## Check Confidence Threshold\nDecision node that determines whether the response quality is sufficient."
},
"typeVersion": 1
},
{
"id": "86941910-a70c-4a2d-a5a2-fc6d5439b4e6",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-2032,
-96
],
"parameters": {
"width": 464,
"height": 496,
"content": "## Cost-Optimized AI Model Routing\n\nThis workflow demonstrates a cost-aware AI routing strategy that balances response quality and API cost. Instead of always using an expensive model, the workflow first generates a response using a lower-cost AI model. A confidence evaluator then reviews the response quality.\n\nThe workflow also estimates token usage and cost differences, helping measure the savings achieved through cost-optimized AI routing.\n\n### How it works\n\nWebhook Trigger receives a user query.\n\nA cheap AI model generates the initial response.\n\nA confidence evaluator agent analyzes the response quality.\n\nIf confidence is low, the request is escalated to a stronger model.\n\nThe workflow calculates token usage and cost difference.\n\nThe final response and cost analysis are returned. "
},
"typeVersion": 1
},
{
"id": "4ccfe423-62d7-4a2f-b079-0cae59970413",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-16,
-128
],
"parameters": {
"color": 7,
"width": 288,
"height": 384,
"content": "## Expensive Model (GPT-5.4)\n\nHandles queries that require higher quality reasoning.\nTriggered only when the confidence score is below the threshold."
},
"typeVersion": 1
},
{
"id": "9b4e4e10-5251-4cc9-abde-54133883f307",
"name": "Sticky Note4",
"type": "n8n-nodes-base.stickyNote",
"position": [
336,
-128
],
"parameters": {
"color": 7,
"width": 224,
"height": 384,
"content": "## Calculate Cost Difference\n\nCalculates token usage and estimated cost of the request.\nDetermines which model was used and compares costs between the cheap and expensive models."
},
"typeVersion": 1
},
{
"id": "a3b8794a-523b-4961-bfbb-56b2f373144d",
"name": "Sticky Note5",
"type": "n8n-nodes-base.stickyNote",
"position": [
-624,
-80
],
"parameters": {
"color": 7,
"width": 272,
"height": 304,
"content": "## Confidence Evaluator\nAnalyzes the cheap model\u2019s response to determine its quality.\nEvaluates accuracy, completeness, clarity, and relevance."
},
"typeVersion": 1
},
{
"id": "c3b6e1ff-507e-4b84-b6bf-5053e749ecaa",
"name": "Sticky Note6",
"type": "n8n-nodes-base.stickyNote",
"position": [
-896,
-128
],
"parameters": {
"color": 7,
"height": 352,
"content": "## Cheap Model (GPT-5-mini)\n\nProcesses the user query using a low-cost AI model.\nThis step provides an initial response while minimizing token costs."
},
"typeVersion": 1
},
{
"id": "dd2cc699-9b00-4e96-b932-1732e7eea5e4",
"name": "Sticky Note8",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1184,
-128
],
"parameters": {
"color": 7,
"height": 368,
"content": "## Workflow Configuration\n\nDefines key configuration settings used across the workflow.\nIncludes the confidence threshold and token pricing"
},
"typeVersion": 1
},
{
"id": "a02448ce-02ae-41bf-8e4f-98c983883ff8",
"name": "Sticky Note9",
"type": "n8n-nodes-base.stickyNote",
"position": [
592,
-128
],
"parameters": {
"color": 7,
"height": 384,
"content": "## Format Final Response\n\nPrepares the final structured output returned by the workflow."
},
"typeVersion": 1
},
{
"id": "53f9248c-0c2e-4481-9617-b0102edae917",
"name": "Sticky Note10",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1456,
-96
],
"parameters": {
"color": 7,
"width": 256,
"height": 336,
"content": "## Webhook Trigger\n\nStarts the workflow when an external application sends a request.\n"
},
"typeVersion": 1
},
{
"id": "21d21b1c-5bf3-4ef1-a424-bb89dd282a84",
"name": "Cheap Model (GPT-5-mini)",
"type": "@n8n/n8n-nodes-langchain.openAi",
"position": [
-880,
80
],
"parameters": {
"modelId": {
"mode": "id",
"value": "gpt-4o-mini"
},
"options": {},
"responses": {
"values": [
{
"content": "={{ $json.query }}"
}
]
},
"builtInTools": {}
},
"typeVersion": 2.1
},
{
"id": "041cee60-6892-4324-a259-fb528479adf9",
"name": "Expensive Model (GPT-5.4)",
"type": "@n8n/n8n-nodes-langchain.openAi",
"position": [
0,
32
],
"parameters": {
"modelId": {
"mode": "id",
"value": "gpt-4o"
},
"options": {},
"responses": {
"values": [
{
"content": "={{ $('Workflow Configuration').first().json.query }}"
}
]
},
"builtInTools": {}
},
"typeVersion": 2.1
}
],
"connections": {
"Webhook Trigger": {
"main": [
[
{
"node": "Workflow Configuration",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "Confidence Evaluator",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Confidence Evaluator": {
"main": [
[
{
"node": "Check Confidence Threshold",
"type": "main",
"index": 0
}
]
]
},
"Parse Confidence JSON": {
"ai_outputParser": [
[
{
"node": "Confidence Evaluator",
"type": "ai_outputParser",
"index": 0
}
]
]
},
"Workflow Configuration": {
"main": [
[
{
"node": "Cheap Model (GPT-5-mini)",
"type": "main",
"index": 0
}
]
]
},
"Cheap Model (GPT-5-mini)": {
"main": [
[
{
"node": "Confidence Evaluator",
"type": "main",
"index": 0
}
]
]
},
"Calculate Cost Difference": {
"main": [
[
{
"node": "Format Final Response",
"type": "main",
"index": 0
}
]
]
},
"Expensive Model (GPT-5.4)": {
"main": [
[
{
"node": "Calculate Cost Difference",
"type": "main",
"index": 0
}
]
]
},
"Check Confidence Threshold": {
"main": [
[
{
"node": "Expensive Model (GPT-5.4)",
"type": "main",
"index": 0
}
],
[
{
"node": "Calculate Cost Difference",
"type": "main",
"index": 0
}
]
]
}
}
}
For the full experience including quality scoring and batch install features for each workflow upgrade to Pro
About this workflow
This workflow implements a cost-optimized AI routing system using n8n. It intelligently decides whether a request should be handled by a low-cost model or escalated to a higher-quality model based on response confidence.
Source: https://n8n.io/workflows/13966/ — original creator credit. Request a take-down →
Related workflows
Workflows that share integrations, category, or trigger type with this one. All free to copy and import.
leads. Uses supabase, gmail, formTrigger, httpRequest. Webhook trigger; 62 nodes.
Build an intelligent WhatsApp assistant that automatically responds to customer messages using AI. This template uses the Evolution API community node for WhatsApp integration and OpenAI for natural l
This workflow automatically processes new free-trial / lead sign-ups in real time: Catches a webhook from any source (Webflow form, Intercom, custom agent, etc.) Filters out personal / disposable / .e
This workflow turns raw product inputs into a complete, launch-ready AI-generated social media campaign package. It accepts product details via webhook, sanitizes messy fields, generates a strategic c
Logo Animator - Supabase. Uses postgres, openAi, agent, toolThink. Webhook trigger; 26 nodes.