RAG system design: chunking, embeddings, vector DBs, retrieval quality
How retrieval-augmented generation works end to end, where quality breaks, and how to measure and improve it.
On this page
Retrieval-augmented generation (RAG) is the most common pattern for AI features that answer from a company's own information: help centres, policies, documents and knowledge bases. It's conceptually simple (find relevant text, give it to the model), but most quality problems in RAG products are retrieval problems, not model problems.
Why interviewers ask this
"How would you design an AI assistant that answers questions from company documents?" is a staple AI system design question. Interviewers expect you to describe the ingestion and query pipelines, name the key design choices (chunking, embeddings, ranking, permissions), and explain how you'd measure retrieval and answer quality separately.
The framework
RAG has two pipelines: one that prepares documents ahead of time, and one that runs on every question.
flowchart LR
subgraph Ingestion["Ingestion, runs ahead of time"]
direction TB
D["Documents"] --> CH["Clean and chunk"]
CH --> EM["Create embeddings"]
EM --> V[("Vector database<br/>plus keyword index")]
end
subgraph Query["Query, runs per question"]
direction TB
Q["User question"] --> QE["Embed question"]
QE --> S["Search the index<br/>for top matches"]
S --> RR["Re-rank and filter<br/>by permissions"]
RR --> P["Prompt with top chunks"]
P --> M["LLM answer with citations"]
end
Ingestion -->|index searched at query time| QueryIngestion
- Collect and clean documents: remove boilerplate, keep titles, track source, date and permissions.
- Chunking: split documents into passages, often a few hundred words, ideally along natural boundaries (sections, headings) with a little overlap. Too big and you waste context and dilute relevance; too small and chunks lose meaning.
- Embeddings: convert each chunk into a vector, a list of numbers that captures meaning, so "reset my password" and "can't log in" end up close together.
- Store vectors in a vector database, usually alongside a keyword index.
Query
- Embed the question and find the most similar chunks (semantic search).
- Hybrid search: combine with keyword search, which is better for exact terms like product names, error codes and IDs.
- Re-rank the candidates with a more precise model, and filter by permissions so users only see documents they're allowed to.
- Generate: give the top chunks to the model with instructions to answer only from them and cite sources, or say it doesn't know.
Where quality breaks
- The answer isn't in the documents (a content gap, not an AI problem).
- Retrieval misses the right chunk (bad chunking, vocabulary mismatch).
- The right chunk is retrieved but buried among noise.
- Outdated or conflicting documents.
- The model ignores the context or over-generalises.
Measuring quality
- Retrieval quality: for a test set of questions with known source documents, is the right chunk in the top 5? (Often called recall@k.)
- Answer quality: correctness, groundedness (supported by the retrieved text), and citation accuracy.
- Production signals: thumbs down, "I don't know" rate, escalations, and unanswered-question themes, which reveal content gaps.
Worked example
Question: "How would you design an AI assistant that answers employees' questions from company documents?"
Sample answer:
"Users and scope: employees asking about policies, processes and internal tools. Success means fast, correct answers with sources, and never exposing documents someone shouldn't see.
Ingestion: connect the wiki, HR policies and IT docs. Chunk by section headings, keep titles and 'last updated' dates, and store document permissions with each chunk. Re-index when documents change.
Query: hybrid search (semantic plus keyword, since employees search for tool names and acronyms), then re-ranking, then permission filtering based on the employee's identity. That's a hard requirement. Top five chunks go to the model with instructions to answer only from them, cite sources, and say 'I couldn't find this' otherwise.
UX: answers show source links and dates; outdated sources are flagged; a 'Was this helpful?' button and a 'Ask a human' fallback.
Quality: an eval set of 200 real employee questions with the correct source document. I'd measure retrieval recall@5 separately from answer correctness, so we know which part to fix. Unanswered questions get reported weekly to document owners as content gaps.
Risks: stale or conflicting documents, permission leaks, and over-confident answers. Mitigations: freshness signals, permission tests, and grounded-only answers."
Common mistakes
- Blaming the model for what are really retrieval or content problems.
- Ignoring permissions, so the assistant leaks confidential documents.
- Semantic search only, missing exact names and codes.
- No separate retrieval metrics.
- Forgetting freshness: documents change, and the index must too.
Interviewer tips
What "great" sounds like:
- "There are two pipelines: ingestion ahead of time, and retrieval per question."
- "I'd measure retrieval and generation separately, to know what to fix."
- "Hybrid search, because users search for exact product names and codes."
- "Permission filtering happens before anything reaches the model."
- "Unanswered questions are a content-gap report for document owners."
Practice questions
Answer one in a mock interview and get graded instantly.
1.How would you design an AI assistant that answers questions from company documents?
Practice this →2.Our RAG assistant often says "I don't know" even when the answer exists in our docs. How would you investigate?
Practice this →3.Explain embeddings and vector databases to a non-technical stakeholder.
Practice this →
Finished reading?
