Skip to content
Mia Automation
NLEN Sign in Put Mia to workGet started

n8n AI tutorial: build AI steps and agents in n8n, with three tested examples

AI in n8n comes down to nodes that call a language model: either one prompt and one answer (an LLM chain), or an agent that picks its own tools. You plug in the model yourself, from a provider or running on your own machine. We built three AI workflows, from simple to agent, and tested them with a small local model. The biggest lesson: let the model read and write, and leave the math to plain code. That change took our agent from 4 correct answers out of 6 (and 2 in a middle version with more tools) to 6 out of 6.

Automation recipesUpdated 9 min read

Tested by us on 11 November 2026.

The AI nodes in n8n

An AI step in n8n is a root node with a chat model attached underneath. In n8n 2.47.3 we counted 20 chat model nodes, including OpenAI, Anthropic, Google Gemini, Mistral, DeepSeek, Groq, OpenRouter and Ollama for models on your own hardware.

Node list from n8n 2.47.3, November 11, 2026. n8n's AI docs cover each node in detail.
NodeWhat it doesWhen to use it
Basic LLM ChainSends one prompt to the model and returns one answerSummarizing, rewriting, picking a single word
Text ClassifierPicks a category from your listSorting, with a safety net behind it (see example 2)
Information ExtractorPulls fields out of free textGetting a name, date or amount from an email
AI AgentA model that calls tools until it has an answerQuestions where the model has to look something up first
ToolsWhat the agent can use: a table, another workflow, an MCP serverAnything the agent needs to read or do
MemoryKeeps the conversation historyChatbots with several turns
Animated diagram: a question goes to the agent, the agent calls a tool that calculates totals and days in code, then the agent phrases the answer and sends it back.
Example 3 in a nutshell: the model decides and writes, the code does the math.

What you need

  • n8n, self-hosted or in the cloud. We tested on n8n 2.47.3, self-hosted with Docker.
  • A chat model. With a hosted provider you create an API key and paste it into the chat model node. We used qwen2.5:7b through Ollama on an ordinary PC with a GTX 1660 Ti, so no key was needed.
  • For examples 1 and 2, an email account with IMAP. For example 3, two tables with quotes and invoices.
  • If Ollama runs as its own container next to n8n, set the Ollama credential's base URL to the service name, ours was http://ollama:11434. From a Docker container, localhost points at the container itself.

Example 1: summarize incoming email

  1. Trigger: Email Trigger (IMAP)

    Reads new mail from your inbox and marks it as read. Messages that arrive together come through as several items in one execution.

  2. Basic LLM Chain with a fixed prompt

    Give the model a strict two-line format and forbid it to make things up. Attach the chat model node underneath. Our prompt is below.

    text
    You summarize business emails for a busy business owner. Write in English, exactly two lines:
    Gist: <what the sender wants, in one sentence>
    Action: <what the owner needs to do, or 'none'>
    Do not make up anything that is not in the email.
  3. Send Email to yourself

    Sends the two lines with the original subject. If your team works in Slack or Teams, use that node instead.

In our test, five client emails arrived in one 11-second execution. The gist was right for all five. The action line wasn't: for three of the five the small model wrote *Action: None* even though the client was asking for something, like having someone look at a sticking cabinet door. Use a stronger model for this step, or leave the action line out. Template: 04-summarize-incoming-email.json.

Example 2: sort email with a safety net

Here the model picks one category out of nine. A plain rule handles mail from known senders, such as your suppliers, and only the rest goes to the model. A code step checks the answer, and anything that isn't on the list goes to a person. On 50 English test emails, the sender rule handled 8 (all correct) and the model the other 42. It got four wrong: two client questions it filed as an appointment and a quote request, and two pieces of noise (a sales pitch and a parcel notice). Nothing went to review in that run. We started with n8n's built-in Text Classifier node and dropped it. In our first run, with a custom system prompt that left out the list of categories (our mistake), one answer the node couldn't parse failed the whole batch of 38 emails with *Model output doesn't fit required format*. With the default prompt, one email out of 28 went in and never came out of the node, without an error. A Basic LLM Chain plus a code check gives one answer per email, every time. More variants are compared in AI automation. Template: 05-sort-email-rules-first.json.

Example 3: an agent that answers questions about your books

The agent gets a question like *Which invoice is the most overdue?* and looks up the answer itself. We asked six questions with known answers, including one it couldn't answer: a client's phone number, which isn't stored anywhere. We built three versions.

Bar chart: version 1 with two table tools got 4 right and 2 wrong, version 2 with an extra filter tool got 2 right and 4 wrong, version 3 where code does the math got 6 right, and the English rerun got 6 right.
More tools didn't help. Taking the math away from the model did. Versions 1 to 3 ran on our Dutch question set; the last bar is the same version 3 asked in English.
  • Version 1: two tools that return the invoice and quote tables; the model does its own math. Asked whether a client had anything outstanding, it only checked invoices and missed an open quote. Asked for the most overdue invoice, it picked one that wasn't even due yet.
  • Version 2: an extra tool that returns only overdue invoices. That question now worked, but the model added 363 and 605 to get 1,068, and offered a made-up email address when asked for a phone number.
  • Version 3 uses a single tool: a sub-workflow that reads both tables and computes totals, days overdue and what's open per client in a Code node. The model only turns that overview into plain language. Six out of six, on a repeat run too. The English rerun got all six right as well; in one earlier English run, it described an invoice due in a week as due today.
n8n execution of the agent: the question arrives through a webhook, the agent calls the chat model twice and the business overview tool once, then sends the answer back. (opens at full size)
Version 3 in action: two model calls, one tool call.

One thing to watch: for the phone-number question, the model didn't call the tool at all. The answer happened to be right, but it never looked at the data. The agent node in n8n 2.47.3 has an option that forces a tool call on the first step (forceToolCallOnFirstIteration); we haven't tested it yet. Templates: 11-ask-your-books-agent.json plus the helper workflow 12-business-overview-helper.json.

What an AI run in n8n costs

n8n shows how many tokens the model read and wrote in each execution (a token is roughly three quarters of a word). These are our averages from the English runs, priced with three models' official rates. Each model counts tokens a little differently, so treat them as an order of magnitude.

Measured November 11, 2026, in n8n 2.47.3. Prices per million tokens: Haiku 5.5 $0.10 in and $0.50 out; Flash-Lite $0.25 and $1.50; Sonnet 5.5 $2 and $10, from the official pricing pages.
WorkflowTokens in / out per runClaude Haiku 5.5Gemini 3.1 Flash-LiteClaude Sonnet 5.5
Summarize email100 / 28$0.02 per 1,000 runs$0.07 per 1,000 runs$0.48 per 1,000 runs
Sort email210 / 2$0.02 per 1,000 runs$0.06 per 1,000 runs$0.44 per 1,000 runs
Agent, per question750 / 40, two model calls$0.10 per 1,000 runs$0.25 per 1,000 runs$1.90 per 1,000 runs

An agent question uses about seven times the tokens of a summary, because the model works twice and reads the whole overview. A local model costs nothing per run, but you wait longer: our agent took 6 to 10 seconds per question (curl round trip included).

AI agents in n8n or in Python?

Build in n8n when your agent mostly calls existing apps, when someone who doesn't code needs to adjust it, or when there's an approval step. Every step is visible and every execution is saved. Write it in code when the logic itself is complex, when you want tests and line-by-line version control, or when the agent works on its own for a long time. We use both: n8n for the integrations and code for the math, as example 3 shows.

Import a ready-made workflow

In a new workflow, click the three dots next to the name, choose Import, then From file. Then connect your email account and your own chat model. All templates, including the ones without AI, are in our ten tested n8n workflows.

Questions

What is n8n, and what are its main features?

n8n is a platform for building workflows that do work across your apps on their own. For AI, it has nodes for a single model prompt, for sorting and extracting text, and for agents that use tools.

How do I build my first n8n workflow with an AI step?

Start with a trigger, such as new email, and add a Basic LLM Chain with a strict prompt. Attach a chat model and send the result to yourself. Or import our email summary template.

Should I build AI agents in n8n or in Python?

n8n suits agents that call apps and need to be visible to coworkers. Python suits complex logic and long autonomous jobs. You can combine the two.

Which AI models can I use in n8n, and what does a run cost?

n8n 2.47.3 has 20 chat model nodes, including OpenAI, Anthropic, Gemini, Mistral and Ollama for local models. In our measurements, a summary on a small hosted model came to about $0.02 per 1,000 runs, and an agent question to about $0.10 per 1,000 runs.

How do I import a ready-made workflow?

In a new workflow, click the three dots next to the name, then Import and From file or From URL. Then connect your own accounts.

Sources