RAG: An AI Agent That Answers From Your Documents
Build RAG in n8n: load documents, chunk them, store embeddings in a vector store, and let an AI agent search them before answering.
Video chapters
- 00:00Intro
- 00:40RAG in plain words
- 02:00Two workflows: load, then chat
- 02:56Step 1: Load your documents
- 04:14Step 2: Embeddings, then run it
- 05:30Step 3: The chat agent
- 06:32Step 4: Keep it honest
- 07:33From learning to production
- 08:275 tips for better RAG answers
- 09:22Your agent now reads your documents
Language models only know what they were trained on. RAG (retrieval-augmented generation) fixes that: before answering, the agent searches your documents and answers from what it finds. In this episode we build it in n8n with two small workflows: one to load documents, one to chat.
What you need
- n8n (self-hosted or Cloud)
- Your free Gemini credential
- One or more documents (PDF, text or Google Doc), e.g. an FAQ or price list
RAG in plain words
RAG stands for retrieval augmented generation. It sounds complicated, but the idea is simple, and it has five parts.
First, your documents are split into chunks: small pieces of text, a paragraph or so each.
Second, each chunk is turned into an embedding. That's a long list of numbers that captures what the text means. Texts with similar meaning get similar numbers, even if they use different words.
Third, those chunks and their numbers are saved in a vector store, a database that can search by meaning instead of exact keywords. Here's what one stored chunk looks like: the text, its vector, and where it came from.
Fourth, retrieval. When someone asks a question, the question is turned into an embedding too, and the vector store returns the chunks that are closest in meaning.
And fifth, the model answers using those chunks. So instead of guessing, it reads the relevant parts of your documents first. That's the whole trick.
You might ask: why not just paste the whole document into the prompt? For one short page, you can. But real knowledge bases have hundreds of pages. Sending all of it with every question is slow, expensive, and often too long for the model. RAG sends only the few pieces that matter for each question.
Quick reference
- Chunks: Documents split into small pieces of text
- Embeddings: Each chunk turned into numbers that capture meaning
- Vector store: A database that finds chunks by meaning
- Retrieval: Question → find the closest chunks
- Grounded answer: The model answers using only those chunks
one chunk · stored with its vector
"Teeth whitening costs $180 per
session. Most patients need 2.
Book at least 3 days ahead."
vector: [0.021, -0.113, 0.087,
0.004, ... 768 numbers]
source: price-list.pdf, page 2Two workflows: load, then chat
Our project has two small workflows. The first one loads documents. The second one chats.
The loading workflow starts with a document. I'm using a clinic's price list and FAQ, but it could be your product manual, a policy, or your lecture notes.
A data loader splits it into chunks.
An embeddings model turns each chunk into numbers. We'll use Google's Gemini embeddings, with the same free credential as before.
And everything lands in a vector store. For learning, we'll use n8n's Simple Vector Store, which needs no setup at all.
The second workflow is a chat. A user asks a question,
and our AI agent gets the vector store as a tool. It searches the documents, reads the best matching chunks, and answers. You run the first workflow whenever your documents change, and the second one is what your users talk to.
Quick reference
Document → Data Loader → Embeddings → Vector Store → Chat → AI Agent
Step 1 · Load your documents
Step one: the loading workflow. Start with a manual trigger. Later you can switch to a Google Drive trigger that runs whenever the file changes.
Add a Google Drive node to download your document. If you're self-hosting, you can also read a file straight from the server's disk.
Next, add a Simple Vector Store node, and choose the insert documents operation.
Give it a memory key, like clinic underscore K B. Think of the key as the name of this knowledge base. We'll use the exact same key later in the chat workflow.
The vector store needs two helpers. The first is the document loader. Add the Default Data Loader, and set the type to binary, because we're passing in a file. It reads PDFs, text files and more.
Inside the loader, choose the Recursive Character Text Splitter. n8n's docs recommend it for most use cases, because it tries to split at paragraphs and sentences, rather than in the middle of a word.
Set the chunk size to around a thousand characters, with an overlap of two hundred. Smaller chunks give more precise matches. Larger chunks give more context per match. And the overlap makes sure a sentence cut at the edge of one chunk also appears whole in the next.
Quick reference
- Trigger: Manual (or Google Drive “file updated”)
- Google Drive → Download file (or Read Binary)
- Simple Vector Store → Insert Documents
- Memory key: clinic_kb
- Default Data Loader → type: Binary
- Text splitter: Recursive Character
SPLITTER SETTINGS
Chunk size: 1000 characters Chunk overlap: 200 characters Small chunks = precise matches Large chunks = more context Overlap = no cut sentences
Step 2 · Embeddings, then run it
Step two. The second helper is the embeddings model. Click the embeddings slot on the vector store, and choose Embeddings Google Gemini.
Select the same Gemini credential you've used since episode one. No new account needed.
There's one rule you must never break. Use the same embeddings model to insert documents and to search them. Different models produce different numbers, and the search simply stops working.
Now execute the workflow. The document is downloaded, split, embedded, and stored.
Open the vector store node's output, and you'll see how many chunks were inserted. My two page price list became fourteen chunks.
An important note. The Simple Vector Store keeps everything in n8n's memory. It's perfect for learning, but it's cleared when n8n restarts. Later in this video, I'll show you what to use in production.
One more thing to watch. If you run the insert workflow twice, you can end up with every chunk stored twice, and duplicate results in your searches. When you reload documents, start from a fresh store. The insert operation has an option to clear the store first, so check for it in your version, and turn it on for full reloads.
Quick reference
- Embeddings slot → Embeddings Google Gemini
- Same Gemini credential from Episode 1
- Rule: same embeddings model to insert AND search
- Execute workflow → chunks inserted
VECTOR STORE · after insert
documents: 1 (price-list.pdf) chunks: 14 memory key: clinic_kb
Step 3 · The chat agent
Step three: the chat workflow. This should feel familiar from episode two. Add a chat trigger, and an AI agent.
Give the agent the Gemini chat model, and simple memory, so it remembers the conversation.
Now the new part. In the agent's tool slot, add a Simple Vector Store, and set its mode so the AI agent can use it as a tool. Instead of inserting, it will now retrieve.
Use exactly the same memory key, clinic underscore K B, and attach the same Gemini embeddings model. Same key, same model. That's how the chat workflow finds the documents the first workflow loaded.
Give the tool a clear name, like Clinic Knowledge, and limit it to around four chunks per search. That's usually enough context, without filling the prompt with noise.
And write a good description. As we learned in episode two, the agent reads this description to decide when to use the tool. Be specific about what's in the documents, and when to search them.
Quick reference
- New workflow: Chat Trigger → AI Agent
- Chat model: Gemini · Memory: Simple Memory
- Tool: Simple Vector Store, used as a tool
- Same memory key: clinic_kb + same embeddings
- Name: Clinic Knowledge · Limit: 4 chunks
TOOL DESCRIPTION
Searches the clinic's price list, FAQ and policies. Use it for ANY question about services, prices, timings or bookings.
Step 4 · Keep it honest
Step four: keep it honest. This is what separates a useful RAG bot from a dangerous one.
In the agent's system message, tell it to answer only from the clinic knowledge tool. If the answer isn't there, it should say it doesn't know, and offer to connect the user with a person. And add a direct rule: never guess prices or times.
Now test it. Open the chat, and ask how much teeth whitening costs.
Look at the agent's logs. It calls the clinic knowledge tool, gets back the chunk about whitening, and answers with the exact price from your document.
Now the important test. Ask something that is not in the documents, like whether the clinic offers hair transplants.
A well instructed agent says it doesn't have that information, and offers to connect you with the team. If it invents an answer instead, make the system message stricter, and lower the model's temperature. Always run this test before giving a RAG bot to real users.
Quick reference
- Ask: “How much is whitening?”
- Agent searches → “$180 per session…”
- Ask something NOT in the docs
- Good result: “I don't know, I can connect you”
SYSTEM MESSAGE
You are the Help Desk for Smile Dental. - Answer ONLY from the "Clinic Knowledge" tool. - If the answer is not there, say you don't know, and offer to connect the user with the team. - Never guess prices or times. - Keep answers short and friendly.
From learning to production
Now, what about production? Let's look at where to store vectors for real.
The Simple Vector Store is what we used today. It's in memory, so it's perfect for learning and demos, but it's cleared when n8n restarts.
For a self-hosted setup, Postgres with the pgvector extension is a great choice. You can run it in the same Docker Compose file from episode four, or use a hosted service like Supabase.
Qdrant is a dedicated vector database. It's open source, and also easy to run in Docker next to n8n.
And Pinecone is fully managed. There's nothing to host, you just create an index and add the credential.
The best part is that n8n has nodes for all of these. To move to production, you swap the vector store node in both workflows, and everything else stays exactly the same.
Quick reference
- Simple Vector Store: In memory. Perfect for learning, cleared on restart
- Postgres + pgvector: Self-host next to n8n, or use Supabase
- Qdrant: Dedicated vector database, easy to run in Docker
- Pinecone: Fully managed, nothing to host
- Same workflow: Swap the vector store node, keep everything else
5 tips for better RAG answers
Five tips to get better answers from any RAG system.
One. Clean documents beat clever settings. Clear headings, short sections, and one topic per section retrieve much better than a messy wall of text.
Two. Add metadata, like the source file name and page number. Then you can ask the agent to mention its source, which builds trust.
Three. Reload when documents change. Old chunks mean old answers. If prices change, run the loading workflow again with a fresh knowledge base, so outdated chunks don't linger.
Four. Before you go live, write ten real questions with the answers you expect, and test them all. Repeat this test every time you change the settings.
And five. Tune the chunk size. Try five hundred, a thousand and two thousand characters with your own documents, and keep whatever answers your test questions best.
Quick reference
- Clean documents: Clear headings and short sections retrieve better
- Add metadata: Source file and page, so answers can cite them
- Re-load on change: Old chunks = old answers. Rebuild when docs change
- Test 10 questions: Write real questions + expected answers first
- Tune chunk size: Try 500, 1000, 2000 and compare
Your agent now reads your documents
Let's recap. You learned the five parts of RAG: chunks, embeddings, a vector store, retrieval, and grounded answers. You built a loading workflow that turns a document into searchable chunks, and a chat agent that searches them before it answers, and you made it honest about what it doesn't know.
Combine this with the Telegram bot from episode three, and you have a customer support assistant that answers from a real company's documents. That's one of the most requested AI projects right now. In the next episode, we switch from chat to schedules, and build an AI news digest that runs every morning on its own. The notes for this episode are linked in the description.
Quick reference
Document → Data Loader → Embeddings → Vector Store → Chat → AI Agent
Practice
- Add a second document and ask a question that needs both.
- Change chunk size to 500 and to 2000, and compare answer quality.
- Add the Telegram trigger from Episode 3 so customers can ask the knowledge base on Telegram.
Next episode
Episode 7: scheduled AI workflows, a daily AI news digest that runs on its own.
Key takeaways
- RAG splits documents into chunks, turns them into embeddings and retrieves the closest ones for each question
- Use two workflows: one loads documents into a vector store, one chats
- Simple Vector Store lives in memory and is cleared on restart; use pgvector, Qdrant or Pinecone in production
- Tell the agent to answer only from retrieved text, and rebuild the store when documents change