Skip to content

Building AI Workflows with n8n

Updated Aug 2026

verified on Ubuntu 26.04 · Aug 2026

Learn how to build AI-powered automations with n8n — connect Ollama, OpenAI, and other LLMs to create intelligent workflows that process data, generate content, and make decisions.

Before you start
  • n8n running on a VPS (see Deploy n8n on a VPS)
  • At least one LLM backend configured (Ollama, OpenAI, etc.)
  • Basic understanding of n8n's visual workflow editor

Why AI + n8n?

n8n is a workflow automation tool, and adding AI to it turns simple automations into intelligent ones. Instead of rigid if-then logic, your workflows can understand text, classify data, generate responses, and make decisions based on context. The appeal is automation without rules — AI can handle edge cases that would require dozens of if-else branches.

This guide assumes you already have n8n running. If not, start with Deploy n8n on a VPS to get the base setup. Once n8n is live, come back here to add AI capabilities.

Connecting n8n to LLM backends

n8n supports several AI providers out of the box. The configuration depends on your backend:

Ollama (self-hosted, free):

  1. In n8n, go to SettingsCredentialsAdd Credential
  2. Search for "Ollama"
  3. Set the base URL to your Ollama instance (e.g., http://host.docker.internal:11434)
  4. Save

OpenAI:

  1. Add an "OpenAI API" credential
  2. Enter your API key
  3. Save

Anthropic:

  1. Add an "Anthropic API" credential
  2. Enter your API key
  3. Save

Once configured, these credentials appear in any AI node you add to a workflow.

Core AI nodes

n8n has several AI-specific nodes:

AI Agent: The most powerful node. It takes a prompt, connects to an LLM, and can use tools (web search, code execution, API calls) to complete tasks. Think of it as an autonomous worker that can research, reason, and act.

LLM Chain: Sends a prompt to an LLM and returns the response. Simple but useful for classification, summarization, or generation tasks.

Text Classifier: Uses an LLM to classify text into categories you define. Useful for routing support tickets, categorizing feedback, or filtering content.

Summarizer: Takes long text and produces a concise summary. Good for processing articles, reports, or conversation logs.

QA Chain: Answers questions based on provided context. Useful for building a knowledge base where you supply the documents and the AI answers questions about them.

Example: AI-powered support ticket routing

Here's a practical example: automatically route support tickets based on their content.

Workflow:

  1. Trigger: Webhook receives a support ticket (email, form submission, etc.)
  2. Text Classifier: Analyze the ticket content
  3. Categories: Billing, Technical, Feature Request, General
  4. Switch: Route to the right team based on classification
  5. AI Agent: Draft a response acknowledging the ticket
  6. Send Email: Notify the customer with the AI-drafted response

Configuration:

  • The Text Classifier node gets categories from a spreadsheet or hardcoded list
  • The AI Agent uses your Ollama or OpenAI credential
  • The response is reviewed by a human before sending (optional)

This turns a manual triage process into an automated one with AI understanding the nuance of each ticket.

Example: content generation pipeline

Generate social media content from a list of topics:

Workflow:

  1. Trigger: Schedule (runs daily)
  2. Google Sheets: Read a list of topics
  3. AI Agent: Generate a social media post for each topic
  4. Filter: Remove low-quality outputs
  5. Buffer/Social Media: Post to your channels

Configuration:

  • The AI Agent gets a prompt like "Write a engaging social media post about {topic}. Keep it under 280 characters. Include relevant hashtags."
  • Use a local Ollama model for free generation, or OpenAI for higher quality
  • The Filter node can use simple length checks or another AI call to score quality

Example: email processing with AI

Automatically process incoming emails:

Workflow:

  1. Trigger: IMAP node watches an inbox
  2. AI Agent: Analyze the email content
  3. Switch: Route based on intent (inquiry, complaint, spam, newsletter)
  4. For inquiries: Draft a response and send for review
  5. For complaints: Create a ticket in your helpdesk
  6. For spam: Delete or archive

Configuration:

  • The AI Agent uses a prompt that understands your business context
  • Include your company name and common topics in the system prompt
  • Route to different LLM models based on complexity (small model for simple routing, larger model for drafting responses)

Using Ollama for cost-free AI

If you're running Ollama on the same server as n8n, you can use it for all AI operations without API costs:

Advantages:

  • Zero per-token cost
  • Data stays on your server
  • No rate limits
  • Works offline

Limitations:

  • Slower than cloud APIs (especially on CPU)
  • Quality varies by model size
  • Requires RAM for the model

Best practices:

  • Use smaller models (3B-7B) for simple tasks like classification
  • Reserve larger models (13B+) for complex reasoning
  • Cache common responses to reduce inference load
  • Run Ollama on a GPU-equipped VPS for better performance

Error handling

AI nodes can fail for several reasons. Add error handling to your workflows:

Rate limiting: If using cloud APIs, add a Wait node between AI calls to avoid rate limits. For Ollama, this isn't an issue.

Timeout: Large models can be slow. Set appropriate timeouts in the HTTP Request node if you're making custom API calls.

Invalid responses: LLMs sometimes return unexpected formats. Use the IF node to validate responses before passing them downstream.

Fallback models: Configure a fallback credential in case your primary model is unavailable. The AI Agent node supports this natively.

Backups and version control

n8n workflows are stored in the database. Back them up regularly:

docker compose exec -T postgres pg_dump -U n8n n8n | gzip > n8n-workflows-$(date +%F).sql.gz

For version control, export workflows as JSON from the n8n UI and commit them to Git. This lets you track changes and roll back if needed.

Performance tips

Use webhooks, not polling: Webhooks trigger instantly; polling adds delay. For real-time AI processing, webhooks are essential.

Batch processing: If you're processing many items, use the Split In Batches node to avoid overwhelming your LLM backend. Process 5-10 items at a time with a small delay between batches.

Cache common responses: If you're asking the same question repeatedly, cache the response in a database or file. The AI node can check the cache first and only call the LLM if the response isn't cached.

Choose the right model: Not every task needs GPT-4. Use smaller, faster models for simple classification and larger models for complex reasoning. The cost difference is significant.

Troubleshooting

AI node returns empty responses. Check the credential configuration and verify the LLM backend is running. For Ollama, confirm a model is pulled and the base URL is correct.

Workflow is slow. AI inference takes time, especially on CPU. Consider using a GPU-equipped VPS, smaller models, or caching frequent responses.

Token limit errors. LLMs have context windows. If your input is too long, split it into chunks or use a model with a larger context window.

Webhook not triggering. Check that n8n is accessible from the internet and the webhook URL is correct. For production use, add authentication to your webhooks.

Verification + next steps

You're done when you can: create a workflow with an AI node, connect it to your LLM backend, and process data through it. Test with a simple classification task, then build more complex workflows.

From here, explore n8n's AI features like memory (keeping conversation context), tools (letting AI call APIs), and retrieval-augmented generation (RAG) for knowledge-based workflows. For the base n8n setup, see Deploy n8n on a VPS. For document-based AI, see AnythingLLM. A Hetzner CX22 (2 vCPU / 4 GB) handles n8n with Ollama; for heavier AI workloads, consider a CX32 (4 vCPU / 8 GB) or GPU instance. See Best VPS for AI & ML Workloads for the ranked picks.

Next steps

Search SelfHost Atlas

Search apps, comparisons, guides, and categories.

We use analytics cookies (Google Analytics, PostHog) to see which guides are useful. No ad networks, no cross-site tracking. See our privacy policy.