How to Build an AI Agent in n8n (Step-by-Step, 2026)

To build an AI agent in n8n, add the AI Agent node, connect a chat model sub-node (OpenAI, Anthropic, or a local Ollama model), then attach tools like HTTP Request, Gmail, or Google Sheets. Write a system prompt defining its job, tools, and output format, and set a max iterations cap. A first agent takes about an afternoon.

I build n8n workflows for clients. A rigid pipeline runs the same steps every time. An AI agent reads an email, figures out what it's about, pulls the right data, and acts. That's the step up, and n8n is one of the easiest places to build your first one. This guide covers what an agent is, what n8n gives you out of the box, a step-by-step build of a lead-qualification agent, and the mistakes that bite almost everyone the first time.

What's the difference between an AI agent and a regular automation?

A regular automation is a fixed recipe. New row in Google Sheets, send a Slack message. Same steps, same order, every single run. That's still the right tool for most jobs.

An AI agent is different in one specific way: it decides. You give a language model a goal, a set of tools, and some context, and the model chooses which tools to call, in what order, and when it's done. The same agent might look up a CRM record on one run and skip it entirely on the next, because the input didn't need it.

Plain automation AI agent
Logic Fixed steps you define Model decides at runtime
Input Structured, predictable Messy text, varied requests
Tools Each node runs in sequence Agent picks tools as needed
Failure style Breaks loudly on bad data Can fail quietly or invent answers
Cost per run Fractions of a cent LLM tokens on every run

That last column matters. Agents handle ambiguity that would take you fifty IF nodes to cover, but they bring new failure modes with them. If all you need is a single ChatGPT call inside a workflow, with no agent picking its own tools, my guide to ChatGPT automation in n8n covers that simpler setup.

What does n8n's AI Agent node do?

The AI Agent node is a root node that orchestrates everything. On its own it does nothing; you plug sub-nodes into it:

Two newer features worth knowing about: you can require human approval before the agent executes specific tools, which is great for anything that sends messages or moves money. And n8n's evaluations feature runs a batch of test inputs through your workflow and scores the outputs, so you catch a broken prompt before your users do.

There's also $fromAI(), a small expression that lets the model fill in a tool's parameters itself. Point a Google Sheets tool at a spreadsheet, mark the filter value as $fromAI('status'), and the agent decides what to search for based on the conversation. Once that clicked for me, building tools got about five times faster.

How to build a lead-qualification agent in n8n, step by step

Here's a build I've done in several variations for clients: an agent that reads inbound leads, scores them, and routes the good ones to a human. You can swap the trigger and tools for a support-triage version with almost no structural changes.

  1. Add the trigger. Use a form trigger, a webhook from your website, or a Gmail trigger watching a leads inbox. The payload should carry the lead's message plus whatever metadata you have (email, company, source).

  2. Drop in the AI Agent node. Connect a chat model sub-node. I usually start with a mid-tier model for triage work and move up only if quality demands it.

  3. Write the system prompt. Define the agent's job, its tools, and its output format. Something like: "You qualify inbound leads for an automation consultancy. Use the enrichment tool to look up the company. Score the lead 1 to 10 based on budget signals, urgency, and fit. Output JSON with score, reason, and recommended_action." Be boring and explicit. The next section covers this in more depth.

  4. Attach tools. For this build: an HTTP Request tool pointed at an enrichment API (or even just the company website), a Google Sheets tool to log every lead and check for duplicates, and a Gmail tool to send the notification. Give each tool a clear name and a one-line description, because the model picks tools based on those descriptions, not on what the node actually does.

  5. Handle the output. After the agent, add an IF node on the score. High scores go to a Slack ping or an email to you, complete with the agent's reasoning. Low scores get logged and receive a polite automated reply.

  6. Test with real garbage. Skip the clean, well-written test leads. Feed it the one-line "how much" emails, messages in other languages, and spam. That's what production looks like.

  7. Activate it, then watch it for a week. Check the execution log daily at first and read what the agent actually did, not just the final output. Tool-call sequences show you where the prompt is vague.

I've built variations of this pattern as support agents with Shopify integration, voice agents, and SQL agents that answer questions over a database with RAG. If you want to see what these look like running rather than read about them, there are live demos on this site you can poke at.

How do you write a good system prompt for an agent?

Most agent problems are prompt problems wearing a disguise. What has worked for me across client builds:

How do you add memory, and when do you need it?

For a one-shot agent like the lead qualifier above, skip memory entirely. Each lead is independent, and memory just adds tokens and cost.

For a chat-style agent, attach a memory sub-node and set a session key, usually the user's email or chat ID, so each person gets their own thread. Simple Memory works fine for prototypes but lives in the n8n instance, so restarts can wipe it. For anything customers touch, use Postgres or Redis chat memory. And cap the context window length: a memory that drags forty old messages into every request gets expensive, and the model starts anchoring on stale context.

What goes wrong with n8n agents (and how to prevent it)

Every first agent I've reviewed had at least two of these.

Failure mode What it looks like The fix
Hallucinated tool inputs Agent invents an email address or row ID that doesn't exist Validate tool inputs in the workflow; never let the model free-type identifiers it should look up
Loops Agent calls the same tool repeatedly, burning tokens Set the max iterations option on the agent node; tighten tool descriptions so results are clearly final
No error handling One failed API call kills the run silently Add an error workflow, set on-error behavior per node, and alert yourself on failures
Overpowered tools Agent has delete or send permissions it rarely needs Scope credentials tightly; require human approval on risky tools
Prompt drift Works in testing, degrades on weird real inputs Build an evaluation set from real executions and re-run it after every prompt change

The common thread: treat the model as a clever but unsupervised intern. Give it narrow tools, check its work where it counts, and log everything.

What does it cost to run?

Two bills. First, n8n hosting: Cloud starts around €24/month on the Starter plan, while self-hosting the Community Edition is free software on a cheap VPS, often under $10/month, though then updates and backups are on you (my self-hosted n8n production checklist covers both). Second, the LLM API: a triage agent run typically costs a fraction of a cent to a few cents depending on the model and how many tools it calls per run. A few hundred leads a month on a mid-tier model usually lands in the single-digit dollars. The cost surprise almost always comes from loops or oversized memory, which is another reason to set iteration caps early.

When should you get help?

Build your first agent yourself. The pattern above is achievable in an afternoon, and you'll learn more from one wonky prototype than from any article. If you'd rather have a coding agent draft the workflow on the canvas for you, I cover n8n MCP with Claude Code, including how to keep it off your production workflows.

Where an experienced n8n consultant earns their fee is the unglamorous part: error handling, evaluation sets, scoped credentials, deduplication, and the judgment call about which steps should stay deterministic instead of being handed to a model. That last one matters more than people expect. A good AI automation consultant will often shrink the agent and grow the plain workflow around it, because boring and reliable beats impressive and flaky every time it runs unattended.

Want a second pair of eyes on your agent?

If you're planning an agent build, or you've got one that mostly works and occasionally embarrasses you, I'm happy to take a look. I'm Top Rated Plus on Upwork with a 100% Job Success Score across 80+ jobs, and my builds include support agents, voice agents, and RAG-based SQL agents. Book a 30-minute call and bring your messiest workflow. Worst case, you leave with a clear list of what to fix.

Frequently asked questions

Can n8n build AI agents?

Yes. n8n has a dedicated AI Agent node that connects a chat model, optional memory, and tools into a working agent. The agent decides at runtime which tools to call, and most regular n8n nodes, like Gmail, Google Sheets, Slack, and HTTP Request, can be attached as tools. It works on both n8n Cloud and self-hosted instances.

What LLMs work with n8n AI agents?

n8n provides chat model sub-nodes for OpenAI, Anthropic Claude, Google Gemini, Mistral, Groq, and local models through Ollama, plus any OpenAI-compatible API endpoint. The model must support tool calling, which current models from all major providers do. Swapping providers only requires changing one sub-node, so testing a cheaper model is easy.

Is n8n free for building AI agents?

The self-hosted Community Edition is free and includes the AI Agent node; you only pay for a server, often under $10 a month on a VPS, plus your LLM API usage. n8n Cloud starts around €24 a month on the Starter plan. LLM costs for a typical triage agent usually run from a fraction of a cent to a few cents per execution.

What is the difference between an AI agent and a regular n8n workflow?

A regular workflow runs fixed steps in a fixed order every time. An AI agent uses a language model to decide which tools to call and in what order, based on the input it receives. Agents handle messy, unpredictable inputs like emails and chat messages, while plain workflows remain cheaper and more reliable for structured, repetitive tasks.

How do you stop an n8n AI agent from looping or hallucinating?

Set the max iterations option on the AI Agent node to cap tool-call loops, and validate tool inputs in the workflow so the model can't invent record IDs or email addresses. Give the agent an explicit fallback in the system prompt, such as routing to manual review when unsure. n8n's evaluations feature and human-in-the-loop tool approval add further guardrails for production agents.