RAGEngineering Hub
Chapter 1 · concept 1 of 4 · show all
  1. 1What RAG is, and why
    1. 1.1Why models need outside knowledge, and what hallucination means
    2. 1.2What RAG is: anatomy of a request
    3. 1.3RAG vs fine tuning vs long context and prompt caching
    4. 1.4Use cases and the quality, latency and cost triangle
    5. Chapter overview
  2. 2Tokens, cost and your first RAG
    1. 2.1Tokens and context windows for RAG
    2. 2.2The RAG lifecycle and your first RAG in miniature
    3. Chapter overview
  3. 3Getting documents in
    1. 3.1Content sources and file formats
    2. 3.2Parsing, layout and OCR
    3. 3.3Tables, images and mixed content
    4. 3.4Cleaning, normalization and quality gates
    5. 3.5Metadata, document identity and versioning
    6. 3.6Updates, deletes, reindexing and ingestion pipelines
    7. Chapter overview
  4. 4Chunking
    1. 4.1Why we chunk, and the main chunking strategies
    2. 4.2Structure aware chunking and tables
    3. 4.3Chunk size, overlap and token based sizing
    4. 4.4Contextual chunking and late chunking
    5. 4.5Parent child and multi representation indexing
    6. 4.6Chunk metadata, provenance and the chunking debugging playbook
    7. Chapter overview
  5. 5Embeddings
    1. 5.1Embeddings and similarity from first principles
    2. 5.2What embeddings cannot do
    3. 5.3Choosing an embedding model: size, cost, language
    4. 5.4Embedding model migration and index rebuilds
    5. Chapter overview
  6. 6Vector stores and scores
    1. 6.1Vector databases, ANN indexes and choosing a store
    2. 6.2Score calibration and thresholds
    3. 6.3Late interaction and multivector retrieval
    4. Chapter overview
  7. 7Finding the right chunks
    1. 7.1Query understanding, rewriting and expansion
    2. 7.2Dense retrieval and keyword search with BM25
    3. 7.3Hybrid retrieval and rank fusion
    4. 7.4topK, no good match, and metadata filtering
    5. 7.5Routing, diversity, deduplication and caching
    6. 7.6Reranking: two stage, cross encoder and LLM rerankers
    7. Chapter overview
  8. 8When retrieval fails
    1. 8.1Retrieval failure analysis playbook
    2. Chapter overview
  9. 9Building the answer
    1. 9.1Context selection, packing and citations
    2. 9.2Grounding prompts and abstention
    3. 9.3Lost in the middle and context management
    4. 9.4Conflicts, staleness and freshness rules
    5. 9.5Hallucination checks and answer verification
    6. 9.6Conversational RAG, answer UX and RAG APIs
    7. Chapter overview
  10. 10Measuring
    1. 10.1Why measure, and what to measure
    2. 10.2Building a golden dataset
    3. 10.3Retrieval metrics: precision, recall, MRR, nDCG
    4. 10.4Faithfulness and answer correctness
    5. 10.5LLM as judge, and judging the judge
    6. 10.6Offline and online evals, regression tests and slices
    7. Chapter overview
  11. 11Safety and operations
    1. 11.1Prompt injection and poisoned documents
    2. 11.2Access control, privacy and governance
    3. 11.3Index lifecycle operations
    4. 11.4Observability and tracing
    5. 11.5Latency, cost, caching and model routing
    6. 11.6Reliability, degraded modes and launch readiness
    7. Chapter overview
  12. 12Scale and advanced RAG
    1. 12.1Scaling, multi tenancy and enterprise architecture
    2. 12.2Choosing an architecture: naive, advanced, agentic, graph
    3. 12.3Agentic RAG and RAG as an MCP tool
    4. 12.4GraphRAG, structured data and multimodal RAG
    5. Chapter overview

Why models need outside knowledge, and what hallucination means

What a language model knows and does not know, why it answers confidently anyway, and why giving it your documents is the fix.

A real question

Why does the AI give a confident answer about our own product, and get it wrong?

It has never seen your documents. When a model does not know something, it does not say so. It writes the answer that sounds most likely.

The problem

Why this matters

A big, modern model knows everything, including our prices and policies.
The belief

A big, modern model knows everything, including our prices and policies.

It knows public text up to a cutoff date. Your private, new or changed facts were never in it, so it guesses.
What really happens

It knows public text up to a cutoff date. Your private, new or changed facts were never in it, so it guesses.

Why not the simpler way

The simple way is to ask a strong model directly. It answers quickly and sounds sure. For public, stable facts that often works. For your company's tariffs, policies and manuals it cannot work: those facts were never in its training data, or they changed after the model was trained. The model does not stop and say so. It writes the most plausible answer it can.

The idea

The idea in plain words

A language model learned patterns from a large amount of text, up to a cutoff date. It does not look anything up. When it lacks a fact, it still predicts fluent text, and that text can be wrong. A fluent answer that the facts do not support is called a hallucination.

Think of a well read new colleague on day one. They write well and know a lot in general, but not your price list. If you hand them the price list, they answer correctly and can point to the line. If you do not, a polite guess is the best they can do.

How it works

Step by step

  1. A model is trained on a huge amount of public text, up to a date. After that, what it knows is fixed.
    1

    It learned from public text, once

    A model is trained on a huge amount of public text, up to a date. After that, what it knows is fixed.

  2. Your tariff sheet was never in that text. The model still answers, with a price that only sounds right.
    2

    Ask about your company, and it guesses

    Your tariff sheet was never in that text. The model still answers, with a price that only sounds right.

  3. Put the real tariff sheet in the prompt. Now it answers from the page and can point to it.
    3

    Hand it the page, and it reads

    Put the real tariff sheet in the prompt. Now it answers from the page and can point to it.

In detail

A model's knowledge lives in its weights, fixed at training time. Three kinds of facts are missing: private facts (your documents), recent facts (anything after the cutoff) and changing facts (the tariff that was replaced last month). Fine tuning can teach style and some facts, but it is slow to update and gives no source to cite.

Retrieval augmented generation takes the other route: find the relevant passages at question time and put them in the prompt. The model then answers from text it can see, and the answer can cite where it came from. When nothing relevant is found, the right behaviour is to say so.

The code

Try it in Python

# Ask the same question with and without your documents (needs an API key).
import anthropic
client = anthropic.Anthropic()

question = "What does error E4012 mean on the Polder FiberBox?"
source = "| E4012 | Mesh pairing failed | Mesh extender on old firmware |"

for prompt in (question, f"Answer only from this source:\n{source}\n\nQuestion: {question}"):
    msg = client.messages.create(model="claude-haiku-5-5", max_tokens=300,
                                 messages=[{"role": "user", "content": prompt}])
    # The reply can start with a thinking block, so print only the text blocks.
    print("".join(b.text for b in msg.content if b.type == "text"), "\n---")

See it

What we measured

Measured · golden questions by type

The Polder golden set, version 1. Every fact in the corpus is fictional, so no model can know it from training.

Questions with a known answer passagebaseline47
Should not be answered: not covered 2, restricted to staff 2, prompt injection 1475

Not measured yet. Planned: ask the 47 answerable questions with and without retrieved context, and count correct answers and invented answers. Needs a model call per question; planned with the precomputed answers.

Try it on a real example

A customer asks: “What does PT-MOB-25GB cost per month today?”

Ask

PT-MOB-25GB is a plan of Polder Telecom, a company invented for this course. It exists in no public text, so no model was ever trained on it.

Whatever price the model writes is a guess. It will not warn you; the sentence will look just as confident as a right answer.

No source, no way to check, and nothing that stops a confident wrong number.

Choosing

Use it when, avoid it when

Use it when
  • Your facts are private, recent or change often
  • Answers must cite a source someone can check
Avoid it when
  • General knowledge questions a model already answers well
  • Tasks that need a new skill or style rather than new facts: consider fine tuning

What it costs · Retrieval adds an index to build and keep current, and more input tokens per call. In return you can update facts by updating documents, not by retraining.

In production

Treat "I don't know based on these documents" as a correct answer and test for it. Include questions in your test set that the documents do not cover, and count how often the system abstains instead of inventing. Ask for citations in the answer and check that the cited passage actually supports it. A hallucination is easiest to catch when every claim must point to a source.

Watch out

Common mistakes

  • Bigger models do not hallucinate. They know more, but they still cannot know facts that were never in their data.
  • Hallucinations are rare bugs. They are the normal behaviour of a model asked about something it does not know.
  • Telling the model "do not make things up" is enough. It helps, but only a source in the prompt gives it something true to say.

Check yourself

Remember this

Remember these

  • A model only knows its training text, up to a cutoff date.
  • Private, new and changed facts are missing, and the model guesses instead of saying so.
  • A fluent answer that the facts do not support is called a hallucination.
  • Giving the model the right document, with a citation, is the fix.

Say it in an interview

  1. 01A model knows only its training data up to a cutoff, and it never looks anything up.
  2. 02Private, recent and changing facts are missing, and the model guesses fluently instead.
  3. 03A hallucination is a fluent answer the facts do not support.
  4. 04RAG puts the relevant passages in the prompt so answers come from visible text with citations.
  5. 05I test refusals too: 5 of my 52 golden questions should not be answered.

Quick quiz

Three questions

Question 1

Q1

A customer asks about Polder Telecom's 2026 tariff. Why can't a general model answer reliably on its own?

Question 2

Q2

What is a hallucination?

Question 3

Q3

What should a well built assistant do when its documents do not cover a question?

Saved on this device. No account needed.