Dev tools
n8n Tutorial: Automate Workflows With AI
A beginner n8n tutorial: set up n8n, build an AI email triage workflow, then create an AI agent with memory and tools, step by step.
This n8n tutorial takes you from zero to two working AI automations. n8n is a visual workflow tool. You connect boxes called nodes on a canvas, and each one does a job: watch your inbox, ask an AI model a question, post to Slack, update a spreadsheet. Because it has built-in AI nodes, it's one of the most popular ways to add AI to everyday business tasks without writing much code.
You'll learn how n8n works, how to set it up, and how to build two projects. The first is an email triage workflow that uses AI to sort incoming mail. The second is an AI agent with memory and tools that you can chat with.
What is n8n?
n8n (pronounced "n-eight-n") is a workflow automation platform. It connects to more than a thousand apps and services, and you can run it in the cloud or on your own server. It's built for both non-coders, who use the visual editor, and developers, who can drop in JavaScript or Python wherever they need it.
Workflows, nodes, and triggers
Everything in n8n is a workflow: a chain of nodes that runs from left to right.
- Trigger nodes start the workflow, such as a new email, a form submission, a schedule, or a webhook call from another app.
- Action nodes do something, such as send a message, add a row to a sheet, or call an API.
- Logic nodes shape the flow, such as If for yes or no decisions, or Edit Fields to rename and clean data.
- AI nodes bring in language models, such as the AI Agent, Text Classifier, Information Extractor, and chat model nodes.
Data flows between nodes as items, and each node can read what earlier nodes produced.
Is n8n free?
A common question is "is n8n free," and the answer depends on how you run it. The self-hosted Community Edition is free to use. n8n is "fair-code," released under its Sustainable Use License, which lets you use it freely for your own and your company's internal automations. It does limit things like reselling n8n itself as a hosted service.
n8n Cloud, the hosted version, is a paid subscription with a free trial. Either way, you pay separately for any AI models you connect, such as OpenAI or Anthropic API usage. Check n8n's pricing page for current plans.
How to set up n8n: cloud or self-hosted
Option 1: n8n Cloud
The fastest start. Sign up on n8n's website, and you'll land in the editor a few minutes later. n8n handles hosting, updates, and uptime. This is the best choice if you just want to learn or don't want to manage a server.
Option 2: n8n self-hosted with Docker
An n8n self-hosted setup gives you full control over your data and no limits from a cloud plan. If you have Docker installed, the official commands are:
docker volume create n8n_data
docker run -it --rm --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n docker.n8n.io/n8nio/n8nThen open http://localhost:5678 in your browser and create your owner account. The volume keeps your workflows and credentials safe when the container restarts.
n8n also offers a one-line install script that uses Docker: curl -fsSL https://get.n8n.io | sh.
Option 3: Try it with Node.js
If you have Node.js 24 or newer, you can run npx n8n to try it locally without Docker. This is fine for experimenting, but use Docker or the cloud for anything you depend on.
For a server that runs around the clock, receives webhooks from the internet, and is properly secured, you'll need a cloud server, a domain, and HTTPS. If that sounds like a lot, start with n8n Cloud and move later. You can export workflows and import them elsewhere.
n8n tutorial part 1: an AI email triage workflow
Our first project reads each new email, uses AI to decide whether it's urgent, a sales lead, or something else, then alerts you on Slack for urgent mail and labels the rest in Gmail.
Step 1: Create a workflow and add the Gmail Trigger
Create a new workflow and click the plus button to add your first step. Search for Gmail Trigger and choose the event for new messages.
You'll need to connect your Google account by creating a credential. n8n walks you through signing in. Credentials are stored separately from workflows, so your passwords and keys never sit inside the workflow itself. Set how often the trigger checks for new mail, then fetch a test event so you have a real email to work with.
Step 2: Add the Text Classifier
Click the plus after the trigger and add the Text Classifier node. In the text field, insert the email's subject and body. Drag them in from the input panel, or type an expression like:
Subject: {{ $json.subject }}
Body: {{ $json.text }}The exact field names depend on what your trigger returns, so check the input panel on the left.
Now define your categories, each with a short description the AI will use:
- Urgent: a customer problem, outage, or deadline that needs a reply today.
- Sales lead: someone asking about pricing, a demo, or buying.
- Other: newsletters, notifications, and everything else.
The Text Classifier needs a model, so connect a chat model sub-node to it, such as OpenAI Chat Model, Anthropic Chat Model, or Google Gemini Chat Model. Add a credential with your API key.
Step 3: Add an action for each branch
The Text Classifier gives you one output per category, so each can go its own way:
- Urgent → add a Slack node that sends a message to your channel with the subject and sender.
- Sales lead → add a Gmail node that adds a "Lead" label, or a Google Sheets node that logs the lead.
- Other → add a Gmail node that adds a "Later" label, or leave it empty.
Step 4: Test each step
Run the workflow manually with your test email and watch each node's output. Click any node to see exactly what went in and what came out. This is the fastest way to find problems.
Tip: "pin" the trigger's test data, so you can rerun later steps without waiting for a new email.
Step 5: Turn it on
When it works, activate the workflow with the switch or publish button at the top of the editor, depending on your n8n version. From now on it runs on its own, and you can review every run in the Executions list.
n8n tutorial part 2: build an n8n AI agent
The email workflow follows fixed steps. An agent is different: you give it a goal and tools, and it decides what to do. Let's build an n8n AI agent you can chat with, which remembers the conversation and can look things up in a spreadsheet.
Step 1: Add a Chat Trigger
Create a new workflow and add the Chat Trigger node. It gives you a chat window inside n8n for testing, and it can also be made public as a chat page or embedded on a website.
Step 2: Add the AI Agent node
Add the AI Agent node after the trigger. This is where the thinking happens. Open it and write a system message that tells the agent its job, for example:
You are an assistant for our sales team. Use the Products sheet to answer questions about our products and prices. If you can't find an answer, say so. Never guess prices.
Step 3: Connect a chat model
The AI Agent has slots underneath for sub-nodes. In the model slot, add a chat model such as Anthropic Chat Model or OpenAI Chat Model. If you want to keep everything on your own machine, the Ollama Chat Model node connects to models running locally through Ollama.
Step 4: Add memory
In the memory slot, add Simple Memory. It keeps the last few messages of each conversation, so the agent remembers what you said a moment ago. You can choose how many past messages it keeps. For conversations that must survive restarts, n8n also offers database-backed memory options.
Step 5: Give it tools
Tools are what make it an agent. In the tool slot, add a Google Sheets tool pointing to your product sheet. Describe clearly what it does and when to use it, since the agent reads these descriptions to decide which tool to call.
Other useful tools include:
- HTTP Request Tool: lets the agent call any web API.
- Call n8n Workflow Tool: lets the agent run another one of your workflows as a tool, which is powerful for reusing logic.
- MCP Client Tool: connects the agent to any MCP server, so it can use tools from other systems. Our guide on how to build an MCP server shows how to make your own.
Step 6: Test in the chat
Open the chat panel and ask, "What does the Pro plan include?" Watch the execution: you'll see the agent call the Sheets tool, read the result, and write an answer. Then ask a follow-up, like "and how much is it?", to check that memory works.
If you're curious about what's happening inside that loop of thinking, calling tools, and checking results, our guide on how to build an AI agent explains it step by step.
n8n basics worth learning early
Expressions. Anything in double curly braces is an expression, for example {{ $json.email }}. It pulls data from the incoming item. You can also reach back to earlier nodes and use simple JavaScript inside expressions.
The Code node. When no node does exactly what you need, the Code node lets you write a few lines of JavaScript or Python.
Templates. n8n has a large public library of ready-made workflows. Importing one is often faster than starting from scratch, and a good way to learn patterns.
Sub-workflows. Split big workflows into smaller ones that call each other, using the Execute Workflow Trigger. They're easier to test and reuse.
Tips for reliable AI workflows
- Set up an error workflow. Create a workflow that starts with the Error Trigger and sends you a message, then select it in your other workflows' settings. You'll hear about failures right away.
- Keep AI steps narrow. A Text Classifier or Information Extractor with clear instructions is more predictable than a general agent. Use agents only where the steps really vary.
- Limit agent loops. The AI Agent has a setting for the maximum number of iterations. Keep it modest so a confused agent can't run up costs.
- Keep humans in the loop for risky actions. Before sending emails to customers or changing records, route the AI's output to a person for approval.
- Watch your model costs. Each AI node call uses tokens. Use smaller models for simple jobs like classification.
n8n vs Zapier: which should you use?
The n8n vs Zapier question comes up constantly, and both are good tools for different people.
Zapier is the easiest place to start. It has the largest catalog of app integrations and a very simple, step-by-step editor. It's fully hosted, so there's nothing to manage. Costs can climb at high volumes, because pricing is based on the number of tasks your automations perform.
n8n has a steeper learning curve, but more power. The canvas handles branches, loops, and complex logic more naturally. You can write code anywhere, and its AI and agent nodes are deep. You can self-host it for free and keep all data on your own servers. On n8n Cloud, pricing is based on workflow runs rather than each individual step, which tends to favor long, multi-step workflows.
Make sits between the two: a visual editor with more flexibility than Zapier and less setup than self-hosted n8n.
A simple rule: choose Zapier for quick, simple automations between popular apps. Choose n8n for complex workflows, AI agents, heavy volumes, or when you need control over your data.
FAQ
Is n8n free?
The self-hosted Community Edition is free under n8n's fair-code license for your own and your company's internal use. n8n Cloud is a paid service with a free trial. AI model usage is billed separately by the model provider.
Is n8n hard to learn?
The basics take an afternoon: triggers, actions, and connecting nodes. Data handling and expressions take longer to master. Building small, real workflows like the ones in this tutorial is the fastest way to learn.
Do I need to know how to code to use n8n?
No. Most workflows can be built entirely with the visual editor. Knowing a little JavaScript helps with expressions and the Code node, but it's optional.
Can n8n use local AI models?
Yes. The Ollama Chat Model node connects to models running on your own computer or server through Ollama. Combined with self-hosted n8n, this keeps all your data in-house.
What can I automate with n8n?
Common uses include email triage, lead routing, CRM updates, report generation, support chatbots, document processing, social media posting, and syncing data between apps. If an app has an API, n8n can usually connect to it.