How to Cut n8n Agent Token Costs with RAG and Cheap Routing

November 9, 2026

An n8n AI Agent that answers questions about your documentation is cheap on day one and startling on day thirty. The reason is structural: every agent step resends the accumulated context, so cost grows with the square of conversation length, not linearly.

This is the optimisation pass. Four changes, each with a measurable effect, and none of them require leaving the workflow editor.

Why does agent cost grow faster than the conversation?

A three-step agent run sends three model calls: call one has the initial context, call two has that plus the first tool result, call three has everything before plus the second result. Prompt tokens are re-billed on every call.

A one-shot RAG question is one call of 2,000 tokens. The same question through a three-step agent is roughly 2,000 plus 3,000 plus 4,000, or 9,000. The agent is four to five times the cost of the direct path for the same answer.

That is fine when the task needs the loop. It is waste when the task is a lookup.

How do you add a vector store as a tool?

In n8n, attach a vector store node (Pinecone, Qdrant, Supabase, or the built-in in-memory store) as a tool on the agent, with an embeddings model. The agent then has a query_docs action.

The quality problem here is chunking, not the node. Two rules:

  • Chunk at 400–800 tokens with 10–15% overlap. Smaller chunks retrieve more precisely but need more of them to answer; larger chunks drag irrelevant text into the prompt. – Keep metadata on every chunk — page title, section, last-updated date. Then the agent can cite a source, and you can filter stale documents out of the tool call.

Without metadata, the agent answers from a 2023 doc with the same confidence as this week’s, and nobody finds out until a customer quotes it back.

How does a router model cut cost?

Put a cheap classification step before the agent. Two or three branches:

  • Lookup — “what is the refund window” — answered by a single vector store query and one small model call. No agent loop. – Action — “cancel order 8841” — goes to the agent with tools. – Unknown — goes to the agent with a note that it may need to ask a clarifying question.

A small model doing three-way classification on a short message costs well under a tenth of a frontier-model agent step. If 70% of your traffic is lookups, routing takes 70% of your requests down a path that is four times cheaper.

Accuracy of the router matters more than its price. Test it on 100 real messages; below 85% agreement with a human label, it is sending requests the wrong way and you should merge two branches.

How do you bound the context a tool returns?

This is where most of the savings live and most teams miss it. A vector store tool that returns ten chunks of 800 tokens each hands the agent 8,000 tokens per call, and that repeats on the next step.

Set the retrieval tool to return three chunks, and post-process to trim each to 400 tokens. Three chunks at 400 tokens is 1,200 — a 6x reduction in what the agent carries forward.

If the answer genuinely needs more context, the agent can query twice. That is cheaper than carrying eleven chunks through every subsequent step.

What about memory?

A window-buffer memory of 10 turns is a reasonable default. The two mistakes:

  • Unbounded memory on a long-running workflow reads every prior turn into the prompt. – Full prior messages stored raw, including the full text of every tool result. Store summaries of tool results, not their bodies, and the memory stays small as the workflow ages.

For stateless workflows — an agent invoked per email — turn memory off entirely. It is pure overhead and some n8n configurations enable it by default.

How do you measure whether it worked?

Track tokens per successful task, not tokens per request. A cheaper pipeline that needs two attempts is not cheaper.

The measurement loop in n8n: log tokens_in, tokens_out, the route taken, and whether the task succeeded, to a table at the end of each workflow. After a week you have per-route unit cost, which is the only number that justifies a further optimisation round.

Also track the escalation rate. If routing sends 40% of lookups to the agent because classification is bad, the routing layer is paying for itself in overhead and nothing else.

When is optimisation premature?

Under a thousand runs a day on a cheap model, all of this is noise. The engineering time to build routing and tune chunking exceeds a year of token spend. Do the retrieval bounding first — it is a config change — and revisit the rest when a monthly bill starts appearing in a budget conversation.

Quick Summary

  • Agent prompts are re-billed every step, so a three-step agent can cost four to five times a single RAG call for the same answer.

  • Route short lookup questions to a one-shot vector query and reserve the agent loop for actions and ambiguous requests.

  • Return three trimmed chunks from the retrieval tool rather than ten full ones; that is usually the single largest saving.

  • Store summaries of tool results in memory, or turn memory off for stateless workflows.

  • Measure tokens per successful task and the router escalation rate, not tokens per request.