← Back to tutorials

n8n Complete Tutorial: Build AI-Powered Automation Workflows

Master n8n for connecting AI tools, APIs, and databases

n8n AI Automation Tutorial: Build Your First AI Workflows

n8n's pitch over Zapier-class tools is simple: self-hosted (your data stays home), source-available, and AI is a first-class citizen — native AI Agent nodes with LangChain under the hood, not just "call an API" steps. This hands-on tutorial builds two real AI workflows: an email triage pipeline and a RAG-powered Q&A bot over your documents. (Platform comparison and the 10-workflow idea list live in the AI office automation guide; 中文总览见 n8n 完整指南.)

Setup (5 minutes)

bash
docker run -d --name n8n -p 5678:5678 \
  -v n8n_data:/home/node/.n8n \
  docker.n8n.io/n8nio/n8n

Open localhost:5678, create the owner account. Add AI credentials under Credentials: an OpenAI/Anthropic key — or point the OpenAI credential's base URL at http://host.docker.internal:11434/v1 to use local Ollama models (the fully-private stack).

Workflow 1: AI email triage

Shape: IMAP/Gmail Trigger → AI classification → route → act.

  • Gmail Trigger node: fires on new email.
  • AI node (Message a Model / Basic LLM Chain) with a structured prompt:
  • text
    Classify this email. Return JSON only:
    {"category": "support" | "sales" | "billing" | "spam" | "other",
     "urgency": "high" | "normal",
     "summary": ""}
    Email subject: {{ $json.subject }}
    Body: {{ $json.text }}
    

  • Structured Output Parser attached to the AI node — n8n validates the JSON against your schema, so downstream nodes get clean fields instead of prose. (This is the platform version of the always-validate rule.)
  • Switch node on category → create a ticket (support), notify #sales (sales), label-and-archive (spam).
  • For urgency: high, a Slack DM with the summary.
  • The pattern to internalize: Trigger → AI-with-schema → Switch → actions — 80% of useful AI automations are this shape with different nouns.

    Workflow 2: RAG Q&A over your documents

    n8n has native vector-store and embedding nodes, so a document chatbot is a two-workflow build:

    Ingestion workflow: folder/Drive trigger → Default Data Loader (splits documents) → Embeddings node → Vector Store node (insert mode — Qdrant, pgvector, or in-memory for testing).

    Query workflow: Chat Trigger (n8n hosts a chat UI for you) → AI Agent node with the vector store attached as a retrieval *tool* → the agent searches your docs and answers with context, deciding per question whether to retrieve.

    That AI Agent node is the differentiator: give it multiple tools (vector store + HTTP request + calculator + your custom sub-workflows) and it does genuine tool-calling loops within a no-code canvas — the spectrum between "automation" and "agent" without writing the loop yourself. (Chunking/retrieval quality concepts transfer directly: semantic search guide.)

    Production habits

  • Pin a model per workflow and note the prompt version in the workflow description — same prompts-are-code discipline as in code.
  • Error workflow: configure a global error handler (n8n supports a dedicated error workflow) that alerts you with the failed node + payload — silent automation failures are worse than no automation.
  • Human gates for consequential actions: n8n's Wait/Approval patterns (e.g. send-and-wait on Slack) before anything irreversible — the approval-gate principle.
  • Cost control: classification on mini-tier models; cache repeated prompts where possible; watch per-execution token logs.
  • Backups: n8n_data volume holds everything — snapshot it.
  • FAQ

    n8n free vs paid? Self-hosted core is free (sustainable-use license — fine for internal use; embedding/reselling needs review). Cloud and enterprise add hosting, SSO, environments.

    JavaScript when nodes run out? The Code node accepts JS/Python — the escape hatch that keeps "no-code" from becoming "no-way".

    vs Zapier/Make for AI specifically? n8n's agent/RAG nodes are a tier deeper than both; the trade is hosting it yourself — full comparison in n8n vs Zapier vs Make.


    *Last updated: June 2026. Node names/features move fast — verify against n8n docs.*

    Also available in 中文.