Skip to content
AIMODELRACE

Module 2 of 6 · Expert · 10 min read

Retrieval-augmented generation

Grounding models in your data: chunking, embeddings, hybrid search, reranking and evaluation.

Key takeaways
  • 1RAG retrieves relevant passages and puts them in the prompt.
  • 2Most RAG failures are retrieval failures, not generation failures.
  • 3Hybrid search plus reranking is a strong default.

01The pipeline

A typical RAG system has two halves: indexing and querying.

  • Index: split documents into chunks, embed each chunk, store vectors and text.
  • Query: embed the question, retrieve similar chunks (plus keyword matches), rerank, and pass the top results to the model.
  • Generate: the model answers using the retrieved passages, ideally citing them.

02Where it breaks

If the right passage is not retrieved, no model can answer correctly. Common causes: chunks that split key information, embeddings that miss exact terms (product codes, names), and queries phrased differently from documents.

Fixes include contextual chunk headers, hybrid BM25 plus vector search, query rewriting and a reranker model.

Tip
Measure retrieval recall separately from answer quality. It tells you which half to fix.

03RAG vs long context

With million-token windows, you can sometimes skip retrieval and paste whole documents. That is simpler and often more accurate for a few documents, but slower and costlier at scale.

Agentic search, where the model issues its own search queries iteratively, increasingly replaces fixed retrieval pipelines for complex questions.

04Production checklist and metrics

Treat retrieval and generation as two separately testable systems. Log the retrieved chunk IDs, similarity scores and final citations on every production query so you can trace bad answers back to a missing chunk or an unfaithful synthesis.

  • Chunking: aim for 300–600 tokens with 10–15% overlap, and prepend document title + section breadcrumbs to every chunk before embedding.
  • Hybrid scoring: merge dense vector search and sparse BM25 via Reciprocal Rank Fusion (RRF), then pass the top 25–50 candidates through a cross-encoder reranker to keep the top 5–8.
  • Core metrics: track Recall@k and MRR on retrieval separately from groundedness (every claim backed by a cited passage) and refusal accuracy (saying 'not in the provided sources' when nothing matches).
Example
Answer using ONLY the passages inside <sources>. Cite each factual claim with [S#]. If the passages do not contain the answer, reply: "Not covered in the provided sources." <sources> [S1] {{chunk_1}} [S2] {{chunk_2}} </sources> Question: {{user_question}}

Sources

Written by AIModelRace. These official pages and papers back up this lesson's main points, so you can check them yourself.

All Learn sources →

Finished reading?

Mark it complete to track your progress, then test yourself with the LLM Internals quiz.