n8n Automation

How to Build an n8n AI Agent Workflow with Tools, Memory and Guardrails

By Arezoo Mohammadzadegan November 6, 2026 12 min read
How to Build an n8n AI Agent Workflow with Tools, Memory and Guardrails

An n8n AI agent workflow is a workflow where an AI Agent node, connected to a chat model, one or more tools and optionally memory, decides which tools to call to finish a task. To build one that behaves, you pick the trigger, write a system message, describe each tool, add memory, and put approval and guardrails around risky actions.

This guide follows that order, one setting at a time, and ends with how to test the agent before anyone else talks to it. If triggers, nodes and executions are new to you, the guide on what n8n is used for and how a workflow runs explains them first. Everything below about n8n’s behaviour comes from the n8n documentation, read on 6 November 2026, with a link to each page.

What an n8n AI agent workflow is made of

The AI Agent node sits at the centre, and it does not work alone. The n8n page for the AI Agent node puts it this way, “Connect a chat model and one or more tools, and the agent decides which tools to call to complete a task.” The same page says you must connect at least one tool sub-node. Sub-nodes are the pieces attached underneath the agent rather than placed in the main line of the workflow.

  • A trigger starts the run. It can be the Chat Trigger for a chat window, or a webhook, a form, an email or a schedule.
  • A chat model is the language model that does the reasoning. Without one, the node stops with the error “A Chat Model sub-node must be connected.”
  • Tools are the only actions the agent can take.
  • Memory is optional and lets the agent refer back to earlier messages in the same conversation.
  • An output parser is optional too, for when the next node needs fixed fields instead of free text.

Since n8n 1.82.0 the node no longer asks you to pick an agent type. According to the same page, every AI Agent node now works as a Tools Agent, and the older version with the agent type setting will be removed in n8n 3.0. The nodes before and after the agent stay ordinary fixed steps, so the agent only decides inside the part of the job you hand to it.

Diagram of an agent block with a trigger before it and model, tools, memory and parser blocks attached below
The AI Agent node with its chat model, tools, memory and output parser attached as sub-nodes, between fixed steps.

Decide what the agent may do before you open the editor

The most useful decision happens before any node is placed. Anthropic’s engineering guide Building effective agents, published in December 2024, recommends “finding the simplest solution possible, and only increasing complexity when needed.” In n8n terms, if every request follows the same path, fixed nodes with a single AI step may be enough, and an agent earns its place when the order of steps depends on what the user asks. How to make that call for each task is the subject of a separate guide in this series.

Then write two short lists. The first holds what the agent may read, such as an order status, a product sheet or a calendar. The second holds what it may change, such as creating a record, sending a message or issuing a refund. Read tools can usually run freely. Every item on the second list is a candidate for human review, covered further down. The guide on how AI agents work in a business explains why tasks that move money or cannot be undone should stay with a person at first.

Start with the trigger and the prompt source

The trigger decides where the agent’s input comes from, and the Prompt setting on the agent has to match it. The Tools Agent documentation describes two choices. “Take from previous node automatically” expects a field called chatInput from the node before it, which is what the Chat Trigger sends. “Define below” lets you write the prompt yourself, usually as an expression that pulls in fields from earlier nodes, such as the body of an email or the answers from a form.

A mismatch here is a common reason a first agent fails. The common issues page lists the error “No prompt specified” for an agent left on the automatic setting without a chat trigger in front of it, and the fix is to switch to Define below. A related error, “400 Invalid value for ‘content'”, means the prompt came out empty, usually because an expression points at a field that does not exist.

If people will chat with the agent directly, the Chat Trigger gives you a hosted chat page or an embedded widget, with authentication set to none, one shared username and password, or n8n user login. Keep Make Chat Publicly Available switched off while you build. The same page notes that every message runs the workflow, so a conversation of 10 messages uses 10 executions, which matters if your plan counts executions.

Connect a chat model

The chat model is a sub-node for the provider you use, with its own credential. n8n leaves the choice of model to you, and that choice affects cost, speed and how reliably the agent calls tools. Two points apply whichever provider you pick. A single run usually involves the agent more than once, because, as the n8n page on what agents do explains, an agent typically runs once to set up, again for a tool call and again to evaluate the tool’s response and reply. And the model provider bills its usage separately from n8n, so set a spending limit on the API key, if your provider offers one, before the agent meets real traffic.

Write the system message

The System Message, found under the agent’s options, is sent to the agent before the conversation starts to guide its decisions, as the Tools Agent page describes it. A good one is short and concrete. For most business agents it covers five things.

  • The job in one or two sentences, and who the agent is talking to.
  • Which tool to use for which kind of request, in plain words.
  • What it must never do, for example quote a price that did not come from a tool, or promise a delivery date.
  • What to say when it cannot help, and how it hands over to a person.
  • The shape of the answer, such as language and length, or the fixed structure the next node expects.

If some tools need human review, the n8n page on human-in-the-loop for AI tool calls advises describing that in the system prompt as well, including which tools need approval and how the agent should respond when a request is denied.

Keep facts that change out of the system message. Prices, stock and opening hours belong in a tool the agent reads, so that one update reaches every conversation.

Give each tool a clear name and description

A tool is the only way the agent can look something up or act. According to the docs page on how tools work, n8n offers built-in service nodes that attach as tools, the HTTP Request Tool for calling an API, a Custom Code Tool, MCP servers and the Call n8n Workflow Tool, which runs another workflow and returns its output.

The agent picks a tool from what its description says, so the description is part of the logic. The Call n8n Workflow Tool page says the description should tell the agent when to use the tool and what input to send, with the example “Call this tool to get a random color.” Anthropic’s guide says a good tool definition “often includes example usage, edge cases, input format requirements,” and advises changing a tool’s arguments so that mistakes are harder to make. In practice that means one tool per job, a name that says what it does, such as get_order_status rather than database, and a description that also says when not to use it.

The inputs the agent fills in come from the $fromAI() function, or from the star button at the end of a field, which hands that field to the model. The $fromAI() guide says the function takes a key of 1 to 64 letters, digits, underscores or hyphens, plus an optional description, type (string, number, boolean or json) and default value, and that it only works in tools connected to the AI Agent node. Fill every field you can with a fixed value or an expression, and leave to the model only what it really has to decide, such as the order number a customer typed.

Two details from the docs save debugging time. A workflow called through the Call n8n Workflow Tool must be published for production runs. Otherwise the agent receives the error “Workflow is not active and cannot be executed” as the tool’s answer, which the docs warn is easy to miss inside a longer reply. And in sub-nodes an expression always resolves to the first input item, so a tool does not loop over several items the way a normal node does.

💬

Need to generate WhatsApp QR Codes for your sales team?

Create instant click-to-chat QR codes with our free Business Generator.

Create WhatsApp QR
100% Free Tool by ArtinWebs

Add memory so the agent can follow a conversation

Without memory, each message starts from nothing, and a follow-up such as “and the second one?” means nothing to the agent. The n8n page on how memory works says AI agent nodes can use memory but AI chains cannot, and lists the options. Simple Memory keeps a set amount of recent chat history inside n8n. Memory service nodes such as Postgres Chat Memory, Redis Chat Memory, Motorhead, Xata and Zep keep it in an outside service. Chat Memory Manager covers cases a memory node cannot handle directly.

The Simple Memory documentation lists two settings. Session Key decides which conversation a message belongs to, so it has to be unique per person or per conversation. Context Window Length sets how many previous interactions are considered with each new message. A longer window gives the agent more context and makes each request to the model larger.

The same page carries the warning “Don’t use this node if running n8n in queue mode,” because n8n cannot guarantee that every call reaches the same worker. If you run queue mode, choose a memory node that stores history in a database. With the Chat Trigger you can also load earlier messages from memory, and the Chat Trigger page recommends connecting the trigger and the agent to the same memory sub-node so both read one source. On WhatsApp, the n8n WhatsApp bot tutorial using the official Cloud API shows how to key memory to the sender’s phone number.

Agents that run on a schedule or handle one email at a time often need no memory at all. If each run is a separate job, memory with a fixed session key would mix unrelated jobs into one history, so leave it off.

Put a person in front of risky tools

n8n can pause an agent before a chosen tool runs and ask a person to approve it. The human-in-the-loop guide sets this up from the Tools connector of the AI Agent node, in a Human review section where you pick an approval channel and then attach the tools that need approval to it. The channels it lists are n8n Chat, Slack, Discord, Telegram, Microsoft Teams, Gmail, WhatsApp Business Cloud, Google Chat and Microsoft Outlook, and the approval channel can be different from the one customers use.

When the agent wants a reviewed tool, the workflow waits. The reviewer sees the tool and the parameters the agent chose. Approving runs the tool with those inputs. Denying cancels the action and tells the agent it was rejected. In the review message, include the tool name and its parameters (the n8n page shows how to reference them with expressions) so the reviewer sees exactly what will happen.

Which tools to review is the second list you wrote at the start. Sending messages to customers, changing prices, issuing refunds and deleting records are the usual candidates, for the reasons set out in what AI agents cannot do, including that an agent can be steered by instructions hidden in the content it reads. The n8n page recommends starting with review switched on and reducing it as confidence grows. The follow-up example in n8n workflow examples for sales teams applies the same idea, with the send-email tool behind review and lookups left free.

Add guardrails on input, output and loops

Approval covers actions. Three more settings cover what goes in, what comes out and how long the agent keeps trying.

The Guardrails node checks text before it reaches the model, or checks the model’s answer before the workflow uses it. Its Check Text for Violations operation sends failing items to a Fail branch and can check for keywords, jailbreak attempts, NSFW content, personal data, secret keys, off-topic requests, URLs, and custom rules or regular expressions. The jailbreak, NSFW, off-topic and custom checks need a chat model of their own. The Sanitize Text operation replaces URLs, secret keys, personal data and regex matches with placeholders instead of blocking the item, which suits logs and alerts.

Max Iterations, in the agent’s options, caps how many times the model runs while trying to produce an answer, and the Tools Agent page gives the default as 10. Anthropic’s guide calls a maximum number of iterations a common stopping condition “to maintain control.” For an agent with two or three tools, a lower value ends a confused run sooner instead of letting it call tools over and over.

Require Specific Output Format, also on the agent, asks you to connect an output parser, such as the Structured Output Parser, when the next node needs fixed fields. If a later node reads an order number and a quantity, a parser is safer than hoping the model writes them the same way every time.

Test the agent before anyone else talks to it

Test in the editor’s chat panel and check what the agent did as well as what it said. Return Intermediate Steps, in the agent’s options, adds the steps the agent took to its output, according to the Tools Agent page, so you can see which tools it called. A short set of test messages catches most problems.

  1. A plain request that needs one tool call. Check that the agent chose the right tool and filled the inputs correctly.
  2. A follow-up that only makes sense with memory.
  3. A request outside its job, which it should decline or hand over instead of improvising.
  4. A request that uses a reviewed tool, followed by a denial. The agent should accept the denial and say so.
  5. A message that tells it to ignore its instructions, to see whether the Guardrails node and your tool limits hold.
  6. A tool failure, such as a wrong ID, to check that the agent reports the problem instead of inventing an answer.

Anthropic’s guide recommends “extensive testing in sandboxed environments, along with the appropriate guardrails,” which in n8n means test credentials and test data until all six pass. After you publish, link the workflow to an error workflow, as described in the guide to n8n error handling, so a failed run reaches a person. A tool error that comes back to the agent as its answer does not fail the run, though, so read a sample of real conversations in the first weeks as well.

What this setup does not solve

None of these settings makes the agent correct. Tool descriptions and a clear system message make good choices more likely, not certain. Review stops a bad action only on the tools you put behind it. Guardrails catch the patterns they check for, at thresholds you set, and the model-based checks are themselves a model’s judgement. Memory only holds what fits in the window you chose. An agent built this way is easier to trust because its permissions are narrow and its risky steps wait for a person, which is a different thing from an agent that never gets it wrong.

Where to go next

The next guide in this branch takes the question from the start of this article further, namely which steps of a workflow should go to an AI Agent node and which should stay fixed, with a way to decide for each task.

If your agent will talk to customers on WhatsApp, the n8n WhatsApp bot tutorial covers the credentials, the memory key per customer and Meta’s messaging rules. If you are still choosing a platform, n8n vs Zapier vs Make explains how each one counts usage, which matters once every chat message is an execution.

If you are planning an agent in n8n and want a second opinion on its tools, approvals and guardrails before it goes live, book a free 30-minute strategy call and bring the two lists from the start of this guide.

Arezoo Mohammadzadegan
About the Author

Arezoo Mohammadzadegan

AI Programmer & Digital Marketing Strategist at ArtinWebs (A M H R MARKETING MANAGEMENT L.L.C). Specialist in Artificial Intelligence development, AI agent programming, n8n automation workflows, and digital transformation. Based in Dubai, UAE.