RAG stands for retrieval-augmented generation. A model doesn't know your documents, so you find the relevant passages and put them in the prompt. It's like an open-book exam: the student's quality matters, but if you hand over the wrong book, nothing else helps.
That gives you the key idea for this round: measure retrieval and answers separately. When an answer is bad, you need to know which part failed. One reported rejection pattern is a candidate who can't state retrieval quality as a number.
- Chunk: A passage cut from a document. Search returns chunks, not whole files.
- Hybrid search: Combining keyword search, which catches exact terms, with vector search, which catches meaning.
- Rerank: A slower, more accurate second pass over the top results.
- Recall@k: Of the passages that matter, the share that reached the top k.
- Faithfulness: Whether the answer is supported by the retrieved text.
Steps:
- Clarify. Users, document size and change rate, permissions, cost of a wrong answer, and latency.
- Indexing path. Parse, chunk, embed, and store with metadata, including who may read each chunk.
- Query path. Rewrite the query if needed, run hybrid search filtered by permission, rerank, then build the prompt.
- Generate with citations. Ask for citations, check them in code, and allow 'I don't know'.
- Measure. A labelled question set: recall@k and MRR for retrieval, faithfulness and correctness for answers.
- Handle change and failure. Re-index on change, track what's stale, and have a fallback when search returns little.
Do you need RAG at all?: One rejection pattern reported is treating retrieval as the default. A small, stable document can simply go in the prompt. A question about live data may need a database query through a tool. Say why you're choosing retrieval.
Permissions go in the search: Filter by access at search time. If you filter after generating, a user's answer has already been shaped by a document they shouldn't have seen. Interviewers like this point.
Symptom to cause
| You see | Check first | Usual fix |
|---|---|---|
| Misses exact terms like error codes | Keyword search | Add hybrid search |
| Finds the right document, wrong passage | Chunk size and rerank | Change the chunking, and add a reranker |
| Right passage, wrong answer | Prompt and context order | Tighten the prompt, and show less text |
| Invented citations | Citation check | Validate citations in code |
| Old answers | Index freshness | Re-index on change, and add dates to metadata |
Code, Splitting documents. Search finds chunks rather than whole documents. Too big blurs the meaning, and too small loses the context.
/** Overlap means a sentence cut at a boundary still appears whole in the next chunk. */
export function chunkWords(text: string, size: number, overlap: number): string[] {
if (overlap >= size) throw new Error("overlap must be smaller than size");
const words = text.split(/\s+/).filter(Boolean);
const out: string[] = [];
for (let i = 0; i < words.length; i += size - overlap) {
out.push(words.slice(i, i + size).join(" "));
if (i + size >= words.length) break;
}
return out;
}
i += size - overlap: Each chunk starts a little before the last one ended.overlap >= size: This would never move forward, so it's refused up front.i + size >= words.length: Stops once the last word is covered, so no tiny leftover chunk appears.
Code, Cosine similarity. The basic measure for comparing two embeddings.
/** Compares direction, not length, so a long document doesn't win by being long. */
export function cosine(a: number[], b: number[]): number {
let dot = 0;
let na = 0;
let nb = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
na += a[i] * a[i];
nb += b[i] * b[i];
}
return na && nb ? dot / Math.sqrt(na * nb) : 0;
}
dot / Math.sqrt(na * nb): Dividing by both lengths leaves only the angle between the vectors.na && nb: An all-zero vector has no direction, so it returns 0 instead of NaN.
Code, Hybrid search with rank fusion. Keyword search finds exact terms like error codes, and vectors find meaning. You usually want both.
/** Keyword and vector scores use different scales, so merge by rank instead of by score. */
export function reciprocalRankFusion(rankings: string[][], k = 60): string[] {
const scores = new Map<string, number>();
for (const ranking of rankings) {
ranking.forEach((id, rank) => scores.set(id, (scores.get(id) ?? 0) + 1 / (k + rank + 1)));
}
return [...scores].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([id]) => id);
}
1 / (k + rank + 1): Uses only position, so the two scoring scales never have to be compared.k = 60: The constant commonly used for this method. It stops first place from dominating.a[0].localeCompare(b[0]): Ties break by id, so the result is repeatable.
Code, Measuring retrieval. Measure this before you look at answers. If the right text never arrives, no prompt can help.
export interface Labelled {
retrieved: string[];
relevant: string[];
}
/** Of the documents that matter, what share reached the top k? Averaged over questions. */
export function recallAtK(rows: Labelled[], k: number): number {
const each = rows.map((r) => r.relevant.filter((id) => r.retrieved.slice(0, k).includes(id)).length / r.relevant.length);
return each.reduce((a, b) => a + b, 0) / rows.length;
}
/** How high does the first useful document sit? 1 means first place. */
export function meanReciprocalRank(rows: Labelled[]): number {
const each = rows.map((r) => {
const at = r.retrieved.findIndex((id) => r.relevant.includes(id));
return at === -1 ? 0 : 1 / (at + 1);
});
return each.reduce((a, b) => a + b, 0) / rows.length;
}
r.retrieved.slice(0, k): Only what actually reaches the prompt counts.r.relevant.length: Recall needs labelled answers, so you need a labelled set.1 / (at + 1): First place scores 1, second 0.5, and a miss 0.Failure, Tuning prompts before measuring retrieval: If the right text never arrives, you're polishing the wrong layer.
Failure, Testing on questions you wrote from the chunks: That flatters recall. Use real user questions if you can.
Failure, Forgetting permissions: An internal search that leaks HR documents is a serious failure.
- Why merge rankings by rank, not by score?
- Keyword and vector scores use different scales, so they can't be compared directly. Rank fusion uses only position.
- Recall@5 is 0.95, yet answers are poor. Where do you look?
- Retrieval looks fine, so look at the prompt, the context order, how much text you're sending, and whether the model is using it. Trace some failures.
- Too large or too small a chunk: what happens?
- Too large and the meaning blurs, and you spend tokens on noise. Too small and you lose the context needed to understand it.
- How do you handle a question the documents don't cover?
- Detect weak retrieval, tell the model to say it doesn't know, and test that behaviour with cases in your eval set.
Sources:
- What applied-AI engineer interviews test (Individual): https://www.techinterview.org/post/3233476824/ai-engineer-interview-rag-agents-evals/