June 2, 2026 · Cuneihive

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

A concrete, step-by-step tutorial for building an n8n AI agent — the AI Agent node, chat model, tools, memory, and a worked lead-qualification example.

Building an n8n AI agent is the fastest way to turn a large language model from a chat toy into infrastructure that actually does work — reads a ticket, checks a database, drafts a reply, books a call. This guide walks through building an n8n AI agent step by step: the AI Agent node, the chat model behind it, tools, memory, structured output, and a worked example you can adapt to a support or lead-qualification workflow. No fluff, just the real node names and the decisions that matter.

What an n8n AI agent actually is

At the center is the AI Agent node (@n8n/n8n-nodes-langchain.agent). It's a multi-turn LLM driver: it takes an incoming message, decides whether it needs to call any of the tools you've given it, loops until it has an answer, and returns a result. Everything else plugs into it as sub-nodes — small satellite nodes wired into dedicated slots on the Agent.

There are four sub-node slots:

  • Model (required) — the language model, e.g. OpenAI Chat Model, Anthropic Chat Model, or OpenRouter Chat Model. Always use the chat variant, not a completion variant.
  • Memory (optional) — conversation history so the agent remembers earlier turns.
  • Tools (optional, but the whole point) — the actions the agent can take.
  • Output Parser (optional) — forces the result into structured JSON.

If all you need is a single, one-shot text response with no tools and no memory (classify this, summarize that), reach for the lighter Basic LLM Chain (@n8n/n8n-nodes-langchain.chainLlm) instead. The Agent earns its keep the moment you need tool calls or multi-turn reasoning.

Step 1: Add a trigger

Every workflow starts with a trigger, and the trigger shapes your input. While building, use the Chat Trigger (@n8n/n8n-nodes-langchain.chatTrigger) — it gives you a chat box right on the canvas to poke at the agent, and it auto-populates a sessionId that memory keys on.

For production, swap in the real surface: a Webhook for a website form or app, or a native Slack/Telegram/Discord trigger for a chatbot. One hard rule for any chat surface that posts replies: filter out the bot's own user ID in the first node after the trigger, or the bot's messages re-trigger the workflow in an infinite loop.

Step 2: Wire up the chat model

Drag a chat-model node into the Agent's Model slot and attach its credential (your provider API key). This is where you pick capability vs. cost — a stronger model reasons better about which tool to call and handles messy input, while a cheaper model is fine for narrow, well-scoped agents. Start on a mid-tier model and adjust once you've watched a few real runs.

Step 3: Write the system prompt

In the Agent node, set promptType to define and point the text at your incoming message, e.g. {{ $json.chatInput }}. Then open Options → System Message. The system prompt is the agent's persona and rulebook. Keep it focused on:

  • Role and voice — "You are a support agent for [product]. Be concise and factual."
  • Output format — "Respond in markdown. Never invent order numbers."
  • Global behavior — refusal rules, the current date, how to escalate.

Resist the urge to cram tool instructions in here. How to call a specific tool belongs in that tool's description, not the system prompt — which brings us to the most important step.

Step 4: Add tools to your n8n AI agent

Tools are what separate an n8n AI agent from a chatbot. The model reads each tool's name and description to decide what to call — so treat them like API design, not afterthoughts. A tool named doStuff with the description "handles data" will be skipped or mis-called silently.

You have four tool types; pick the lightest that covers the job:

  1. Native tool nodesGmail Tool, Slack Tool, Calculator Tool, and dozens more. Lowest config overhead.
  2. HTTP Request Tool — for any external API, or when a native node is missing an operation. It can reuse an existing OAuth/API-key credential.
  3. Sub-workflow as tool (toolWorkflow) — the most powerful option. Any n8n workflow becomes a tool with typed inputs via the fromAi() expression. Reach for this whenever the logic is multi-step (look up a record, transform it, write it back).
  4. MCP tool — for capabilities exposed by another published workflow or MCP server.

A critical safety pattern: any tool that sends, pays, or changes something a person will see should be wrapped in a human-in-the-loop review node (slackHitlTool, telegramHitlTool, etc.). It pauses execution for a human to approve before the action fires — and the approval message should show the actual parameters the tool will receive, not a paraphrase.

One more control worth knowing: the Agent loops until it's done, and a badly-scoped tool can send it spinning. Set a sane max iterations in the Agent's options so a confused run stops instead of burning tokens in a call loop.

Step 5: Give it memory

Without a memory sub-node, every call is stateless — the agent forgets the previous turn. For most chat agents, add Simple Memory (memoryBufferWindow), which keeps the last N messages per session and persists across executions.

The one thing that must be right is the session key. Chat Trigger fills sessionId automatically, but on a webhook you pass your own conversation ID through and bind it to the memory node's sessionKey. Hardcode it or leave it blank and conversations cross between users. Plumb a stable key from trigger to memory every time.

Step 6 (optional): Force structured output

If a downstream node needs clean data — not prose — add an Output Parser sub-node (outputParserStructured). Define a real JSON schema (schemaType: 'manual') with your fields, enums, and which are required. Then set autoFix: true and wire a coding-capable model as the fixer: if the model returns malformed JSON, the parser retries against the schema instead of failing the whole run.

Worked example: a lead-qualification agent

Here's the shape of a practical n8n AI agent for inbound leads:

  • Trigger: Webhook receiving a form submission (name, email, message).
  • Model: a chat model in the Model slot.
  • System prompt: "You qualify inbound leads. Extract intent and urgency. If the lead asks to speak with someone, use the booking tool. Never fabricate availability."
  • Tools:
    • HTTP Request Tool → look up the company domain for firmographic context.
    • toolWorkflow → "Check calendar availability," a sub-workflow returning open slots.
    • Gmail Tool wrapped in a human review node → draft a reply, but pause for the operator to approve before it sends.
  • Memory: Simple Memory keyed on the lead's email, so a follow-up message continues the thread.
  • Output Parser: structured JSON — { intent, urgency, qualified, next_step } — written to your database.

That single workflow reads the lead, enriches it, decides the next action, and drafts the outreach — with a human gate on anything that leaves the building. This is exactly the pattern behind Cuneihive's autonomous revenue infrastructure: agents that qualify, route, and respond without waiting on a person to notice. Pair it with an outbound voice campaign engine and the same qualified lead can be dialed in seconds, not hours.

Common mistakes to avoid

  • Vague tool names/descriptions — the number-one cause of an agent "not working." Be specific: Search customer database, not query.
  • No session key — crossed conversations and broken memory.
  • Output parser without autoFix — one bad JSON response kills the run.
  • Passing files directly to a tool — binary can't cross the tool boundary; stage uploads to storage and pass the key as a string.
  • Wrapping image/audio generation in an Agent — use the provider's native single-call node instead; binary doesn't flow through the Agent's output.

From tutorial to production

Building one agent on the canvas is the easy part. Making it reliable — error handling, human review on the right actions, memory that scales, and a data layer it can trust — is where most projects stall. If you'd rather have production-grade agents engineered end to end instead of maintaining a fragile canvas yourself, that's what we do. Explore our full range of autonomous systems and services, or Schedule a Build and we'll scope the exact agent your workflow needs.

n8nai agentsautomationtutorialllm
How to Build an n8n AI Agent | Cuneihive