How I Built an LLM Council to Solve Code Hallucinations in n8n
From the publisher’s feed, collected in the ZeroLabs newsroom.
I used to have a big problem when asking AI models for help with coding. Often, the AI would give me an answer that looked very confident but was sometimes wrong. This is what people call a hallucination. I tried fixing this by just using larger and larger models, but even the best ones make mistakes.That is when I decided to try something completely different. Instead of relying on one single model, I built a team of models in n8n to check each other’s work. I call this my LLM Council.Here is exactly how I built this system and the rules I gave to each AI agent.Before we look at the exact steps, here is a quick summary of how my n8n system works:I tried addressing the hallucination issue by adding models from many different makers, so I am not just relying on OpenAI or Google.Not every prompt requires the LLM council, so the system filters the easy questions out first.I built a Confidence Gate that reads all the answers and gives a score.A Switch node looks at the score to decide what path to take next.If the confidence is high, an All Clear AI agent picks the best answer and cleans it up.If the confidence is low, the system escalates the issue to a massive top coder LLM to fix the problem on its ownBelow is the screenshot of my n8n workflow and also attached n8n workflow json at end of blog.Step 1: Decoding the User Prompt Not every question needs a whole team of models to answer it. The council takes time and computing power, so I only want to use it for hard software engineering problems.To do this, I built a filter using an ‘If’ rule right at the start. When a user sends a message, my system decodes the prompt to check if it is related to software engineering. It looks for specific words: “code”, “function”, “javascript”, “refactor”, or “rewrite”.If the user query does not have these words, it means the question is not about software engineering. In that case, the LLM council does not get involved. The question just goes straight to a basic AI agent powered by the deepseek-r1:1.5b model to give a quick answer.Step 2: Convening the Council If the prompt does contain one of those coding words, the question is sent to the council. This council is made of three different agents. Here is how I wrote the system prompts for every AI agent.The First Drafter (Agent 1): This agent uses the qwen3-next:80b-cloud model to write the first attempt at an answer. I kept its prompt very simple: “You are a helpful assistant. Answer the user’s question accurately. Dont write too much explanation or summary just provide answer with little summary of it.”The First Checker (Agent 2): The first answer is then sent to Agent 2, which runs on the minimax-m2.7:cloud model. This is where the fact checking begins. I gave it a very strict prompt: “You are a strict fact-checker and corrector. Your only job is to review the original user question and the previous assistant’s answer. Rules — follow them exactly:If the previous answer is factually correct and directly answers the question output it exactly as-is. Do not add any extra text, explanations, or wrappers.If the previous answer contains any factual error, hallucination, or is irrelevant rewrite it to be 100% correct, concise, and natural.Never say things like ‘The previous response was wrong because…’. Never add meta-commentary.Output only the final answer, nothing else.”The Final Reviewer (Agent 3): To be extra safe, I pass everything to Agent 3, powered by the gpt-oss:120b-cloud model. It looks at the original question, Agent 1’s answer, and Agent 2’s answer. It follows the exact same strict fact checking rules as Agent 2.Step 3: Building the Confidence Gate After the council does its work, I need to know if they actually solved the problem. To do this, I built a Confidence Gate.This gate uses the huge mistral-large-3:675b-cloud model. It acts as a judge. It reads the original question and the three answers from the council. I wrote a very specific prompt so that it only outputs a score and nothing else: “You are a confidence evaluator and final answer selector agent. Your ONLY job is to output a single integer from 1 to 9… Rules you MUST follow: Output EXACTLY one number and nothing else. No explanation. No reasoning. No meta-commentary. No text before or after the number. No full stop… Just the raw number… If you output anything else, you have failed the task.”Step 4: Escalation vs. Success Finally, the system looks at the score from the Confidence Gate to decide what to do next. I use a Switch rule for this.If confidence is high (Score is 5 or more): This means the council did a great job and agrees on the answer. The system consults my “All Clear” AI agent, powered by the qwen3.5:397b-cloud model. Its system prompt tells it: “You have simple job… There are three answers to above question. You need to choose the correct and rewrite it.” It gives the final, clean answer to the user.If confidence is low (Score is 5 or less): This means the council got confused or failed to find a good answer. When this happens, I am escalating this to my absolute best coding model, the Cloud FinalBoss. This agent uses the massive qwen3-coder:480b-cloud model. It takes over the task completely to solve the really hard coding problem without relying on the council’s confused answers.By building the system this way, I rarely ever see hallucinations anymore. The models catch each other’s mistakes, and the system knows exactly when to ask a bigger model for help.— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —Quick Setup GuideIf you want to build something like this yourself, here is a quick guide on how to get started. Note that these steps are not in the workflow file, but this is how I set my tools up.1. How to install n8n The simplest way to install n8n is by using Docker. If you have Docker installed on your computer, you can run a single terminal command to spin up the n8n server. You can also use npm if you prefer using Node.js.2. How to install Ollama You will need Ollama to run some of these AI models. Go to the official Ollama website and download the installer for your computer. Click the file, and it will install automatically.3. How to use Ollama Cloud Models Inside your n8n workspace, you will need to connect your Ollama account. Go to the credentials menu and create a new setup for the Ollama API. When you are building your workflow, add an Ollama Chat Model block. In the settings of that block, you can type the name of the cloud model you want, like “minimax-m2.7:cloud”, and it will pull the model directly.{ "name": "My workflow", "nodes": [ { "parameters": { "options": {} }, "type": "@n8n/n8n-nodes-langchain.chatTrigger", "typeVersion": 1.1, "position": [ -656, 688 ], "id": "8c6bd33f-7343-4e7e-9845-622a50cccd9f", "name": "When chat message received", "webhookId": "e1aa2edd-1cba-4172-847b-5b72b9adc992" }, { "parameters": { "options": { "systemMessage": "You are a helpful assistant. Answer the user's question accurately. Dont write too much explanation or summary just provide answer with little summary of it." } }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 2, "position": [ -208, 384 ], "id": "6baaa8e4-de74-4b66-a582-06b47a3328fb", "name": "Agent 1" }, { "parameters": { "promptType": "define", "text": "=You are a strict fact-checker and corrector. Your only job is to review the original user question and the previous assistant's answer.\n\nRules — follow them exactly:\n1. If the previous answer is factually correct and directly answers the question → output it exactly as-is. Do not add any extra text, explanations, or wrappers.\n2. If the previous answer contains any factual error, hallucination, or is irrelevant → rewrite it to be 100% correct, concise, and natural.\n3. Never say things like \"The previous response was wrong because...\". Never add meta-commentary.\n4. Output only the final answer, nothing else.\n\nOriginal question: {{ $('When chat message received').item.json.chatInput }}\nPrevious assistant answer: {{ $json.output }}\n", "options": {} }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 2, "position": [ 144, 480 ], "id": "7c5847b7-ec34-4a85-aa7a-2230834493bd", "name": "Agent 2" }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 }, "conditions": [ { "id": "1458e226-6ef3-4149-80f5-fcc4e2efbb22", "leftValue": "={{ $json.chatInput }}", "rightValue": "code", "operator": { "type": "string", "operation": "contains" } }, { "id": "5aae3ca3-35a2-41f4-a856-82b09439782b", "leftValue": "={{ $json.chatInput }}", "rightValue": "function", "operator": { "type": "string", "operation": "contains" } }, { "id": "dc28eea6-bf0d-4b2d-a833-51a2ea571a51", "leftValue": "={{ $json.chatInput }}", "rightValue": "javascript", "operator": { "type": "string", "operation": "contains" } }, { "id": "0cdc0ac4-6630-4439-bda6-b2162d1c3bd5", "leftValue": "={{ $json.chatInput }}", "rightValue": "refactor", "operator": { "type": "string", "operation": "contains" } }, { "id": "41dbc9ea-9b80-4814-bb4f-a631a9e7cee9", "leftValue": "={{ $json.chatInput }}", "rightValue": "rewrite", "operator": { "type": "string", "operation": "contains" } } ], "combinator": "or" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.2, "position": [ -432, 688 ], "id": "34cb69dc-3cf8-4b88-a186-3b1f28b98945", "name": "If" }, { "parameters": { "options": {} }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 2, "position": [ -192, 832 ], "id": "f702b617-b6b4-41ca-b943-4df84ce2918a", "name": "AI Agent" }, { "parameters": { "model": "deepseek-r1:1.5b", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ -144, 1056 ], "id": "eeed2ac7-dc90-4776-a4b9-ec96d8822b39", "name": "Ollama Chat Model", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "rules": { "values": [ { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "leftValue": "={{ Number($json.output) }}", "rightValue": 5, "operator": { "type": "number", "operation": "lte" }, "id": "4d3b7f32-e7c4-41b5-bd9c-89ad8e1c1769" } ], "combinator": "and" } }, { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "19ccbebc-6d58-408e-815a-99e735da8fa4", "leftValue": "={{ Number($json.output) }}", "rightValue": 5, "operator": { "type": "number", "operation": "gte" } } ], "combinator": "and" } } ] }, "options": {} }, "type": "n8n-nodes-base.switch", "typeVersion": 3.4, "position": [ 1200, 480 ], "id": "dcd4a5f2-c6bb-44e5-b0ff-7e3370c08500", "name": "Switch", "alwaysOutputData": false }, { "parameters": { "promptType": "define", "text": "=", "options": { "systemMessage": "=You are a strict fact-checker and corrector. Your only job is to review the original user question and the previous assistant's answer.\n\nRules — follow them exactly:\n1. If the previous answer is factually correct and directly answers the question → output it exactly as-is. Do not add any extra text, explanations, or wrappers.\n2. If the previous answer contains any factual error, hallucination, or is irrelevant → rewrite it to be 100% correct, concise, and natural.\n3. Never say things like \"The previous response was wrong because...\". Never add meta-commentary.\n4. Output only the final answer, nothing else.\n\nOriginal question: {{ $('When chat message received').item.json.chatInput }}\nAgent 1 answer: {{ $('Agent 1').item.json.output }}\nAgent 2 answer: {{ $json.output }}\n" } }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 2, "position": [ 496, 480 ], "id": "ccbf6e7f-7972-4325-b8da-c233c6c8ced2", "name": "Agent 3" }, { "parameters": { "promptType": "define", "text": "=", "options": { "systemMessage": "=You are a confidence evaluator and final answer selector agent. Your ONLY job is to output a single integer from 1 to 9.\n\nQuestion: {{ $('When chat message received').item.json.chatInput }}\n\nAnswer 1: {{ $('Agent 1').item.json.output }}\n\nAnswer 2: {{ $('Agent 2').item.json.output }}\n\nAnswer 3: {{ $json.output }}\n\nRules you MUST follow:\n- Output EXACTLY one number and nothing else.\n- No explanation.\n- No reasoning.\n- No meta-commentary.\n- No text before or after the number.\n- No full stop, period, or any other character.\n- Just the raw number like this:\n\n9\n\nIf you output anything else, you have failed the task.\n\nRespond with only a single digit number between 1 and 9. Begin your response immediately with the number." } }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 3, "position": [ 848, 480 ], "id": "f98d8f08-239a-40c2-aa3c-30c62fa3eeaa", "name": "Confidence Gate" }, { "parameters": { "model": "minimax-m2.7:cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ 224, 704 ], "id": "abdc0bcd-ce9b-4e18-84bf-b7a803155ee9", "name": "minimax m 2.7 cloud", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "model": "qwen3-coder:480b-cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ 1504, 400 ], "id": "98019b42-bdb8-4427-b963-810f215dbae3", "name": "qwen3 coder 480b2", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "model": "qwen3-next:80b-cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ -144, 608 ], "id": "52cc87ea-28fd-4e53-a58e-c688ca373b41", "name": "qwen3-next:80b", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "model": "gpt-oss:120b-cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ 576, 704 ], "id": "8c030054-188d-4628-838c-84250702dda9", "name": "gpt oss 120b", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "model": "qwen3.5:397b-cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ 1504, 912 ], "id": "2f6fcab8-bbf7-4311-ad76-f9fc66009e56", "name": "qwen 397b", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } }, { "parameters": { "promptType": "define", "text": "=", "options": { "systemMessage": "=You have simple job. User just asked a question which is below. \n\n{{ $('When chat message received').item.json.chatInput }}\n\nThere are three answers to above question. You need to choose the correct and rewrite it.\n\nAnswer 1:\n{{ $('Agent 1').item.json.output }}\n\nAnswer 2:\n{{ $('Agent 2').item.json.output }}\n\nAnswer 3: \n{{ $('Agent 3').item.json.output }}\n" } }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 3, "position": [ 1424, 688 ], "id": "86972f1b-e47f-4d9d-881c-98d808112e0f", "name": "Agent allClear" }, { "parameters": { "options": {} }, "type": "@n8n/n8n-nodes-langchain.agent", "typeVersion": 3, "position": [ 1424, 176 ], "id": "03dbda30-5b61-46b7-a465-f1cf57d9db66", "name": "Cloud FinalBoss" }, { "parameters": { "model": "mistral-large-3:675b-cloud", "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOllama", "typeVersion": 1, "position": [ 928, 704 ], "id": "3ed2e631-a069-4b12-8a11-31b4e149fca3", "name": "Mistral large 671b", "credentials": { "ollamaApi": { "id": "2EoRJwgvKk64nuKe", "name": "Ollama account 4" } } } ], "pinData": {}, "connections": { "When chat message received": { "main": [ [ { "node": "If", "type": "main", "index": 0 } ] ] }, "Agent 1": { "main": [ [ { "node": "Agent 2", "type": "main", "index": 0 } ] ] }, "Agent 2": { "main": [ [ { "node": "Agent 3", "type": "main", "index": 0 } ] ] }, "If": { "main": [ [ { "node": "Agent 1", "type": "main", "index": 0 } ], [ { "node": "AI Agent", "type": "main", "index": 0 } ] ] }, "Ollama Chat Model": { "ai_languageModel": [ [ { "node": "AI Agent", "type": "ai_languageModel", "index": 0 } ] ] }, "Switch": { "main": [ [ { "node": "Cloud FinalBoss", "type": "main", "index": 0 } ], [ { "node": "Agent allClear", "type": "main", "index": 0 } ] ] }, "Agent 3": { "main": [ [ { "node": "Confidence Gate", "type": "main", "index": 0 } ] ] }, "Confidence Gate": { "main": [ [ { "node": "Switch", "type": "main", "index": 0 } ] ] }, "minimax m 2.7 cloud": { "ai_languageModel": [ [ { "node": "Agent 2", "type": "ai_languageModel", "index": 0 } ] ] }, "qwen3 coder 480b2": { "ai_languageModel": [ [ { "node": "Cloud FinalBoss", "type": "ai_languageModel", "index": 0 } ] ] }, "qwen3-next:80b": { "ai_languageModel": [ [ { "node": "Agent 1", "type": "ai_languageModel", "index": 0 } ] ] }, "gpt oss 120b": { "ai_languageModel": [ [ { "node": "Agent 3", "type": "ai_languageModel", "index": 0 } ] ] }, "qwen 397b": { "ai_languageModel": [ [ { "node": "Agent allClear", "type": "ai_languageModel", "index": 0 } ] ] }, "Mistral large 671b": { "ai_languageModel": [ [ { "node": "Confidence Gate", "type": "ai_languageModel", "index": 0 } ] ] } }, "active": false, "settings": { "executionOrder": "v1" }, "versionId": "be24fb93-b2fe-4703-8af3-3bf302f91865", "meta": { "templateCredsSetupCompleted": true, "instanceId": "ec45ea17523c6a1aaa8225b7fd9fc3f3c31288867f2be6b9ba35b91e9cbd600c" }, "id": "oifAZPWCK9Resiqn", "tags": []}How I Built an LLM Council to Solve Code Hallucinations in n8n was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.