n8n AI agents self-hosted with Ollama
A working n8n AI agent that uses a locally-hosted Ollama LLM — no OpenAI key, no API bill, no data leaving your server. The pattern, the credentials, the trade-offs, and the model choices that actually work for tool-using agents.
Why self-host the LLM behind n8n
- Cost. An OpenAI
gpt-4ocall for a typical agent run costs $0.01-0.05. At 1,000 runs/day that's $10-50/day, every day. A 16 GB self-hosted box is $30/month flat. - Data residency. Customer PII and proprietary content stays on your server.
- Predictable performance. No rate limits, no model deprecations, no surprise context-window changes.
- Offline / air-gapped. The agent works without internet access to LLM providers.
The trade-off: local models are smaller and less capable than frontier hosted models. They're great for tool-routing, summarization, structured extraction, and routine reasoning. They're meaningfully worse at long chain-of-thought and complex code generation.
Prerequisites
- n8n self-hosted — see install n8n on a VPS.
- Ollama running, with a tool-capable model pulled — see install Ollama.
- n8n version >= 1.40 (the modern
AI Agentnode).
Tested on the same box: n8n 1.71.3 + Ollama 0.4.6 + qwen2.5:14b on a 16 GB Inferno VPS in Frankfurt.
Pick a model that can actually use tools
Not every open-weight model handles tool calls reliably. Some emit broken JSON; some hallucinate tool names. As of mid-2026 the models we trust for n8n agents:
| Model | Tool reliability | Speed (CPU, 16 GB) |
|---|---|---|
qwen2.5:14b | Strong | ~6 tok/s |
llama3.1:8b | Good | ~11 tok/s |
qwen2.5-coder:14b | Strong (code-bias) | ~6 tok/s |
mistral-small:24b | Very strong | ~3 tok/s |
For a starter, pull qwen2.5:14b — best balance of reasoning, tool-use reliability, and speed.
Step 1 — Connect n8n to Ollama
In n8n: Credentials → New → "Ollama API." Set the Base URL to:
http://host.docker.internal:11434— if n8n runs in Docker and Ollama natively on the host.http://ollama:11434— if both run in the same Compose stack and you've named the Ollama serviceollama.http://127.0.0.1:11434— if n8n runs natively (not in Docker).
If you exposed Ollama with token auth (see install guide), use the public HTTPS URL and pass the bearer token. For same-box setups the unauth localhost route is cleaner.
Click "Test" — green tick if Ollama responds.
Step 2 — Build the agent workflow
- Add a Webhook node (or any trigger). This is the agent's entry point.
- Add an AI Agent node. Connect the trigger to its input.
- On the AI Agent, set Chat Model → "Ollama Chat Model" → pick the credential and model (
qwen2.5:14b). - Add Memory — "Window Buffer Memory" with sessionId from the webhook payload, last 10 messages.
- Add Tools the agent can call — see below.
Useful tools to attach
HTTP Request tool
The Swiss-army knife. Lets the agent call any HTTP API — your CRM, an internal microservice, a webhook back to the user. Describe the API and parameters in the tool description so the model knows when to call it.
Postgres tool
For "look up customer X in our database" patterns. Describe the relevant tables in the tool description; the model writes the SQL.
Heads up: giving an LLM raw SQL access is a real risk. Constrain to a read-only Postgres user, or wrap the query in a stored procedure with parameter validation.
Code (JavaScript) tool
The agent emits JS that runs in n8n's sandbox. Useful for date math, string parsing, JSON shape transformations. The model is given the input shape and asked to produce code that returns the right output.
Webhook back to caller
For long-running agents, push status updates to the user mid-flight (Telegram message, Slack thread reply, server-sent event).
System prompt that works
On the AI Agent node, set the System Prompt to something concrete. Open-weight models are more sensitive to vague prompts than hosted ones.
You are a helpful operations agent for ACME Inc.
You can:
- Look up customers by email via the "find_customer" tool.
- Fetch order status via the "get_order_status" tool.
- Send a follow-up email via the "send_email" tool.
Rules:
- Always confirm the customer's email before sending email.
- Never invent order IDs. If a tool returns no data, say so plainly.
- Reply concisely; one paragraph unless the user asks for detail.
- If you don't have a tool for what the user asks, say so and stop.
Current date: {{ $now.toISO() }}.
Memory: pick the right type
- Window Buffer Memory — last N messages. Cheap, simple, good default for chat-style agents.
- Vector Memory — embeds past messages, retrieves relevant ones. Better for long conversations; requires an embedding model (your Ollama can serve one too —
nomic-embed-text). - No memory — for stateless agents that handle one independent request at a time.
Expected behavior and limits
From real workflows running on the test stack:
- Single-tool calls (one HTTP fetch, then respond): ~3-6 seconds end to end with
qwen2.5:14bon 16 GB CPU. - Multi-step (3-5 tool calls + summary): 15-40 seconds.
- Anything that needs 10+ tool calls or extensive reasoning: meaningfully slower than a hosted GPT-class model. Re-architect the workflow into smaller deterministic n8n steps with the LLM doing only the parts that need natural-language reasoning.
Common self-hosted-agent mistakes
"Agent never calls tools, just chats"
The model isn't tool-capable, or the tool descriptions are too vague. Use one of the recommended models above and write concrete descriptions ("Use this tool to fetch the status of an order. Required parameter: order_id (string).").
"Agent calls tools but with garbage arguments"
Smaller models fabricate. Either upgrade to a 14B+ tool-tuned model or add validation in the tool that rejects bad inputs and lets the agent retry.
"Workflow times out after 60 seconds"
n8n's default execution timeout. For agents that take longer, raise EXECUTIONS_TIMEOUT in env or switch to queue mode, which lets you handle longer-running executions cleanly.
"RAM hits cap, Ollama gets killed"
You loaded too many models or too large a context. Tune OLLAMA_MAX_LOADED_MODELS=1 and reduce context size in the n8n node config.
"Tool output too big to fit in context"
The agent fetched a 50 KB JSON and now the next step blows past the context window. Pre-process in n8n (filter, summarize) before feeding back into the agent.
VPS for n8n + Ollama on one box?
The sweet spot is a 16 GB Inferno plan — runs n8n + Postgres + Redis + Ollama + a 14B model comfortably. Single box, no inter-machine network hops on every tool call.
What to do next
- Install Ollama if you haven't.
- Install n8n on the same VPS.
- Queue mode for long-running agent runs.
- Best VPS for Ollama — sizing for the LLM side.
Author: Marek Vainionpää — SRE, Helsinki. Last tested: 2026-05-22 with n8n 1.71.3 AI Agent node, Ollama 0.4.6, qwen2.5:14b, 16 GB AMD EPYC Inferno VPS.