Blog

Leanroute for n8n: a community node for AI Agent workflows

4 min readLeanroute Team

n8n workflows run when nobody is watching. A schedule trigger fires every five minutes, a webhook gets hammered by a misbehaving client, an AI Agent decides it needs one more tool call. That is exactly the situation where you want a gateway between your workflows and the model providers: something that caps the spend, caches the repeats, and keeps the workflow running when one provider has a bad hour.

Until now, the way to put Leanroute behind n8n was the built-in OpenAI node with a custom base URL. It works, but you type model ids by hand and switch fields into expression mode to get past the OpenAI-only picker. So we built a proper community node: n8n-nodes-leanroute.

Leanroute Chat Model connected to an n8n AI Agent

What you get

The package adds two nodes and a credential:

  • Leanroute Chat Model. A chat model for n8n's AI Agent (and anything else that takes a language model). Pick a model from the list or type any id from the catalog.
  • Leanroute. A regular node for single calls outside an agent: send a prompt to a model, or list every model your key can reach.
  • Leanroute API credential. Your Leanroute API key, entered once and shared by both nodes.

Install it in five minutes

You need a self-hosted n8n instance and an owner account on it.

  1. Create an API key at Dashboard, API keys. One key per workflow is a good habit, for reasons covered below.
  2. In n8n, open Settings, Community Nodes, Install, enter n8n-nodes-leanroute and install.
  3. In a workflow, add a Chat Trigger and an AI Agent. In the agent's Chat Model slot, choose Leanroute Chat Model and create the credential with your key.
  4. Pick a model and open the chat panel. Send "hi".

The reply comes back through Leanroute, and the request shows up in your request logs with the provider that answered and what it cost.

No n8n yet? This runs it locally with Docker:

docker run -it --rm -p 5678:5678 -v n8n_data:/home/node/.n8n docker.n8n.io/n8nio/n8n

The settings that matter

The Chat Model node keeps its options short:

  • Model. A list of current models from DeepSeek, Anthropic, OpenAI, Google and Alibaba, plus Custom Model ID for anything else in the catalog, such as Kimi, GLM or Sarvam. Use the provider/model form, for example anthropic/claude-sonnet-5.
  • Temperature and Max Tokens. Max tokens defaults to 4096. Lower it for agents that only need short answers; it also bounds the cost of a single call.
  • Disable Gateway Model Swaps. If your organization has cheaper-model swaps turned on, the gateway may serve a request with a cheaper model in the same quality tier. Switch this on to pin a node to exactly the model you picked. It sends x-gateway-routing: explicit on every call.

Why route n8n through a gateway at all

Spend caps per workflow. Give each workflow its own API key and set a daily cap on that key in the dashboard. An experimental flow on a one-minute schedule with a $1 daily cap can misbehave all night and cost you a dollar. When the cap is hit, the node fails with a clear 402 spend_cap_exceeded in the execution log instead of quietly running up a bill. More in Spend caps and rate limits.

Caching for repetitive automations. Workflows repeat themselves: the same classification prompt for the same kind of email, the same summary request for a webhook that fires twice. Identical and near-identical requests are answered from cache for 15 minutes and billed at $0, with nothing to set up.

Failover when a provider is down. If the provider behind your model returns an error, the gateway retries on an equivalent model from another provider, so a scheduled run doesn't fail because one vendor had an outage.

Guardrails before data leaves. Workflows move real data: customer emails, CRM records, support tickets. With PII redaction on, email addresses and card numbers are replaced before the prompt reaches any provider. See LLM guardrails.

One key, every provider. Switch a node from DeepSeek to Claude to Gemini by changing a dropdown, not by adding three credentials.

What to know before you rely on it

Being specific about the limits:

  • n8n Cloud. Cloud only allows community nodes that n8n has verified, and this one isn't verified yet. On Cloud, use the built-in OpenAI node with base URL https://api.leanroute.dev/v1; the n8n guide walks through it.
  • Agents with tools. When the AI Agent passes tools to the model, the gateway never swaps the model and doesn't fail over to another provider, because tool-call formats differ between providers and a silent switch could break the agent. Caps, caching rules, guardrails and logging still apply.
  • BYOK plans. On a bring-your-own-key plan, pick a model from a provider you've added a key for. Other models return provider_not_configured with a list of models your keys can call.

Get started

Install n8n-nodes-leanroute, point a Chat Model at it, and set a daily cap on the key before you schedule anything. The source is on GitHub, where issues and pull requests are welcome, and the full setup guide is at /docs/integrations/n8n.

n8nAI Agentworkflow automationLLM Gateway