Track
Chunk · Embed · Rerank · Ground
An LLM knows what it was trained on — and nothing else. Ask it about recent events, proprietary documents, or your own codebase, and it will either guess wrong or say it doesn't know. Retrieval-Augmented Generation (RAG) solves this by giving the LLM access to external data at query time.
The idea is simple: when a question comes in, search a knowledge base for relevant documents, stuff them into the LLM's context window, and let the LLM answer from that material.
RAG follows a series of stages: Query → Retrieve → Rerank → Ground → Answer. The user's question is first optionally rewritten. Then we search for candidate chunks using keyword or vector search. A reranker sharpens precision. The top chunks are formatted with citations, and the LLM produces an answer from the grounded context.
Initial retrieval returns candidates ranked by similarity, but top results aren't always most relevant. A reranker applies a more accurate model to re-score top-K candidates, looking at the full query-chunk pair together.
Traditional RAG is one-shot: query → retrieve → generate. Agentic RAG wraps this in a feedback loop: rewrite, retrieve, inspect, retry, rerank, verify, then answer. The agent can catch bad retrievals and retry with better queries.
Use agentic RAG when precision matters (medical, legal, financial) and you can afford the latency. In interviews, expect the follow-up: 'When would you add an agent loop around retrieval?'
Wrong chunk: answer cites irrelevant text. Fix: rerank and relevance threshold. Too many chunks: context overflows. Fix: cap chunks. Missing citation: unsupported claim. Fix: prompt to only answer from context. Stale source: outdated document. Fix: timestamp filter. Hallucination despite retrieval: answer contradicts provided sources. Fix: strengthen system prompt and validate citations.
Rewrite when query is vague. Use vector search for semantic matching. Use keyword search for proper nouns and exact phrases. Rerank for precision-critical apps. Say 'not enough evidence' when top retrieval scores fall below a threshold.
Concrete Example
def chunk_text(text, chunk_size=500, overlap=50):
words = text.split()
chunks = []
start = 0
while start < len(words):
end = start + chunk_size
chunk = words[start:end]
chunks.append(" ".join(chunk))
start = end - overlap
return chunks
def rerank(query, candidates, reranker, top_n):
scored = [(reranker(query, c["text"]), c)
for c in candidates]
scored.sort(key=lambda x: x[0], reverse=True)
return [c for _, c in scored[:top_n]]Chunking splits documents into overlapping pieces so no information falls at boundaries. Reranking scores each candidate with a cross-encoder, sorts by relevance, and returns the top N.
Split documents into overlapping pieces that fit the LLM's context window.
Convert chunks and queries to vectors, find nearest neighbors by similarity.
Apply a second-stage relevance model to sharpen precision at the top of results.
Reformulate vague queries for better retrieval recall.
Include retrieved chunks with source citations for attributable answers.
Wrap retrieval in an agent loop that rewrites, inspects, retries, reranks, and verifies.
Anticipate wrong chunks, missing citations, stale sources with explicit fallbacks.
Interview revision
7 problems. Sign in to start solving.
Sign in to open a workspace and solve these problems.