EN · $ USD
Developer and Application Servers

How to Set Up an AI Agent in n8n

7 min read Updated: Sunucu Atlas technical team

In short

To build an AI agent in n8n, add an AI Agent node after a Chat Trigger, then connect a chat model (OpenAI Chat Model or Anthropic Chat Model), memory so it remembers the conversation (Simple Memory or Postgres Chat Memory), and tools it can call (HTTP Request Tool, other workflows). The system message defines the agent's role and limits, and the model decides on its own which tool to use and when.

In this guide
  1. The building blocks of an AI Agent
  2. Prerequisites
  3. Step 1: Start with a Chat Trigger
  4. Step 2: Connect the AI Agent and chat model
  5. Step 3: Write the system message
  6. Step 4: Add memory
  7. Step 5: Define the tools
  8. Step 6: Test and set limits
  9. Cost and performance tips
  10. Take your agent live

The n8n AI Agent is the node that turns a large language model (LLM) from a text generator into an assistant that can make decisions. In a classic workflow, the order of steps is fixed; an agent looks at the user's question and decides for itself which tool to call, what data to fetch and when to answer. This guide covers connecting a model, memory and tools to the AI Agent node, and ends with a customer support assistant that can look up order status and explain the return policy.

The building blocks of an AI Agent

The AI Agent node does not work on its own; it gets its power from the sub-nodes connected beneath it:

  • Chat Model: The language model that serves as the agent's brain. n8n offers options such as OpenAI Chat Model, Anthropic Chat Model (Claude), Google Gemini Chat Model, and Ollama Chat Model for local models. For the agent to use tools, the model you choose must support tool calling (function calling).
  • Memory: Stores the conversation history. Without memory, the agent answers every message as if it were seeing it for the first time.
  • Tools: Capabilities the agent can call, such as sending a request to an API, running another n8n workflow, querying a database or using a calculator.

The agent works in a loop: it reads the question, calls a tool if needed, evaluates the tool's result, calls another tool if needed, and writes the answer once it has enough information. The Max Iterations setting in the node options caps how many rounds this loop can run.

Prerequisites

  • An up-to-date n8n install. AI nodes are under constant development, so some options may be missing in older versions; update by following the n8n update guide.
  • An API key created in an OpenAI or Anthropic account, with a payment method on file. API usage is billed per token.
  • A data source for the agent to query: in this example, we use the WooCommerce REST API.

Step 1: Start with a Chat Trigger

  1. Create a new workflow and add the When chat message received (Chat Trigger) node.
  2. This node opens a chat window in the n8n editor for testing. Later, with the Make Chat Publicly Available option, you can publish it as a public chat page or as a chat widget embedded in your site (n8n's @n8n/chat package).

If you want to connect the agent to email, Telegram or WhatsApp instead of chat, use the matching trigger in place of the Chat Trigger. For Telegram, the n8n Telegram notification guide is a good starting point.

Step 2: Connect the AI Agent and chat model

  1. Add an AI Agent node after the Chat Trigger. By default it runs with Tools Agent behavior, the recommended setup for scenarios that use tools.
  2. From the Chat Model connector under the node, choose OpenAI Chat Model or Anthropic Chat Model.
  3. Create a new credential and enter your API key, then select the model you want from the list.
  4. Keep the Sampling Temperature low in the model settings (between 0 and 0.3). In customer support, you want consistency more than creativity.

Which model to pick depends on cost and quality. Small, fast models suit simple classification and short answers; larger models are better for multi-step reasoning and long context. Model names change often, so check the current list in your provider's documentation.

Step 3: Write the system message

In the AI Agent node, the Options → System Message field defines the agent's identity, task boundaries and tool-use rules. A good system message is the setting that shapes the agent's behavior the most. The example below is written for a Turkish-language store; write yours in your customers' language:

Sen "Örnek Mağaza" için çalışan bir müşteri destek asistanısın.
Yalnızca Türkçe ve kibar, kısa cümlelerle yanıt ver.

Kurallar:
- Sipariş durumu sorulursa önce sipariş numarasını iste,
  ardından siparis_sorgula aracını kullan.
- İade ve değişim sorularında yalnızca iade_politikasi aracından
  gelen bilgiyi kullan, kendi bilgini ekleme.
- Araçlardan bilgi alamazsan tahmin yürütme; müşteriyi
  destek@ornekmagaza.com adresine yönlendir.
- Kart bilgisi, parola veya T.C. kimlik numarası isteme.
- Bugünün tarihi: {{ $now.toFormat('dd.MM.yyyy') }}

(In English, the rules say: ask for the order number before using the order lookup tool, answer return questions only from the return policy tool, never guess and refer the customer to the support address instead, and never ask for card details, passwords or national ID numbers.) A "don't make things up if you don't know" rule significantly reduces the tendency of language models to confidently produce information that isn't true (hallucination).

Step 4: Add memory

Memory lets the agent remember earlier messages in the same session. That is how the agent keeps context when a customer who asked "Where is my order?" replies with just the number in the next message.

Memory typeWhere it stores dataWhen to use it
Simple MemoryIn the n8n process memoryTesting and simple use; cleared when n8n restarts
Postgres Chat MemoryIn a PostgreSQL tableProduction; persistent and survives restarts
Redis Chat MemoryIn RedisVery high traffic and time-limited sessions

Postgres Chat Memory is recommended in production. In the memory node, set the Session ID to the {{ $json.sessionId }} value generated by the Chat Trigger, so each customer's conversation is kept separate. Use Context Window Length to set how many previous messages are sent to the model; a value of 5-10 preserves context while limiting token cost.

Step 5: Define the tools

Order lookup tool

Add an HTTP Request Tool to the agent's Tool connector:

  • Name: siparis_sorgula
  • Description: "Fetches the order status, shipping details and items using the order number the customer provides."
  • URL: https://ornekmagaza.com/wp-json/wc/v3/orders/{{ $fromAI('siparisNo', 'Müşterinin sipariş numarası', 'number') }}
  • Authentication: A credential created with your WooCommerce REST API keys (read-only permission is enough).

The $fromAI() function lets the model extract the parameter value from the conversation. The tool's description is critical: the model decides which tool to call and when by reading this text. Vague descriptions lead to the wrong tool being picked.

Security note: the agent may show order details to anyone who knows the order number. In real use, we recommend an intermediate workflow that also asks for the email address or the last four digits of the phone number along with the order number, and checks them against the data the tool returns.

Return policy tool

For fixed information, you can create a separate workflow and connect it to the agent with the Call n8n Workflow Tool. For a larger knowledge base, a more scalable approach is to load your documents into a vector database (for example PGVector or Qdrant) and expose it to the agent with the Vector Store Tool, in other words, to set up RAG (Retrieval-Augmented Generation).

Step 6: Test and set limits

  1. Use the Chat button in the editor to ask questions like "Where is my order number 12345?" In the right panel, you can see step by step which tool the agent called and with which parameters.
  2. Push the agent: off-topic questions, a missing order number, an order that does not exist. In each case, check whether it follows the rules in the system message.
  3. Keep Max Iterations reasonable (5-10). This stops the agent from getting stuck in a loop and wasting tokens.
  4. Set a monthly spending limit in your OpenAI or Anthropic dashboard.

Never write API keys in plain text in the system message or a Code node; always store them as n8n credentials. When your data must not leave your infrastructure, running the model on your own server is also an option; for requirements, see the local LLM with Ollama guide.

Cost and performance tips

  • Keep context small: Tool responses are sent straight to the model. A WooCommerce order response contains dozens of fields; returning only the status, tracking number and product names through an intermediate workflow noticeably cuts token usage.
  • Split the task: Instead of sending every message to a large model, classify intent with a small model first (order, return, general question) and route only complex requests to the agent.
  • Hand off to a human: If the agent cannot solve the issue in two attempts, add a tool that passes the conversation to your support team via Telegram or email. For customer satisfaction, this is worth more than having AI answer every question.
  • Review the logs: Check the agent's actual answers in the Executions tab every week, and fix recurring mistakes by adding new rules to the system message.

Take your agent live

For a support assistant to serve customers, n8n has to run without interruption, the Chat Trigger must be reachable over HTTPS, and memory must be kept in a persistent database. Sunucu Atlas preinstalled n8n servers meet all three requirements out of the box: n8n comes ready on Docker, the PostgreSQL database is ready to use for Postgres Chat Memory, and an automatic SSL certificate is configured.

AI Agent flows use more memory than classic automations because of long chat histories, large API responses and concurrent sessions. That is why we recommend the SA-N8N-2 n8n server with 4 CPU, 8 GB RAM and 80 GB NVMe disk for agent projects. If you plan to run several agents and a vector database on the same server, SA-N8N-3 with 12 GB RAM leaves more headroom.

Frequently asked questions

Which model should I use for an n8n AI Agent?

Any chat model that supports tool calling will work; OpenAI, Anthropic Claude and Google Gemini models are available as built-in nodes in n8n. Small models lower costs for simple tasks, while larger models are more consistent at multi-step reasoning.

Why doesn't my AI Agent remember previous messages?

Either no Memory sub-node is connected to the agent, or the Session ID changes with every message. Add Postgres Chat Memory and use the Chat Trigger's sessionId value as the Session ID.

What is the difference between Simple Memory and Postgres Chat Memory?

Simple Memory keeps the conversation in the n8n process memory and is cleared when n8n restarts. Postgres Chat Memory writes the history to a database, which makes it the persistent, reliable choice for production.

Is the n8n AI Agent free?

On a self-hosted n8n install, there is no extra license fee for the AI Agent node. However, API usage from providers such as OpenAI or Anthropic is billed per token.

The agent keeps calling the wrong tool. What should I do?

Rewrite each tool's Description field so it clearly explains when to use it, and add tool-use rules to the system message. Lowering the model temperature also improves consistency.

Does an AI agent send customer data outside my server?

If you use a cloud-based model, the messages and tool results given to the agent are sent to the model provider. If you don't want that, give the agent only the fields it needs or run the model on your own server.

For this guide

Recommended plans

AI agents need more memory than classic flows because of long chat histories and large API responses. SA-N8N-2, with 8 GB RAM, ready-to-use PostgreSQL and automatic SSL, is a solid foundation for running your support assistant in production with persistent memory.

SA-N8N-1

n8n
Recommended

4 GB RAM

$2.75/mo

+VAT

  • Processor2 CPU
  • Disk40 GB NVMe
  • LocationIstanbul
  • 1G Port
  • n8n Pre-installed (Docker)
  • PostgreSQL Database
  • Automatic SSL Certificate

SA-N8N-2

n8n

8 GB RAM

$4.32/mo

+VAT

  • Processor4 CPU
  • Disk80 GB NVMe
  • LocationIstanbul
  • 1G Port
  • n8n Pre-installed (Docker)
  • PostgreSQL Database
  • Automatic SSL Certificate

SA-N8N-3

n8n

12 GB RAM

$6.29/mo

+VAT

  • Processor6 CPU
  • Disk120 GB NVMe
  • LocationIstanbul
  • 1G Port
  • n8n Pre-installed (Docker)
  • PostgreSQL Database
  • Automatic SSL Certificate

n8n Server — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.