ai-automation · 11 min read

n8n AI Agent Tutorial: Build Your First Agent Step by Step

How to build an n8n AI agent: Chat Trigger, AI Agent node, chat model, memory and tools, plus $fromAI(), human approval and fixes for common errors.

Published

Reading time

11 min

Written by

KavioWeb

Cover for the n8n AI agent tutorial: build your first agent step by step

An n8n AI agent is the AI Agent node with a chat model and at least one tool connected to it. You send it a message, the model decides which tool to call, n8n runs that tool, and the model uses the result to answer. To build your first one, add a Chat Trigger, an AI Agent node, a chat model, a memory node and one tool, then test it in the built-in chat panel. This guide walks through each step using n8n's official docs (checked November 2026).

/m/190d414c1c8ccbf128b7500b-1600.webp

What an n8n AI agent actually is

n8n's docs describe the AI Agent node as the piece that lets you build an agent: you connect a chat model and one or more tools, and the agent decides which tools to call to complete a task. A plain workflow follows a fixed path you drew in advance. An agent uses a language model to pick the next action. n8n's own explanation is that a chain follows a predetermined sequence of calls, while an agent "uses a language model to determine which actions to take".

That difference decides when to use one:

Fixed workflow / chain

AI Agent

Who picks the next step

You, at build time

The model, at run time

Memory between messages

No (chains can't use memory)

Yes, with a memory sub-node

Tools

None for a basic LLM chain

Yes, at least one required

Best for

Predictable, repeatable jobs

Open-ended requests with several possible actions

If the job is "when a form arrives, add a row and send an email", build a normal workflow. If it is "answer whatever the customer asks and look things up as needed", an agent fits. Source: n8n, Agents vs chains and What agents do.

What you need before you start

  • A running n8n instance. Cloud works, and so does your own server. If you want your own, our guide to self-hosting n8n on a VPS with Docker Compose covers the setup.

  • Access to a language model: an API key from a provider n8n supports (the Tools Agent docs list OpenAI, Groq, Mistral Cloud, Anthropic and Microsoft Foundry chat models), or a model you host yourself with Ollama.

  • One small task for the agent to do. Start with a single tool, not five.

The n8n docs note that on n8n Cloud you can use the OpenAI Chat Model node with Gateway credits instead of your own OpenAI API key. If that applies to you, you can skip the key for a first test.

Step 1: Add a Chat Trigger

Create a new workflow and add the Chat Trigger node. n8n describes it as the node to use when building AI workflows for chatbots and other chat interfaces. It must be connected to an agent or chain root node.

Two settings matter:

  • Make Chat Publicly Available: leave it off while you build, and turn it on only when you want users to reach the chat. You test through the manual chat panel in the editor.

  • Mode: Hosted Chat gives you n8n's chat page with no extra setup. Embedded Chat is for putting the chat on your own site with n8n's chat widget or your own interface calling the webhook URL.

One cost note from the docs: every message sent to the Chat Trigger executes your workflow, so a conversation of 10 messages uses 10 executions of your plan's allowance.

Step 2: Add the AI Agent node and a chat model

Add the AI Agent node and connect it to the trigger. By default, the Prompt setting reads the user's message from the connected Chat Trigger node.

The agent will complain until it has a model. Click the + Chat Model connector at the bottom of the node and pick your provider, then add credentials. This is the "A Chat Model sub-node must be connected" error in n8n's troubleshooting page.

A note on the agent type: the old agent-type setting was deprecated in n8n 1.82.0, and every AI Agent node now works as a Tools Agent. Version 1 of the node, which still has that setting, will be removed in n8n 3.0, so use the latest node version when you build.

Step 3: Add memory so it remembers the conversation

Without memory, every message starts from scratch. The easiest option is the Simple Memory node, which keeps a customizable length of chat history for the current session. It has two parameters:

  • Session Key: the key used to store the memory in the workflow data.

  • Context Window Length: how many previous interactions to consider.

Two limits from the docs are worth knowing before you go live. First, the Tools Agent page says memory doesn't persist between sessions. Second, Simple Memory doesn't work in an active production workflow if your instance uses queue mode, because n8n can't guarantee every call goes to the same worker. If you plan to scale out, look at the database-backed memory nodes n8n provides: Postgres Chat Memory, Redis Chat Memory, Motorhead, Xata and Zep.

Source: n8n, How memory works and Simple Memory.

Step 4: Give the agent a tool

The AI Agent node requires at least one tool sub-node. n8n ships built-in tools for services like Wikipedia and SerpAPI, plus a registry of MCP servers you can connect in one click. Three tools are the most flexible:

Tool

What it does

Call n8n Workflow Tool

Loads any other n8n workflow as a tool

Custom Code Tool

Runs code you write

HTTP Request Tool

Calls an API or fetches a web page

For a first agent, the Call n8n Workflow Tool is a good choice because you can build the real logic as an ordinary workflow you can test on its own. Fill in the Description carefully. That text tells the agent when to use the tool, so "Looks up an order by its order number and returns the status" works better than "Order tool".

Two gotchas from the docs:

  • If you pick the sub-workflow from the Database source, it must be published before the agent can run it in production. Otherwise the tool returns Workflow is not active and cannot be executed, and that text goes back to the agent as the tool result, which is easy to miss inside a longer reply.

  • Tool-calling support depends on the model. If you run models locally, check that the model you pull supports tools; Ollama's docs describe tool calling and use qwen3 in their examples.

/m/6abeeddae838dfacad89936d-1600.webp

Let the model fill in tool parameters with $fromAI()

For tools connected to the agent, many fields can be filled in by the model. In an expression, use the $fromAI() function:

{{ $fromAI('email') }} 

The arguments are hints for the model, not references to existing values. The full signature takes a key, and optionally a description, a type (string, number, boolean or json) and a defaultValue:

$fromAI("numItemsInStock", "Number of items in stock", "number", 5) 

The key must be 1 to 64 characters and can only contain letters, numbers, underscores and hyphens. $fromAI() only works for tools connected to the AI Agent node, and it doesn't work with the Code tool. Source: n8n, Use AI for parameters.

Step 5: Write the system message and test

Open the agent's options and write a short system message that states its job, what it may and may not do, and when to call each tool. Keep it specific. Then open the chat panel in the editor and try three kinds of message: one that needs the tool, one that doesn't, and one that is missing a detail the tool needs. In chat workflows the model may ask the user for a missing value, such as an email address, instead of guessing.

Check the execution log after each test. An agent runs several times per message: once to decide, once to call a tool, and again to read the result and reply. Seeing which tool it chose and with what input is how you debug it.

Step 6: Add a human approval step for risky tools

For tools that send messages, change records or delete data, n8n lets you require human approval before the agent runs them. The workflow pauses, a reviewer sees which tool the agent wants to use and with what parameters, and they approve or deny. If denied, the action is cancelled and the agent is told. You can send the request through Chat, Slack, Discord, Telegram, Microsoft Teams, Gmail or WhatsApp Business Cloud. The docs suggest starting with review on and loosening it as you gain confidence. Source: n8n, Human-in-the-loop for tools.

Common errors and fixes

These come from n8n's AI Agent common-issues page:

  • "400 Invalid value for 'content': expected a string, got null": the Prompt input contains a null. Check that your expression resolves to a real value, or remove nulls from chatInput.

  • "No prompt specified": the agent expected the prompt from the previous node. Change Prompt from Connected Chat Trigger Node to Define below if you aren't using the Chat Trigger.

  • "A Chat Model sub-node must be connected": add a chat model with the + Chat Model connector.

  • Error in sub-node Simple Memory: usually an older node version in a copied template. Delete the node and add it again.

Running the agent against a local model

If you would rather not send data to a hosted API, n8n has an Ollama Chat Model node. The credential needs the Base URL of your Ollama instance; the default is http://localhost:11434. If n8n and Ollama run in separate Docker containers, put both on the same user-defined bridge network and use http://<ollama-container>:11434, for example http://ollama:11434. Our walkthrough on self-hosting Ollama and Open WebUI on a VPS shows how to run the model side. Check that the model you choose supports tool calling, or the agent won't be able to use its tools.

n8n's newer "Agents" feature vs the AI Agent node

n8n's docs now also describe a separate Agents feature, where agents are built alongside workflows as their own artifacts, with their own instructions, tools and knowledge base, and can be reached through chat, channels and schedules. As of November 2026 that feature is marked Preview, which means it can make mistakes and its behavior may change. The docs state that turning it off doesn't affect the AI Agent workflow node used in this guide, so the steps above stay valid either way. Source: n8n, Build and manage agents.

Before you put an agent in front of customers

An agent that can call tools is software that takes actions on its own judgment, so treat it like production code. Give each tool the least access it needs, require approval for anything irreversible, and log every run. Our launch checklist for vibe-coded apps covers the wider list of things to check before real users arrive.

If you want a working agent wired into your own CRM, inbox or helpdesk, and not a demo, that is the kind of project we build. See our services or contact us with a short description of the workflow you want to automate.

FAQ

Is the n8n AI agent free?

The n8n AI Agent node is part of n8n, and n8n offers both Cloud and self-hosted options. The model you connect is billed separately by its provider unless you run one yourself, and on Cloud each chat message counts as a workflow execution. Check n8n's pricing page for current plan limits.

What is the difference between an n8n agent and a chain?

A chain follows a fixed sequence of AI calls that you define. An agent uses the language model to decide which action or tool to use next. Chains don't support memory in n8n; agents do.

Which chat models work with the n8n AI Agent node?

The Tools Agent documentation lists OpenAI, Groq, Mistral Cloud, Anthropic and Microsoft Foundry chat models. n8n also has an Ollama Chat Model node for models you host yourself, and the model needs to support tool calling for the agent to use its tools.

Why does my n8n agent forget the conversation?

Either no memory node is connected, or the session key changes between messages. Add Simple Memory and keep the Session Key stable for one conversation. Memory doesn't persist between separate sessions.

Can an n8n AI agent call another workflow?

Yes. The Call n8n Workflow Tool lets the agent run any other workflow and read its output. If you choose the sub-workflow from the database, it has to be published for production runs to work.

Can I make an n8n agent ask for approval before it acts?

Yes. Turn on human review for selected tools and route the approval request to Slack, Telegram, Gmail, Teams or other supported channels. The agent only runs the tool after a person approves it.

Sources

  1. n8n docs: AI Agent node
  2. n8n docs: Tools Agent
  3. n8n docs: AI Agent common issues
  4. n8n docs: Chat Trigger
  5. n8n docs: Simple Memory
  6. n8n docs: How memory works
  7. n8n docs: How tools work
  8. n8n docs: Call n8n Workflow Tool
  9. n8n docs: Use AI for parameters ($fromAI)
  10. n8n docs: Human-in-the-loop for tools
  11. n8n docs: Ollama credentials
  12. n8n docs: Build and manage agents
  13. Ollama docs: Tool calling

Next article

All articles →
Cover: vibe-coded app to production launch checklist

vibe-coding · 7 min read

Vibe-Coded App to Production: The Launch Checklist

A practical checklist for taking an app built with Cursor, Lovable, Bolt or Claude Code from demo to real users: secrets, database permissions, auth, payments, backups, monitoring and the domain.

Read next ↗