LangChain blueprint · Corporate knowledge
Retrieval-augmented generation (RAG)
Index your documents once, then answer each question from the chunks that match it.
- Steps
- 7
- Subjective questions
- 32
- Objective questions
- 32
The problem and the outcome
Problem: Models do not know your private documents and invent answers about them.
Outcome: Answers grounded in your own documents, with the sources cited.
Architecture
How it works, step by step
- Document loaders read your sources into Document objects.
- A text splitter cuts them into overlapping chunks small enough to embed.
- Each chunk is embedded and saved in a vector store, once, ahead of time.
- At question time the retriever embeds the query and fetches the most similar chunks.
- A prompt template places those chunks next to the question.
- The chat model answers from that context instead of from memory alone.
- An output parser returns structured text, with the sources it used.
Tech stack
- LangChain document loaders & text splitters
- Embedding model (OpenAI, Voyage, BGE)
- Vector store (pgvector, Pinecone, Chroma, Qdrant)
- Chat model
- LangSmith for tracing and evals
Key design decisions
- Chunk by document structure (headings, paragraphs) with some overlap, so each chunk is a self-contained idea.
- Hybrid search (vectors plus keywords such as BM25) with a reranker, because pure vectors miss exact terms like product codes.
- Answer only from the retrieved context, cite the sources, and say 'I don't know' when the context does not cover it.
Common mistakes
- Chunks that are too big (diluted matches) or too small (missing context).
- Never re-indexing, so answers quote outdated policies.
- Judging quality by eye instead of measuring retrieval and answer accuracy on a test set.
What to measure
- Retrieval hit rate / recall@k
- Answer faithfulness to the sources
- Share of 'don't know' answers
- Latency and cost per question
Most-asked interview questions
Subjective questions 32
1What is RAG and why use it instead of fine-tuning?Asked almost every time
Retrieval-augmented generation fetches relevant passages from your own data at question time and puts them in the prompt, so the model answers from current, citable facts. Fine-tuning changes the model's behaviour or style but is a poor way to add knowledge: it is costly to update, cannot cite, and still hallucinates. RAG suits knowledge that changes; fine-tuning suits format and tone.
2How do you evaluate a RAG system?Asked almost every time
Evaluate retrieval and generation separately. Retrieval: did the top-k chunks contain the answer (recall@k, MRR)? Generation: is the answer faithful to the context, relevant to the question and correct against a reference? Build a dataset of real questions with expected answers, run it on every change, and use LLM-as-judge plus spot checks by people.
3Walk through a production RAG pipeline end to end.Asked almost every time
Ingestion: load documents, clean and split them by structure, embed chunks and store them with metadata in a vector store, refreshing on changes. Query time: optionally rewrite the question, retrieve with hybrid search and filters, rerank, build a prompt with the top chunks and citations, generate an answer grounded in that context, and log everything for evaluation.
4How do you handle questions the documents cannot answer?Asked almost every time
Instruct the model to answer only from the context and say it does not know otherwise, set a relevance threshold below which no answer is attempted, offer a handoff or search link, and measure the 'don't know' rate on test questions that have no answer.
5How do you keep the index fresh when documents change?Asked almost every time
Track a content hash and source ID per chunk, re-embed only changed documents, delete chunks of removed documents, run incremental ingestion on a schedule or on change events, and record index versions so answers can be traced to a snapshot.
6How do you choose a chunk size and overlap?Very often asked
Start around a few hundred tokens with 10–20% overlap, split on natural boundaries, and then tune on your own evaluation set. Dense technical documents often need smaller chunks; narrative documents larger ones. Parent-document retrieval (search small chunks, return their larger parent) gets precise matching with enough context.
7How do metadata filters improve retrieval?Very often asked
Storing fields like product, region, date, access level and document type lets you filter before similarity search, so a question about the 2026 policy for India only searches relevant chunks, improving precision and enforcing permissions.
8How do you enforce document permissions in RAG?Very often asked
Store access control data with each chunk and filter retrieval by the user's groups, or keep separate indexes per tenant. Never rely on the model to hide content it was given.
9What is query rewriting or multi-query retrieval?Very often asked
An LLM rewrites a vague or conversational question into a clearer standalone query, or generates several variants, and results are merged. It improves recall for short or ambiguous questions at the cost of extra calls.
10How do you handle tables, PDFs and scanned documents?Very often asked
Use layout-aware parsers to keep tables and headings, OCR for scans, convert tables to Markdown or describe them, keep page numbers in metadata for citations, and test extraction quality before indexing.
11How do you add citations to answers?Very often asked
Number the retrieved chunks in the prompt, ask the model to cite chunk numbers after claims, map them back to source documents and pages, and verify cited text supports the claim for high-stakes answers.
12Which vector store would you choose and why?Very often asked
pgvector when you already run Postgres and want transactions and filters in one place; managed stores like Pinecone for scale without operations; Qdrant, Weaviate or Milvus for rich filtering and self-hosting; Chroma for prototypes. Choose by scale, filtering needs and operations.
13How do you reduce latency in a RAG app?Very often asked
Cache embeddings and frequent answers, retrieve fewer but better chunks, run retrieval and rewriting in parallel, use a smaller model for rewriting and reranking, stream the answer, and keep the vector store close to the app.
14What is contextual compression?Very often asked
After retrieval, extract only the sentences relevant to the question from each chunk (with an LLM or embeddings filter) before passing them to the model, reducing tokens and noise.
15How do you combine keyword and vector search in a RAG system?Very often asked
Run a BM25 or full-text search and a vector search in parallel, merge the candidates with reciprocal rank fusion or weighted scores, then rerank the merged list. Keyword search catches exact terms such as product codes and names that embeddings often blur.
16How do you split documents that mix headings, tables and code blocks?Very often asked
Start from the structure of the documents, splitting on headings and paragraphs, and test a few sizes on your evaluation set. Small chunks give precise matches but lose context; large chunks keep context but dilute relevance. A modest overlap keeps sentences that straddle a boundary retrievable.
17How do you build a RAG test set?Often asked
Collect real user questions, write reference answers and the documents that support them with domain experts, include unanswerable and tricky questions, and version the set as documents change.
18What is GraphRAG and when is it useful?Often asked
It builds a knowledge graph of entities and relations from documents and retrieves over that graph (often with community summaries), which helps for questions that connect many documents or need global summaries.
19How do you choose top-k?Often asked
Start with around 4–8 chunks and tune on your evaluation set: too few misses answers, too many adds noise and cost. Rerank a larger candidate set (20–50) down to the final k.
20How do you handle multilingual documents and questions?Often asked
Use a multilingual embedding model, or translate queries and documents to a pivot language; keep language metadata, and answer in the user's language.
21What are common RAG failure modes?Often asked
Retrieval misses the right chunk, chunks lack context (pronouns without referents), outdated documents win, the model ignores context and hallucinates, or conflicting sources produce mixed answers. Each needs its own fix and metric.
22How do you monitor RAG in production?Often asked
Trace each request with retrieved chunks and scores, collect user feedback, run online evaluators for faithfulness and relevance on samples, track 'don't know' rate, latency and cost per answer.
23How does LangChain structure a RAG chain?Often asked
A retriever (vector_store.as_retriever()) feeds a prompt template with context and question, piped into a chat model and an output parser, or wrapped as a retrieval tool for an agent created with create_agent.
24When should RAG be an agent tool instead of a fixed chain?Often asked
When questions may need several searches, different sources or no retrieval at all; an agent decides when and what to retrieve. Fixed chains are cheaper and more predictable for simple Q&A.
25What is a parent-child (small-to-big) retrieval strategy?Often asked
Embed small child chunks for precise matching, but when one matches, return its larger parent section to the model. You get accurate retrieval and enough surrounding context to answer well.
26How do you stop retrieved documents from carrying prompt injection into the model?Often asked
Treat retrieved text as untrusted data: put it in clearly delimited context, instruct the model never to follow instructions found in documents, strip or flag suspicious patterns at ingestion, and limit what tools the model may call based on retrieved content.
27How do you handle questions that compare two documents or time periods?Often asked
Decompose the question into sub-questions, retrieve separately for each entity or period with metadata filters, and have the model compare the two evidence sets explicitly, citing both.
28How do you evaluate retrieval separately from generation?Often asked
Label the passages that answer each test question, then measure recall@k and mean reciprocal rank for retrieval alone. Judge generation separately for faithfulness to the retrieved context and answer relevance, so you know which half to fix.
29How do you handle very long documents?Sometimes asked
Use hierarchical chunking (sections and paragraphs), store document summaries for coarse retrieval, then search within the chosen documents for detail.
30What is late chunking or contextual retrieval?Sometimes asked
Adding document-level context to each chunk before embedding (for example a short summary of where the chunk sits), so isolated chunks keep their meaning and retrieval improves.
31How do you control cost for a RAG service?Sometimes asked
Cache answers to repeated questions, limit context size, use cheaper models where quality allows, batch embedding jobs and avoid re-embedding unchanged content.
32How would you add a semantic cache to a RAG service?Sometimes asked
Embed each incoming question, look for a previously answered question above a high similarity threshold, scoped to the same user permissions and index version, and return the cached answer. Invalidate entries when the underlying documents change.
Objective questions 32
1What does an embedding model produce for a chunk of text?Asked almost every time
Why: Embeddings map text to vectors, so similar meanings end up close together for similarity search.
2What should a RAG app do when retrieved context lacks the answer?Asked almost every time
Why: This keeps answers grounded.
3How are document permissions enforced in RAG?Asked almost every time
Why: Never give the model forbidden content.
4What should trigger re-embedding?Asked almost every time
Why: Only changed documents.
5Which technique improves the ordering of retrieved chunks before they reach the model?Very often asked
Why: A reranker scores each question–chunk pair together, which is more accurate than vector distance alone.
6Why combine keyword (BM25) search with vector search?Very often asked
Why: Hybrid search catches exact-match terms that semantic vectors can miss.
7Metadata filters mainly improve…Very often asked
Why: Search only relevant chunks.
8What does multi-query retrieval do?Very often asked
Why: Several variants improve recall.
9Which store fits when you already run Postgres?Very often asked
Why: Vectors and filters in one database.
10How should citations be produced?Very often asked
Why: Verify they support the claim.
11What does contextual compression reduce?Very often asked
Why: Keeps only relevant sentences.
12A good starting top-k after reranking is…Very often asked
Why: Tune on your eval set.
13Hybrid search mainly helps with…Very often asked
Why: Keyword matching catches what embeddings blur.
14Reciprocal rank fusion is used to…Very often asked
Why: It combines rankings without comparable scores.
15GraphRAG helps most with…Often asked
Why: It uses entity relations.
16What should a RAG test set include?Often asked
Why: Cover tricky cases too.
17Which parser matters for PDFs with tables?Often asked
Why: Keeps tables and headings.
18When is an agent better than a fixed RAG chain?Often asked
Why: The agent decides retrieval.
19What should be traced for each RAG request?Often asked
Why: Needed to debug retrieval.
20Which embedding suits mixed-language content?Often asked
Why: Matches across languages.
21A common RAG failure is…Often asked
Why: Measure retrieval separately.
22Contextual retrieval adds… to each chunk before embedding.Often asked
Why: Chunks keep their meaning.
23Small-to-big retrieval returns…Often asked
Why: Precise matching plus enough context.
24Instructions found inside retrieved documents should be…Often asked
Why: Documents can carry prompt injection.
25Retrieval quality on its own is measured with…Often asked
Why: Separating retrieval from generation shows what to fix.
26Very small chunks tend to…Often asked
Why: Chunk size trades precision against context.
27Rerank a larger candidate set of about…Sometimes asked
Why: Then keep the best few.
28Which reduces RAG latency?Sometimes asked
Why: Users see output sooner.
29Very long documents are best handled with…Sometimes asked
Why: Coarse then fine retrieval.
30What reduces RAG cost for repeated questions?Sometimes asked
Why: Repeated questions are common.
31Which LangChain method turns a vector store into a retriever?Sometimes asked
Why: It returns a retriever runnable.
32A semantic cache for RAG must be scoped by…Sometimes asked
Why: Cached answers must not cross access boundaries or outlive their sources.
Track your progress through every blueprint in your dashboard.
Start learning