Skip to content
Engineering Bible
AI Engineering

AI Engineering on RAG

RAG gives the model your knowledge

  • A model only knows its training data: it has a cutoff and none of your docs.
  • Retrieval-augmented generation (RAG) adds the right text to the prompt.
  • Index once: chunk your documents, embed them, store them in a vector DB.
  • Then for each question, fetch the closest chunks and answer from them.

Embeddings turn meaning into numbers

  • An embedding model turns text into a vector: hundreds to thousands of numbers.
  • Texts with similar meaning get vectors that sit close together.
  • “Reset my password” lands near “I can't log in” with no words in common.

1. Index (once)

  1. Documents
  2. Chunks
  3. Embeddings
  4. Vector DB

2. Answer (every question)

  1. Question
  2. Search vector DB
  3. Chunks + question
  4. LLM writes answer

Vector databases search fast by being nearly exact

  • Comparing a question with millions of vectors one by one is too slow.
  • Approximate nearest neighbor (ANN) indexes, like HNSW, jump to close matches.
  • They trade a sliver of accuracy for huge speed. The best match is usually found.

Chunking makes or breaks it

  • Chunks that are too big blur the meaning. Too small, and they lose context.
  • Split on natural edges like headings and paragraphs, with a little overlap.
  • Store the source with every chunk, so answers can cite it.

Search two ways, then rerank

  • Vector search finds meaning but can miss exact terms like product codes.
  • Keyword search, like BM25, nails exact words but misses synonyms.
  • Hybrid search runs both, and a reranker puts the best results on top.

Most RAG bugs are retrieval bugs

  • If the right chunk isn't found, the model can't use it.
  • Test retrieval alone: for real questions, is the right chunk in the top results?
AI Engineering in five pages2 / 5