
How to Build an AI Agent with n8n: A Beginner’s Step-by-Step Guide

How to Build an AI Agent with n8n: A Beginner’s Step-by-Step Guide
You build an AI agent in n8n by adding one AI Agent node to a workflow, connecting a chat model that supports tool calling, and giving the agent a tool it can choose to use. In this guide you build a small support assistant that reads a question, searches a demonstration FAQ, writes a draft answer, and flags anything uncertain for a person. The easiest first setup is n8n Cloud with a low-cost hosted model such as gpt-4o-mini, and a working version takes roughly 30 to 60 minutes. Everything it produces is a draft. Nothing reaches a customer automatically.
- An agent is not a chatbot with a nicer prompt. It becomes an agent when the model can decide to take an action, such as searching your FAQ, before it answers.
- The model you pick must support tool calling. This is the single most common reason a first build quietly fails.
- Forcing a structured JSON output is what lets the workflow route an uncertain answer to a human instead of guessing from free text.
- Cost splits four ways: the n8n plan, model usage, hosting, and your time. Self-hosting is free software, not a free setup.
A support inbox on a Monday has forty messages in it, and maybe thirty of them have been answered before in almost the same words. Where is my order. Do you ship here. What is the return window.
Someone still opens each one, remembers the policy, and types it out again. That repetition is the obvious thing to hand to software, and it is also the thing people get wrong first. The usual attempt is to point a language model at the inbox and let it write replies. It works for a while, and then it confidently invents a refund policy your company has never offered.
The fix is not a cleverer model. It is giving the model a way to look things up, and a way to say "I do not know this one, a person should take it." That gap, between a thing that writes and a thing that checks before it writes, is the difference between a text generator and an agent. It is also what you are about to build.
What an AI agent actually is
An AI agent is a program that uses a language model to decide what to do, not just what to say. A chatbot takes your text and writes a reply in one step. An agent can choose to take an action first, such as searching a document, and then use what it found to answer. n8n's documentation describes an agent as something that "uses a language model to decide which actions to take," making the model the decision-maker rather than just the writer.
Six words come up repeatedly. Here is what each means in practice.
| Term | What it means here |
|---|---|
| Node | One block in a workflow that does a single job, such as "run the AI agent" or "check a condition." You join nodes with lines to pass data along. |
| Trigger | The node that starts a workflow. Here it is where a sample question enters. |
| Credential | A saved, encrypted login such as an API key, so n8n can talk to another service. Set it once, reuse it. |
| API | How two programs talk to each other. When n8n uses a model, it talks to that model's API. |
| Tool calling | The model's ability to say "I want to use this tool now, with this input." The model does not run the tool. n8n runs it and hands back the result. Only some models can do this, which matters a lot later. |
| Memory | An optional add-on so the agent remembers earlier messages. n8n notes this memory does not persist between sessions. |
If you only ask a model to write an answer from a prompt, with no tool and no decision, that is a text generator, however it is labelled. The build below is a genuine agent because the model chooses whether to call the FAQ tool, chooses what to search for, and decides whether a human is needed. It is also deliberately not autonomous. A person reviews anything uncertain, and that is a design choice, not a missing feature.
For the background on how agents reason, the types of agents, and the safety thinking behind them, our explainer on what AI agents are and how they work is the companion to this hands-on guide.
What you will build
An internal first-draft support assistant. A question goes in, the agent searches a small FAQ, and a draft comes out with a flag on it saying whether a person needs to look before it goes anywhere.
That distinction matters more than anything else in this guide, so it is worth naming directly.
The trigger that brings the question in, the test on the review flag, and the two steps that store the result. These run identically every time.
Whether and when to call the FAQ tool, what to search for, how to word the draft, whether something is missing, and whether a person should take over.
Before you start: Cloud or self-hosting?
For a first agent, use n8n Cloud. It removes server setup and networking, which is where most first builds die. Self-hosting is a good option once you want privacy or a local model, but it adds moving parts that are easy to get wrong on day one.
| n8n Cloud | Self-host, your machine | Self-host, Docker or server | |
|---|---|---|---|
| Setup effort | Lowest. Sign up and build | Medium. Install and run it | Higher. Container, server, upkeep |
| Maintained by | n8n | You | You |
| Cost shape | Monthly subscription | Free software, you pay for the machine | Free software, you pay for the server |
| Reach a model on your laptop? | No. It runs on n8n's servers | Yes, with the right address | Yes, with the right address |
| Best for | A first working agent, fast | Local testing and learning | A durable, private setup |
You also need a chat model that supports tool calling. The simplest choice is an OpenAI account with a small amount of API credit. You do not need a database, a website, or any real customer data. The FAQ is fictional sample data that lives inside the workflow.
Common failureWhy "localhost" is a trap if you self-host
Beginners point n8n at a model running on their own computer using localhost, then get a connection refused error. Every environment has its own idea of what localhost means. n8n Cloud cannot reach your laptop at all, because it runs on n8n's servers. And when n8n runs inside Docker, localhost means "inside this container," not your computer. Full address table in section 08.
Building it, step by step
Open n8n and create a new, empty workflow. Each step says what to do and how to tell it worked.
- Add a trigger and a sample question
Click the
PASS the node outputs question with your text in it+on the canvas, search for Manual Trigger, and add "When clicking 'Execute workflow'." This is just a start button for testing. Then add an Edit Fields node (also called Set) after it and rename it to "Sample question." Add one field calledquestion, type String, set to a fictional message such as "Do you ship to Canada and how long does it take?" Click Execute step. - Add the AI Agent node
Add a node after "Sample question" and search for AI Agent. In current n8n versions this node runs as a Tools Agent, which is what lets it call tools. Set Source for Prompt to "Define below," then put
PASS the node shows connector points underneath for a chat model, memory, tool and output parser. A warning until you attach a model is expected{{ $json.question }}in the Prompt (User Message) field so the sample question feeds in. - Connect the brain
Click the Chat Model connector under the agent and add OpenAI Chat Model. Under "Credential to connect with" choose Create new credential and paste your API key. Never paste a real key into a screenshot or a shared doc. Set Model to
PASS no credential error. A 401 means the key is wrong or billing is not set upgpt-4o-mini, which is low cost and supports tool calling. - Give the agent a tool
This is the step that turns a chatbot into an agent. Click the Tool connector and add a Custom Code Tool. Name it
PASS the tool is on the agent's Tool port with a name and descriptionfaq_lookup. The description matters more than people expect, because the model reads it to decide when to use the tool. Use: "Search the company FAQ for an answer. Input: a short search phrase describing what the customer is asking about. Returns matching FAQ entries, or an empty list if nothing matches." Then paste the code below. - Give it instructions and a fixed output shape
In the agent node open Options and add a System Message, using the instructions in section 05. Then turn on Require Specific Output Format. A new Output Parser connector appears. Click it, add a Structured Output Parser, and give it the JSON example below so every answer returns in the same shape.
PASS the agent now has a model, a tool and an output parser attached - Route uncertain answers to a human
Add an IF node after the agent. Add one Boolean condition with the value
PASS two clean branches leave the IF node{{ $json.output.needs_human_review }}and the operator "is true." On the true branch add an Edit Fields node called "Needs human review" settingstatustoreview. On the false branch add one called "Draft ready" settingstatustoready.
// Demonstration FAQ (fictional sample data). Replace with your own later.
const faq = [
{ id: "SHIP-INTL", q: "international shipping countries and time",
a: "We ship to the US, Canada, the UK, and Australia. International orders arrive in 7 to 12 business days." },
{ id: "RETURNS", q: "returns refund policy window",
a: "Items can be returned within 30 days of delivery for a full refund, if unused and in original packaging." },
{ id: "ORDER-TRACK", q: "track order tracking number status",
a: "A tracking link is emailed when an order ships. Customers can also see status under Account, then Orders." },
{ id: "PAYMENT", q: "payment methods cards accepted",
a: "We accept Visa, Mastercard, and American Express. We do not accept cash on delivery." }
];
// The agent's search text arrives as the tool input (the `query` variable).
const text = (query || "").toLowerCase();
const words = text.split(/\s+/).filter(Boolean);
const matches = faq.filter(item => {
const haystack = (item.q + " " + item.a).toLowerCase();
return words.some(w => haystack.includes(w));
});
return JSON.stringify(matches.length ? matches : [{ note: "No FAQ match found." }]);
The snippet above is illustrative and was not run on a live instance for this article. How the Custom Code Tool exposes the agent's input can vary by n8n version. Check it on your own instance and adjust the variable name if yours differs.
{
"draft_answer": "A short, friendly draft reply to the customer.",
"used_faq": true,
"matched_faq_ids": ["SHIP-INTL"],
"needs_human_review": false,
"missing_information": "",
"confidence": "high"
}
Forcing a fixed JSON shape is what makes the next step reliable. Without it, the IF node would have to infer a yes or no out of a paragraph of prose, which fails the moment the model phrases things differently.
Every node setting in one place
Use this as the build checklist. Anything not listed stays at its default.
| Node | Role | Key settings |
|---|---|---|
| Manual Trigger | Start button | None |
| Edit Fields "Sample question" | Holds a test question | question (String) set to your sample message |
| AI Agent | The agent itself | Source for Prompt: Define below. User Message: {{ $json.question }}. Require Specific Output Format: On. Options › System Message: see below. Max Iterations: 10 (default) |
| OpenAI Chat Model | The brain | Credential: your own API key. Model: gpt-4o-mini |
| Custom Code Tool | FAQ lookup | Name: faq_lookup. Description and Code as given above |
| Structured Output Parser | Forces clean JSON | The JSON contract given above |
| Memory | Optional | Leave out for version one. Add later for multi-turn chat |
| IF | Human review gate | Boolean: {{ $json.output.needs_human_review }} is true |
| Edit Fields ×2 | The two outcomes | status set to review on true, ready on false |
The rulebookThe agent's full system instructions
Paste this into the System Message field. It is written to be strict about drafts, honest about gaps, and clear about when to stop and ask.
You are a customer support drafting assistant for an online store.
Your job is to write a DRAFT reply for a human agent to review. You never
send messages to customers directly.
How to work:
1. Read the customer's question.
2. Use the faq_lookup tool to search the FAQ before you answer. Choose a
short, specific search phrase based on what the customer is asking.
3. Base your draft only on what the FAQ returns and on the customer's
question. Do not invent policies, prices, dates, or promises.
4. If the FAQ does not clearly cover the question, say so plainly in the
draft and set needs_human_review to true.
Rules:
- Be warm, brief, and clear. Use plain language. No emojis.
- If you are missing any information needed to answer correctly, set
needs_human_review to true and describe what is missing in
missing_information.
- Never guarantee anything the FAQ does not state.
- If the question involves a complaint, a refund dispute, a legal or safety
issue, or anything sensitive, set needs_human_review to true regardless
of what the FAQ says.
- Set confidence to "high", "medium", or "low" based on how well the FAQ
covered the question.
Always return your answer in the required structured format with these
fields: draft_answer, used_faq, matched_faq_ids, needs_human_review,
missing_information, confidence.
Testing it, and knowing it works
Change the question field, click Execute workflow, and read the agent's output. The table shows how a correctly configured agent should behave. These are expected behaviours for teaching, not measured results from a particular run.
| Sample question | What should happen | Expected flag |
|---|---|---|
| "Do you ship to Canada and how long?" | Calls the tool, finds SHIP-INTL, answers confidently | false, confidence high |
| "What's your return window?" | Finds RETURNS, cites 30 days | false |
| "Can I pay with cryptocurrency?" | FAQ does not cover it, so it says so | true, missing_information filled |
| "I want a refund, your product broke and I'm furious." | Sensitive complaint rule fires | true even if partly covered |
| "asdfgh" | No useful match | true, confidence low |
Run each one, open the agent's output, and check the JSON has all six fields. Confirm the IF node sends the first two to "Draft ready" and the last three to "Needs human review." To see the agent's working, turn on Return Intermediate Steps in Options and confirm it actually called the tool rather than answering from its own memory.
ReusableVerification checklist
- The agent calls
faq_lookupon a normal question, rather than answering from training - A clearly covered question returns
needs_human_review: false - A question the FAQ does not cover returns
true - A sensitive or complaint question returns
true - Every output has all six fields, every time
- The IF node routes both branches correctly
- The draft never promises anything the FAQ does not state
- No real API keys appear in the workflow, in screenshots, or in exports
When it breaks: what to check first
Five things go wrong far more often than anything else. Match what you see on screen to a column, then work down it.
| Symptom | Likely cause | What to check |
|---|---|---|
| 401 or invalid API key | Credential wrong, or no billing | Re-paste the key with no stray spaces, confirm the right provider, confirm credit is active, then use the credential's Test button |
| Agent never calls the tool | Model cannot tool-call, or the tool is vague | Confirm the model supports tool calling. Make the tool name and description specific. Tell the agent to use it in the system message |
| Connection refused | Wrong address between n8n and the model | See the address table in section 08. On an IPv6 error use http://127.0.0.1:11434. Make sure the model listens on 0.0.0.0 |
| Output is not valid JSON | Output format not enforced | Turn on Require Specific Output Format and connect the Structured Output Parser. Optionally add an Auto-fixing Output Parser. Keep the system message's field list matching the parser |
| IF always picks one branch | Wrong field path | Open the agent output and confirm the real path to needs_human_review, then update the IF expression |
| Nothing runs | Trigger or connections | Click Execute workflow, confirm the trigger has test data, confirm every node is actually joined up |
Running it on a local model with Ollama
You can run a model on your own hardware with Ollama instead of paying per token. For an agent there is one hard requirement: the model must support tool calling, and plenty of local models do not. Ollama supports tool calling with popular models such as Llama 3.1, and groups the capable ones under a Tools category in its model library. Pick from that category, pull the model, and check before you build. A model without tool support will simply never call faq_lookup.
Then swap the chat model for the Ollama Chat Model and set the Base URL in the credential. The correct address depends entirely on where n8n runs, which is where almost everyone gets stuck.
| Where n8n and Ollama run | Base URL | Note |
|---|---|---|
| Both on your own machine | http://localhost:11434 | On an IPv6 error, use http://127.0.0.1:11434 |
| n8n in Docker, Ollama on the host | http://host.docker.internal:11434 | On Linux, start the container with --add-host host.docker.internal:host-gateway |
| Ollama in Docker, n8n on your machine | http://localhost:11434 | Publish the port with -p 11434:11434 |
| Both in separate containers | http://ollama:11434 | Use the Ollama container's name |
| n8n Cloud, Ollama on your laptop | Not possible as is | Cloud runs on n8n's servers. You would need a secured public endpoint, an advanced setup |
One more rule from the docs: Ollama has to listen on 0.0.0.0, not only on localhost, or nothing in a container can reach it.
What an n8n AI agent costs
There is no single price. Cost splits four ways, and the parts behave very differently. All figures were verified in October 2026 and will drift, so check the linked pages before you budget anything.
Cost 1 of 4The n8n plan
n8n Cloud pricing, billed annually: Starter at 20 euros a month for 2,500 workflow executions, Pro at 50 euros for 10,000, Business at 667 euros for 40,000, Enterprise on request. An execution is one complete run of your workflow, regardless of how many steps it contains. There is a 14-day free trial with Pro-level features and 1,000 executions, no card required. Monthly billing costs more than the annual rates shown.
Cost 2 of 4Model usage
With a hosted model you pay per token. OpenAI lists gpt-4o-mini at 0.15 US dollars per million input tokens and 0.60 per million output tokens. Support questions are small. As an illustrative calculation, not a benchmark, a draft using roughly 2,000 input and 500 output tokens costs about 0.0006 dollars, comfortably under a cent. Measure your own usage rather than trusting that figure, because prompt length and FAQ size change it. With a local model through Ollama there is no per-token charge at all. The download is free and the cost is your hardware and electricity.
Cost 3 of 4Hosting, if you self-host
The self-hosted Community Edition is free software under the Sustainable Use License. n8n calls this fair-code and source available, which is not the same as fully open source, because it restricts some commercial uses. In practice you may run it free for your own company's internal work, including a business that makes money. You need a commercial licence if people outside your organisation build or configure workflows, if you host n8n as a service for paying customers, or if you need enterprise features such as single sign-on. Free software still is not a free setup: you pay for a machine to run it on, and local models want real RAM.
Cost 4 of 4Your time
On Cloud, updates, backups and uptime are someone else's job. Self-hosted, they are yours. No invoice shows that cost, which is exactly why it gets forgotten.
The cheapest way to learn is the Cloud trial and a few cents of model usage. The cheapest way to run privately at scale is self-hosting, paid for in server bills and your own attention.
Where this approach falls short
Worth being clear about the edges before you put anything near a customer.
- It is only as good as the FAQ. A thin or out-of-date FAQ produces confident, wrong drafts. The model will not make up for bad source content.
- Keyword matching is crude. The demo tool matches words, so a customer who phrases something unusually may get no match. A real version would search properly, or use a vector store.
- The review flag is a judgement, not a guarantee. The agent can be wrong about being right. Keep a person on the branch that matters until you have watched it for a while.
NextSensible upgrades, one at a time
- Add memory
So it can handle a back-and-forth conversation rather than single questions.
- Swap the demo FAQ for real content
Point the tool at your actual help centre or a vector store instead of the four sample entries.
- Send the draft where a person will see it
A shared inbox, a Slack channel, or your ticketing tool, on the review branch.
- Move the lookup into a sub-workflow
Using the Call n8n Workflow Tool, once it grows beyond a snippet.
Re-run the checklist after each change. One upgrade at a time is how you keep knowing what broke.
Frequently asked questions
Is this a real AI agent, or just a chatbot?
Do I need to know how to code to build an AI agent in n8n?
Which model should a beginner use for an n8n AI agent?
Can I run an n8n AI agent completely free?
Why does my n8n agent ignore the FAQ tool?
Why do I get a connection refused error with a local model in n8n?
How long does it take to build a first AI agent in n8n?
Taking this further than a demo FAQ?
Auriga IT builds AI and automation systems for businesses, including conversational AI, intelligent automation of manual workflows, and integration with existing systems. If you are weighing where an agent would genuinely help in your operations, we are happy to look at one workflow with you.
See our AI services →All verified 20 October 2026 against n8n 2.x documentation. Node names and settings reflect the current AI Agent (Tools Agent) node and may change in later versions.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s [...]






