How to Build an AI Agent in n8n (Step-by-Step)
This page may contain affiliate links. If you buy through them we may earn a commission at no extra cost to you.
Direct answer: building a working AI agent in n8n means four pieces: a trigger that starts the workflow, an AI Agent node with a connected LLM credential and a clear system prompt, optional tool nodes the agent can call to actually do something (not just answer), and a memory node if it needs to hold context across turns. A simple question-answering agent is runnable in about fifteen minutes; one with real tools and memory takes closer to an hour, most of which goes into writing a system prompt that actually constrains the agent's behavior.
Step 1: Pick a trigger
Every n8n workflow starts with a trigger, and an agent is no different. A Chat Trigger is the fastest way to test an agent inside n8n's own interface before wiring it to anything real. For production use, common choices are a Webhook (for a custom frontend or API call), a Telegram Trigger if the agent should live inside a chat app, or a Form Trigger for a simple internal tool. If you're building a Telegram-based agent specifically, our guide to building a Telegram bot with n8n covers the bot-setup side that pairs with this step.
Step 2: Add the AI Agent node and connect an LLM
Drop in an AI Agent node and connect it to a Chat Model node, OpenAI, Anthropic, or another supported provider, using an API credential you've already created (our guide to connecting OpenAI to n8n covers that credential setup in detail if you haven't done it yet). This chat-model connection is what actually generates the agent's responses; the Agent node itself is the orchestration layer around it.
Write the system prompt now, not later. The single biggest difference between an agent that behaves reliably and one that doesn't, in our testing, was prompt specificity. "You are a helpful assistant" produces vague, inconsistent behavior. "You are a support triage agent. Classify each incoming message as billing, technical, or general, and respond only with the category and a one-sentence reason" produces something you can actually trust in production.
Step 3: Give it tools so it can act, not just answer
A chat-only agent is a chatbot with extra steps. What makes it an agent is the ability to take action, and in n8n that means connecting tool sub-nodes to the AI Agent node's tool input. Common options: an HTTP Request Tool to call an external API (checking an order status, looking up a CRM record), a Vector Store Tool for retrieval over your own documents, or even another full n8n Workflow Tool, letting one agent call a separate workflow as a callable action.
| Tool type | What it lets the agent do | Example use |
|---|---|---|
| HTTP Request Tool | Call an external API | Look up an order's status in your store's API |
| Vector Store Tool | Retrieve from your own documents | Search a product FAQ |
| Workflow Tool | Call another n8n workflow as an action | Trigger a separate approval or fulfillment flow |
We built a support agent that used an HTTP Request tool to look up order status in our e-commerce platform's API and a Vector Store tool to search a product FAQ. The agent decided on its own, based on the system prompt, which tool to call for a given question, which is the part that genuinely feels different from a scripted if/then workflow. Giving an agent more than three or four tools, in our tests, measurably increased the rate at which it picked the wrong one; keep the toolset narrow and specific to the agent's actual job.
Step 4: Add memory if the agent needs to hold context
Without a memory node, every message to the agent is a blank slate; it has no idea what was said two messages ago. For a quick test or a stateless single-turn agent, that's fine and you can skip this step. For anything conversational, attach a Simple Memory node for a lightweight session buffer, or a database-backed memory node (Postgres Chat Memory, Redis Chat Memory) if you need conversations to persist across sessions or server restarts.
Simple Memory is enough for most internal tools and low-volume bots. Move to a database-backed option once you have enough concurrent users that you need memory isolated per user or per chat session rather than shared across everyone hitting the workflow.
Step 5: Test adversarially, not just happily
Run the obvious happy-path question first to confirm the pipeline works, then deliberately try to break it: ask something outside its intended scope, give it an ambiguous input, or ask it to use a tool incorrectly. In our testing, this is where most agents reveal a system prompt that's too permissive, agents that will confidently answer questions they have no business answering, or attempt a tool call with malformed input that the tool node then fails on.
Tighten the system prompt based on what you find, explicitly telling the agent what's out of scope is usually more effective than hoping the model infers it. If the agent will handle anything customer-facing or involving real actions (refunds, account changes), add a human-approval step, a Slack or email notification the agent sends before taking an irreversible action, rather than trusting it unsupervised from day one.
When building in n8n isn't the right call
If you don't have a developer's worth of time to spend assembling nodes, writing prompts, and debugging tool calls, and you just need a working agent this week, a no-code platform will get there faster. Our roundup of the best no-code AI agent builders and our Lindy review cover the alternative if speed matters more than full control over logic. The trade-off runs the other way too: n8n gives you precise control over tools, triggers, and memory that a role-based platform abstracts away, which matters once your use case outgrows what a pre-built role can do. For a lighter-weight, code-first alternative to n8n's visual canvas if you're building agents alongside other automation, our Flowise vs n8n comparison is worth a look, and our n8n vs OpenAI AgentKit comparison covers how n8n's approach stacks up against OpenAI's own agent-building tools. Once the agent itself works, the next step for most teams is grounding it in their own documents; our guide to building a RAG chatbot with n8n covers exactly that, using the same Agent node this guide introduces.
Frequently asked questions
Do I need to self-host n8n to build an AI agent?
No. n8n Cloud supports the AI Agent node identically to a self-hosted instance. Self-hosting matters if you want full control over data or you're already running n8n for other automations, but it isn't required to build or run an agent.
Which LLM should I connect to the AI Agent node?
n8n supports OpenAI, Anthropic, and several other providers through dedicated credential nodes, plus any OpenAI-compatible endpoint for self-hosted models. In our testing, the right choice depends more on your task's complexity and your budget per call than on any single 'best' model; start with whichever provider you already have an API key for and switch only if you hit a specific capability or cost wall.
How is this different from just using a no-code AI agent builder like Lindy?
Building in n8n gives you full control over logic, which tools the agent can call, and how it's triggered (webhook, schedule, Telegram message, form submission), at the cost of assembling it yourself. A platform like Lindy trades that control for speed: a role-based agent running in minutes with no node graph to design. See our roundup of the best no-code AI agent builders if speed matters more than control for your use case.
Why does my n8n AI agent keep forgetting earlier parts of the conversation?
By default, the AI Agent node has no memory; every execution is a fresh, stateless call to the LLM. Attach a memory node, Simple Memory for a quick fix, or a database-backed option like Postgres Chat Memory for something that persists across sessions, and connect it to the agent's memory input.