RobotsHelp

n8n AI Agent RAG: Make It Answer From Your Own Docs

Written by

in

You built an agent in n8n. It sounds smart, and then a customer asks about your refund policy and it makes something up. That is the moment RAG is for.

An n8n AI agent with RAG looks up the answer in your own files first, then writes its reply from what it found. RAG stands for retrieval-augmented generation, an approach described in a 2020 research paper that pairs a language model with a searchable store of documents. Below is the setup, step by step, with no code.

No password needed – your reset opens instantly.

No password. Just your email, and you’re in.

What RAG does for an n8n agent

A plain agent only knows what the model learned in training, plus what you type into the prompt. It has never seen your price list, your onboarding doc or last month’s meeting notes.

With RAG, your files are cut into small pieces and stored in a vector store, which is a database that finds pieces by meaning instead of exact words. When a question comes in, the agent searches that store, pulls the few best pieces and answers from them. You can tell it to say “I don’t know” when nothing matches, which stops most of the invented answers.

What you need before you start

  • An n8n account, cloud or self-hosted.
  • An API key for a model provider such as OpenAI, which you use for both the chat model and the embeddings.
  • A few files to test with. Start with one document you know well, so you can judge the answers.

Step 1: Load and split your documents

Create one workflow just for loading. Add a trigger (a manual one is fine), then a node that reads your file, for example a PDF from Google Drive. Connect it to a vector store node set to insert documents.

The vector store node takes two sub-nodes. The first is a Default Data Loader, which turns the file into text. The second is a text splitter, which cuts that text into chunks. A recursive character splitter works well to start. Chunks of a few hundred words with a small overlap are a sensible first try, and you can adjust after testing.

Step 2: Add embeddings and a vector store

The same vector store node needs an embeddings sub-node. Embeddings turn each chunk into numbers that capture its meaning. Pick the embeddings model from your provider and use the same one later when you search, otherwise the results will be poor.

For the store itself, n8n has a simple in-memory option that is good for learning. It forgets everything when n8n restarts, so for real use pick a persistent one such as Supabase, Pinecone or Qdrant. The n8n AI documentation lists the supported stores and how each one connects.

Run the loading workflow once. Your chunks are now stored.

Step 3: Connect the agent to your knowledge

Now build the answering workflow. Start with a chat trigger and an AI Agent node. Give the agent a chat model, then add a vector store as a tool. In the tool settings, point it at the same store and the same embeddings model, and write a clear description such as “Search the company handbook for policy questions.” The agent reads that description to decide when to use the tool.

If you have not built an agent in n8n before, the n8n AI agent tutorial and the AI agent workflow guide on this site cover the basics first.

Step 4: Write a system prompt that keeps it honest

This short prompt does a lot of the work. Paste it into the agent’s system message and edit the details:

You answer questions about our company using the handbook tool.
Always search the tool before answering.
If the tool returns nothing relevant, say you don't know and suggest contacting support.
Keep answers short and mention which document the answer came from.

Asking for the source document makes wrong answers easy to spot, because you can open the file and check.

Step 5: Test it with questions you know the answer to

Ask five questions whose answers you can find in the file yourself. Then ask two that the file does not cover. A good setup answers the first five correctly and admits it does not know the last two.

If it gets things wrong, the cause is usually one of these:

  • Chunks too big or too small. Big chunks bury the answer, tiny ones lose context. Change the size and reload.
  • Mixed embeddings models. Loading with one and searching with another breaks retrieval.
  • Messy source files. Scanned PDFs without text and tables that turn into jumbled lines give the agent nothing to work with. Clean the source first.
  • Vague tool description. If the agent never calls the tool, rewrite the description so it says exactly what is inside.

Keeping it up to date

Documents change. Add a trigger to the loading workflow, for example a new or updated file in a folder, so fresh content is stored without you remembering to do it. Remove or replace old chunks when a document is rewritten, or the agent may quote the outdated version.

Where to use it this week

  • A support assistant that answers from your help articles.
  • An internal bot that finds answers in your handbook or process docs.
  • A research helper over a folder of reports. If this is your main goal, see how to build an AI knowledge base.

Start small: one document, one agent, five test questions. Once that works, adding more files is the easy part.

Prefer a no-code route first? The guide to build an AI chatbot for business covers a simpler first version before you wire up RAG in n8n.

Want to keep going?

Launchpad is free and walks you through practical AI and automation for your real work.

No password needed – your reset opens instantly.

No password. Just your email, and you’re in.

Prefer a lighter first project? This n8n AI agent for marketing builds a weekly digest without any code.

🇳🇱 Deze pagina in het Nederlands  ·  🇩🇪 Diese Seite auf Deutsch