RAG System (Retrieval-Augmented Generation)

Question Answering over a Company's Documents

Design a system that answers questions from a company's own documents: it finds the right passages with keyword and vector search, hands them to a language model, and returns an answer with citations that respects who may read what.

Overview

A language model knows only what was in its training data. It does not know your company's refund policy, last week's incident report or the contract signed yesterday, and when asked, it may produce a confident guess. Retrieval-augmented generation (RAG) fixes this by looking up relevant passages at question time and giving them to the model with the question, so the answer is based on your documents and can cite them.

The name comes from Lewis et al. (2020), who paired a sequence-to-sequence model (the parametric memory) with a dense vector index of Wikipedia searched by a neural retriever (the non-parametric memory). Production systems today usually keep the model fixed and put the engineering into the retrieval side: how documents are split, how passages are found, who may see them, and how quality is measured.

In an interview, the model call is one box. The design is everything around it: ingestion that keeps the index fresh, retrieval that finds the right passage when the words differ and when they match exactly, permissions that never leak a document, and an evaluation loop that tells you whether a change helped.

Questions to ask first

Before drawing anything, pin down the corpus, the users and the targets. Each question comes with the answer this design assumes.

  • What documents, and how many? Wiki pages, PDFs and tickets: 10 million documents across many companies (tenants).
  • Who asks, and how often? Employees, 2 million questions a day.
  • Do permissions matter? Yes. A user must never see a passage from a document they cannot open.
  • How fresh? An edited document is answerable within 10 minutes (a target we chose).
  • How fast? First words within about 1.5 seconds, streamed (a target we chose).
  • Which model? Any hosted model behind an interface; that is not our choice here.

The design at a glance

A user's question goes to the query service, which searches two indexes at once: a BM25 keyword index and an approximate nearest-neighbor (ANN) vector index. It fuses the two result lists, has a cross-encoder rerank the best candidates, sends only those chunks to the language model, and logs a trace of the answer for evaluation. Both indexes are filled offline: source systems such as the wiki, drives and tickets send changes to ingestion, which chunks each document, writes the chunks to both indexes, and records the document and its permissions in the documents database. The Visual Explanation below draws this, and the sections that follow open up each part.

What you will be able to do

  • Size a RAG system: chunks, vector storage, question rate and token volume.
  • Chunk documents along their structure and keep each chunk understandable on its own.
  • Combine BM25 and vector search with reciprocal rank fusion, then rerank.
  • Enforce tenant and document permissions inside retrieval, and keep the index fresh.
  • Measure retrieval and answers separately, with a golden set and production traces.

Key concepts

  • Chunking
  • Embeddings
  • BM25
  • Hybrid retrieval
  • Reciprocal rank fusion
  • Reranking
  • Grounded generation
  • Citations
  • Permission filtering
  • Retrieval evaluation

Functional requirements

  • Connect sources and ingest documents (PDF, HTML, Markdown, office files) with their permissions
  • Re-index a document when it changes or is deleted
  • Answer a question with passages from documents the user may read, citing each one
  • Say so when the documents do not contain the answer
  • Stream the answer as it is generated
  • Collect feedback (helpful or not) on each answer

Non-functional requirements

  • Never return or use a passage from a document the user cannot open
  • First words within about 1.5 s at p95; full answers in a few seconds (targets we chose)
  • Edits and deletions reflected within 10 minutes
  • Every answer traceable: question, retrieved chunks, prompt version, model and answer are logged
  • Quality changes (chunking, retrieval, prompt, model) are measured before they ship

Interview questions

  • Why not just put all the documents in the model's context?
  • How do you choose a chunking strategy?
  • Why hybrid search, and how do you combine the results?
  • How do you stop the system from leaking documents between users?
  • How do you evaluate a RAG system?
  • What is HyDE, and when would you use it?
  • How do you keep answers fresh when documents change?
  • Where does the latency go, and how do you cut it?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio