Vector stores and RAG

n8n · Advanced · 13 min

Agents only know their training data; RAG grounds answers in your current documents.

Ask a model about your refund policy and it will guess, because your policy isn't in its training data. RAG (retrieval-augmented generation) fixes this: before answering, the system looks up the relevant passages in your documents and gives them to the model.

The lookup uses embeddings: lists of numbers that capture what a piece of text means, so texts with similar meaning get similar numbers. A vector store is a database that stores these numbers and finds the closest matches to a question, even when the wording differs.

Step 1: insert your documents (a separate workflow you run when documents change):

  • Fetch the files (Google Drive, a website, a database).
  • A vector store node (for example Simple Vector Store for testing, or Pinecone, Qdrant, Supabase or Postgres PGVector for real use) in Insert Documents mode.
  • Plug in an Embeddings sub-node (the model that turns text into numbers).
  • Plug in a Default Data Loader with a text splitter that cuts documents into chunks (small passages). The Recursive Character Text Splitter is a good default. Add metadata such as source or date to each chunk.

Step 2: query. Attach the same vector store to your AI Agent as a tool, with a description such as "Search the company handbook". Set how many chunks to return. You must use the same embeddings model as for insertion, otherwise searches return nonsense.

Keep chunks focused, return a handful rather than dozens (they all cost tokens), and tell the agent in its system message to say "I don't know" when the search finds nothing relevant.

Simple Vector Store lives in memory and is lost on restart: use a real database in production.

Remember

  • RAG = retrieve relevant chunks, then let the model answer from them
  • Insert: loader + splitter + embeddings into a vector store
  • Query with the same embeddings model; attach the store as an agent tool

Watch out for

  • Inserting with one embeddings model and querying with another, which makes search results meaningless.

Check yourself

  1. Why does the query side need the same embeddings model as the insert side?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.n8n.io/build/integrate-ai/understand-ai-components/retrieve-relevant-context