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.

Disclosure: contains an affiliate link to Inferno VPS — the host this stack runs on.

Why self-host the LLM behind n8n

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

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:

ModelTool reliabilitySpeed (CPU, 16 GB)
qwen2.5:14bStrong~6 tok/s
llama3.1:8bGood~11 tok/s
qwen2.5-coder:14bStrong (code-bias)~6 tok/s
mistral-small:24bVery 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:

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

  1. Add a Webhook node (or any trigger). This is the agent's entry point.
  2. Add an AI Agent node. Connect the trigger to its input.
  3. On the AI Agent, set Chat Model → "Ollama Chat Model" → pick the credential and model (qwen2.5:14b).
  4. Add Memory — "Window Buffer Memory" with sessionId from the webhook payload, last 10 messages.
  5. 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

Expected behavior and limits

From real workflows running on the test stack:

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.

View Inferno plans →

What to do next


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.