December 3, 2026 • 9 min read • Tutorial
AI Agents n8n Tutorial OpenAI

How to Build Your First AI Agent in n8n (Step by Step)

A practical, beginner-friendly walk-through: from an empty canvas to an AI agent that remembers the conversation and uses real tools.

How to Build Your First AI Agent in n8n (Step by Step)

Everyone is talking about AI agents, but most explanations stay abstract. In practice, an AI agent is simply a language model that can decide which actions to take: look something up, call an API, write to a spreadsheet, then answer. n8n lets you build one visually, without writing a backend.

This tutorial is for small-business owners, operations people and freelancers who know the basics of n8n (or are willing to learn them) and want a first agent that does something useful. No machine-learning knowledge is needed.

By the end, you will have an agent that chats with you, remembers the conversation, uses a tool to look up real data, and follows clear rules about what it may and may not do.

What an AI agent is (and when you need one)

A normal n8n workflow follows a fixed path: trigger, step 1, step 2, done. An AI agent is different. You give it a goal, a set of tools and some rules, and the model decides at runtime which tools to use, in what order, and when it has enough information to answer.

That flexibility is useful when the input is unpredictable. Good first use cases:

  • An internal assistant that answers questions like "Which orders are still unpaid?" by reading a Google Sheet.
  • A customer-facing helper for a shop or clinic that answers questions about opening hours, services and availability.
  • A research helper for an agency that looks up a company and summarises what it does.

When the steps are always the same (for example "every new form submission goes to the CRM"), you do not need an agent. A classic workflow is cheaper, faster and more predictable. Use an agent when the decision of what to do next depends on what the user says.

What you need before you start

  • A running n8n instance. Self-hosted, n8n Cloud or managed hosting all work. If you do not have one yet, our n8n deployment guide walks through the options.
  • An API key for a language model provider, such as OpenAI, Anthropic or Google. This tutorial uses the OpenAI Chat Model node, but the steps are the same for other providers.
  • Optional: a Google account if you want the agent to read a Google Sheet in the tool step.

The AI nodes in n8n are documented in the official advanced AI docs. Node names change slightly between versions, so if something looks a little different in your editor, check there.

The workflow, node by node

The finished workflow is small: a trigger, the AI Agent node, and three sub-nodes plugged into the agent (a chat model, memory and a tool). Here is how to build it.

1. Chat Trigger ("When chat message received")

Create a new workflow and add the Chat Trigger node. It gives you a chat window inside the editor so you can talk to your agent while you build it. Later, you can make the chat public (n8n gives you a hosted chat page or an embeddable widget) or replace the trigger with WhatsApp, Telegram, Slack or a Webhook.

Leave the defaults for now. Each message you type becomes an item with a chatInput field (the text) and a sessionId (which conversation it belongs to).

2. AI Agent

Add an AI Agent node after the trigger. This is the "brain" that coordinates everything. Key settings:

  • Source for prompt (user message): keep "Connected Chat Trigger Node" so the agent reads the message from the chat. If you use another trigger, choose "Define below" and map the text with an expression such as {{ $json.message }}.
  • System message (under Options): this is where you define who the agent is and what rules it follows. It is the single most important setting. We cover it in detail below.

The AI Agent node has connectors at the bottom for Chat Model, Memory and Tool. A chat model is required; the others are optional but make the agent far more useful.

3. OpenAI Chat Model (the language model)

Click the Chat Model connector and add OpenAI Chat Model (or Anthropic, Google Gemini, Mistral, etc.). Create a credential with your API key, then pick a model from the list.

For a first agent, a smaller, cheaper model is usually enough for simple lookups. Move to a larger model if you see it choosing the wrong tool or misunderstanding instructions. Keep the temperature low (for example 0.2 to 0.4) if you want consistent, factual answers.

4. Simple Memory

Without memory, every message is a fresh start: ask "What about Saturday?" and the agent has no idea what you were talking about. Click the Memory connector and add Simple Memory (called Window Buffer Memory in older versions).

  • Session key: with the Chat Trigger, the default uses the chat's session ID automatically. With other triggers, map something unique per conversation, such as a phone number or Telegram chat ID.
  • Context window length: how many recent messages to remember. Five to ten is a good start. More memory means more tokens sent to the model on every turn, which costs more.

Simple Memory lives inside the n8n instance and is fine for testing. For production, especially if you run n8n in queue mode with several workers, use a persistent store such as Postgres Chat Memory or Redis Chat Memory.

5. A first tool: Calculator

Click the Tool connector and add the Calculator tool. It needs no configuration. Language models are notoriously unreliable at arithmetic, so a calculator is a simple way to see tool use in action. Ask "What is 17.5% of 2,340?" and open the execution log: you will see the agent decide to call the calculator, receive the result, then answer.

6. A useful tool: Google Sheets

Now give the agent access to real data. Add a Google Sheets tool on the Tool connector, connect your Google credential, choose the operation Get Row(s), and select a spreadsheet, for example a price list with columns Service, Price and Duration.

Give the tool a clear description. The agent reads this description to decide when to use the tool, so be specific: "Use this to look up the current price and duration of a service. Returns all services." Vague descriptions are the most common reason an agent ignores a tool.

If you want the model to fill in a filter value itself (such as a service name), n8n lets you mark a parameter as defined by the model, which inserts a $fromAI() expression. For a small sheet, returning all rows is simpler and works well.

Write a system message that keeps the agent on track

The system message is the agent's job description. A clear one prevents most problems. Here is a template for a small hair salon that you can adapt:

You are the booking assistant for Studio Lina, a hair salon.

Your job:
- Answer questions about services, prices and opening hours.
- Always use the "price list" tool to check prices. Never guess a price.
- Use the calculator for any arithmetic.

Rules:
- Keep answers short: 2-3 sentences.
- If you do not know something or the tool returns nothing,
  say so and suggest calling the salon at the number on our website.
- Do not give medical advice about scalp or skin conditions.
- Reply in the language the customer writes in.

Opening hours: Tuesday-Saturday, 9:00-19:00. Closed Sunday and Monday.

Notice the pattern: who it is, what it should do, which tools to use for what, what to do when it does not know, and the fixed facts it can rely on. Static facts that rarely change can live in the system message; anything that changes often should come from a tool.

Run this on your own n8n, without the DevOps

AutomateSpot deploys a ready-to-use n8n server for you in minutes: SSL, backups and updates handled, so you can focus on building workflows.

Deploy n8n in minutes

Test it properly

Open the chat panel and try questions in three groups:

  1. Happy path: "How much is a haircut and colour?" Check that it calls the Google Sheets tool and quotes the right price.
  2. Follow-ups: "And how long does that take?" Check that memory works and it knows what "that" refers to.
  3. Edge cases: ask about a service you do not offer, ask for a discount, ask something unrelated. Check that it says it does not know instead of inventing.

After each test, open the execution and look at the agent's steps. n8n shows which tool was called, with which input, and what came back. This is how you debug an agent: not by guessing, but by reading what it actually did.

Connect it to a real channel

Once it behaves well in the test chat, decide where people will use it:

  • Public chat page or website widget: enable the public option on the Chat Trigger. Consider adding authentication if it exposes internal data.
  • Telegram or Slack: replace the Chat Trigger with the Telegram Trigger or Slack Trigger, set the agent's prompt source to "Define below", and send the agent's output back with a Telegram or Slack node.
  • Your own app: use a Webhook node as the trigger and a Respond to Webhook node at the end to return the answer.
  • WhatsApp: possible through the WhatsApp Business Platform; see our article on auto-replying to WhatsApp customers with AI for what that involves.

Remember to activate the workflow. An inactive workflow only runs while you test it in the editor.

Costs and limits

There are two cost components:

  • n8n itself. The self-hosted Community Edition is free to run under n8n's fair-code (Sustainable Use) license; you pay for the server. n8n Cloud and managed hosting are paid plans. Agent workflows count as normal executions.
  • The language model. Providers charge per token, for both the text sent (system message, memory, tool results) and the text generated. Check your provider's current pricing page. An agent that calls several tools per question makes several model calls, so one user message can cost more than you expect.

Practical limits to keep in mind: models can still make mistakes, so do not let an agent take irreversible actions (refunds, deletions, sending money) without a human check. Large tool outputs, such as a whole spreadsheet with thousands of rows, can exceed the model's context or get expensive; filter data before handing it to the agent. For more ways to keep AI usage affordable, see cost-effective AI automation with n8n.

Common mistakes and tips

  • Vague tool descriptions. The agent picks tools based on their names and descriptions. Write them like instructions for a new employee.
  • Too many tools at once. Start with two or three. Every extra tool is another option the model can get wrong.
  • Putting everything in the system message. Long price lists or catalogues belong in a tool, not in a prompt that is sent with every message.
  • No "I don't know" rule. Without one, models tend to produce a plausible-sounding answer. Tell it explicitly what to do when information is missing.
  • Forgetting the session key. If every user shares the same memory session, conversations get mixed up. Use a unique ID per user or chat.
  • Exposing personal data. If tools can read customer records, make sure only authorised people can reach the agent, and avoid sending more personal data to the model provider than needed.
  • Not reading executions. Review real conversations weekly and adjust the system message and tool descriptions based on what went wrong.
  • Add an error workflow. Set an error workflow (using the Error Trigger node) in the workflow settings so you are notified if the agent or a tool fails.

Where to go next

Once your first agent works, the natural next steps are giving it more tools (an HTTP Request tool to call your own APIs, a database tool, or a sub-workflow as a tool), switching to persistent memory, and connecting it to the channel your customers or team already use. If you want ideas for other AI workflows, our overview of AI automation workflows in n8n is a good starting point.

Bottom line

An n8n AI agent is a trigger, an AI Agent node, a chat model, memory and a few well-described tools. The hard part is not the wiring; it is a clear system message, good tool descriptions and honest testing.

Start small with one useful tool, read the executions, and expand only when the basics are reliable.

Written by AutomateSpot — helping small businesses automate smarter.