Retrieval-Augmented Generation (RAG) and Vector Databases / How RAG Works

How RAG works

Lesson 4 of 43. Open to read. No account needed.

The Problem RAG Solves

A language model answers from patterns in its training data. That leaves three gaps for an organisation's own questions:

  • Private knowledge. Alder Lane Makerspace's laser rules were never in any model's training data, so a model can only guess at them.
  • Freshness. Training stops at a date; a refund policy changed last month is unknown to the model.
  • Evidence. Even when a model is right, it cannot show where the answer came from, so you cannot check it.

Retrieval-augmented generation (RAG) closes these gaps by giving the model the relevant text at question time and asking it to answer from that text, citing it. The name comes from a 2020 research paper (S44) that combined a retriever with a generator; today the term covers any system that retrieves passages and generates an answer from them. In the previous lesson you ran one: it found three documents, put them in the prompt with their IDs, and the answer cited them.

Two Pipelines Around One Store

Two horizontal pipelines meeting at a vector store in the middle. The upper, indexing pipeline runs ahead of time: load documents, clean them, split them into chunks, embed each chunk, store vectors with text and metadata. The lower, query pipeline runs for every question: embed the question, retrieve the nearest chunks from the store, optionally rerank them, build a prompt with labelled sources, generate an answer and check its citations. Warning markers on eight of the steps name a typical failure: outdated or missing document, boilerplate left in, a chunk boundary through an answer, a mismatched embedding model, a filter that is missing, the right chunk ranked too low, sources the model ignores, and a citation that does not support its sentence.

Indexing prepares the store once and again whenever documents change; querying uses it for every question.

Read the diagram as two flows that share the vector store.

Indexing, done ahead of time:

  1. Load each document and keep where it came from (its ID, title, date and who may see it).
  2. Clean it: remove navigation menus, page furniture and markup, keep the headings.
  3. Chunk it into passages small enough to embed and specific enough to answer one kind of question.
  4. Embed each chunk: turn its text into a vector.
  5. Store the vector with the chunk's text and metadata in a vector database.

Querying, done for every question:

  1. Embed the question with the same model used for the chunks.
  2. Retrieve the chunks whose vectors are nearest to the question's, applying any filters (for example, only documents this user may see).
  3. Rerank (optional): rescore the candidates with a slower, more accurate model.
  4. Build the prompt: instructions, then the chosen chunks labelled with their IDs, then the question.
  5. Generate the answer with the language model.
  6. Check the answer: are the citations real, retrieved sources? Did an unanswerable question get the agreed refusal?

In the demonstration, indexing and querying happened in one script and the "store" was a NumPy array in memory. Later modules replace each step with a real component and measure it.

Why Grounding Helps and Where It Fails

Giving the model the right passages makes a correct answer much more likely, and citations let you check it. But every stage can still fail, and the failure often looks like a confident answer:

StageWhat goes wrongWhat it looks like
DocumentsThe answer is not in the collection, or only an outdated version isA confident answer from the old policy, or a guess
CleaningMenus and boilerplate are indexed with the contentIrrelevant chunks rank high because they share common words
ChunkingA chunk boundary cuts an answer in half, or a chunk exceeds the model's input limitThe right document is found but the deciding sentence is missing
EmbeddingQuestion and chunks embedded by different modelsNonsense rankings with no error message
RetrievalThe right chunk ranks below the cut-off, or a permission filter is missingThe answer comes from the wrong document, or from one the user should not see
PromptToo many sources, or the key one buried in the middle of a long promptThe model ignores the passage that answers the question (S93)
GenerationThe model mixes source facts with its own guessesFluent sentences that no source supports
CitationsA citation is attached to the wrong sentenceThe answer looks checked but is not

This is why the course measures retrieval separately from generation: when an answer is wrong you need to know whether the right text never reached the model or the model misused it.

Words the Course Uses

TermMeaning in this course
DocumentOne source file, such as a policy page
ChunkA passage of a document that is embedded and retrieved as one unit
Embedding (vector)A fixed-length list of numbers representing a text's meaning
Vector store, vector databaseStores vectors with text and metadata and finds the nearest ones
RetrieverThe code that turns a question into a ranked list of chunks
Grounded answerAn answer built only from the supplied sources, with citations
Refusal sentenceThe exact sentence the model must give when the sources do not answer

Check Your Understanding

  1. Name the two pipelines and say which one runs for every question.
  2. For each symptom, name the stage that most likely caused it:
    1. An answer quotes the refund period from last year's policy.
    2. Rankings became nonsense after a library upgrade changed the default embedding model for questions only.
    3. A member gets an answer quoting the staff alarm procedure.
    4. The right document is retrieved, but the answer says the sources do not mention the fire procedure, which is in its last paragraph.
    5. An answer cites [refunds] for a sentence about opening hours.
    6. Navigation text such as "Home | Contact | Privacy" appears in retrieved chunks.

Answers

  1. Indexing (load, clean, chunk, embed, store) and querying (embed, retrieve, rerank, prompt, generate, check). Querying runs for every question.
  2. (1) Documents: an outdated version is in the collection and nothing prefers the current one. (2) Embedding: questions and chunks now come from different models. (3) Retrieval: no permission filter was applied. (4) Chunking: the retrieved chunk ended before the fire paragraph, which sits in a chunk that was not retrieved. Embedding truncation alone would not cause this, because the prompt carries the chunk's full text; truncation makes text harder to find, as the embeddings module shows. (5) Citations: the model attached a citation to a sentence it does not support. (6) Cleaning: page furniture was not removed.

38 more lessons in the full course

Course access opens every lesson and adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 199 plus 18% GST. 90 days of online access. No auto-renewal.

Get the full course

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy