Explore topic-wise interview questions and answers.
RAG Fundamentals
QUESTION 01
What is Retrieval-Augmented Generation (RAG) and why was it developed?
š DEFINITION:
Retrieval-Augmented Generation (RAG) is an architecture that combines information retrieval with language model generation. Instead of relying solely on the model's parametric memory (knowledge stored in weights during training), RAG retrieves relevant documents from an external knowledge base and incorporates them into the prompt, grounding generation in verifiable, up-to-date information.
āļø HOW IT WORKS:
RAG operates in two main phases. First, retrieval: given a user query, the system retrieves relevant documents from a knowledge base using vector similarity search (embeddings) or keyword search. Second, generation: the retrieved documents are added to the prompt as context, and the LLM generates a response conditioned on both the query and the retrieved information. The model can cite sources, and answers are grounded in the provided documents. The knowledge base can be updated independently of the model, enabling fresh information without retraining.
š” WHY IT MATTERS:
RAG was developed to address fundamental limitations of pure LLMs. First, knowledge cutoff - models only know information up to their training date. RAG enables access to current information. Second, hallucination - models often fabricate information. RAG grounds answers in retrieved documents, reducing hallucinations. Third, source attribution - RAG can cite sources, enabling verification. Fourth, domain adaptation - organizations can use RAG with their private documents without fine-tuning. Fifth, cost - updating a knowledge base is cheaper than retraining models. RAG has become the dominant architecture for knowledge-intensive applications, powering customer support, research assistants, and enterprise AI.
š EXAMPLE:
A user asks a customer support chatbot about a recently released product. Pure LLM, trained months ago, doesn't know about it and hallucinates. RAG system: 1) Retrieves the new product documentation from the company's knowledge base. 2) Adds relevant sections to the prompt. 3) Generates answer based on actual documentation: 'The new XYZ Pro features 32GB RAM and costs $1999, according to our product specs.' With source citation. The answer is accurate, up-to-date, and verifiable. This is why RAG is essential for production applications requiring current, reliable information.
QUESTION 02
Describe the end-to-end pipeline of a basic RAG system.
š DEFINITION:
The RAG pipeline is a multi-stage process that transforms a user query into a grounded response through document retrieval and context-augmented generation. Each stage plays a critical role in ensuring the final output is accurate, relevant, and grounded in the provided knowledge base.
āļø HOW IT WORKS:
The pipeline consists of: 1) Query processing - clean and normalize user input, optionally expand or rewrite for better retrieval. 2) Retrieval - convert query to embedding, search vector database for similar document chunks, return top-k (typically 3-10) most relevant chunks. May combine with keyword search (hybrid). 3) Context assembly - combine retrieved chunks into a coherent context, respecting token limits, preserving document structure, adding metadata. 4) Prompt construction - create prompt with system instructions, retrieved context, and user query. Include formatting instructions and citation requirements. 5) Generation - LLM generates response conditioned on prompt, using retrieved context as sole knowledge source. 6) Post-processing - format output, verify citations, check for safety, add disclaimers if needed. 7) Logging - store query, retrieved chunks, response for monitoring and improvement.
š” WHY IT MATTERS:
Understanding the full pipeline is essential for building effective RAG systems. Each stage can fail independently: poor retrieval means wrong context, bad context assembly confuses the model, weak prompting leads to ignoring context, generation may hallucinate despite good context. Systematic optimization requires measuring and improving each component. The pipeline also determines latency and cost: retrieval adds 50-200ms, generation dominates. For production, each stage must be reliable, observable, and scalable.
š EXAMPLE:
Customer query: 'What's your return policy for electronics?' Pipeline: 1) Query cleaned, spelling corrected. 2) Embedding search returns 5 chunks: return policy overview, electronics exceptions, warranty info, refund process, damaged items policy. 3) Context assembled with section headers. 4) Prompt: 'Using only the provided documents, answer the query. Cite sources. Documents: [chunks]. Query: [query].' 5) Model generates: 'Electronics can be returned within 30 days if unopened. Opened items have 15-day return window (per Electronics Returns policy). Full refund processed within 5-7 business days (Refund Process doc).' 6) Citations added, response checked for safety. 7) Logged for quality monitoring. This pipeline delivers accurate, grounded response.
QUESTION 03
What are the main components of a RAG system?
š DEFINITION:
A RAG system comprises three main components that work together to enable grounded generation: the indexing pipeline for preparing knowledge, the retrieval system for finding relevant information, and the generation component for synthesizing responses. Each component has subcomponents that must be carefully designed and integrated.
āļø HOW IT WORKS:
Indexing pipeline: document loader (ingests various formats: PDF, HTML, DOCX), splitter/chunker (divides documents into manageable pieces with overlap), embedder (converts chunks to vectors), vector store (database for similarity search). Also stores metadata (source, date, section) for filtering. 2) Retrieval system: query embedder (same model as indexing), search algorithm (ANN: HNSW, IVF), reranker (optional second-stage reordering for precision), filter application (metadata constraints). 3) Generation component: prompt template (structures context and query), LLM (generates response), output parser (extracts structured data), citation verifier (checks claims against sources). Optional: query rewriting, retrieval feedback loops, and caching.
š” WHY IT MATTERS:
Each component affects overall system performance. Chunk size impacts retrieval precision and context utilization. Embedding model quality determines if relevant documents are found. Vector store choice affects latency and scalability. Reranking can boost precision by 10-20%. Prompt design determines whether model uses retrieved context effectively. Understanding components enables systematic optimization: if answers are bad, is it retrieval failure (wrong chunks) or generation failure (ignoring chunks)? Component-level evaluation pinpoints issues. For production, each component must be reliable, observable, and replaceable.
š EXAMPLE:
Legal RAG system components: Indexing: processes contracts (PDFs), chunks by section (500 tokens with 10% overlap), embeds with legal-tuned model, stores in Pinecone with metadata (contract type, date, parties). Retrieval: query embedded, searches top-20, reranks with cross-encoder for precision, filters by date range. Generation: prompt instructs model to cite specific clauses, uses GPT-4, verifies citations against source chunks. If answer cites wrong section, component-level analysis shows retrieval found right document but wrong chunk - chunking strategy needs adjustment. This component view enables targeted fixes.
QUESTION 04
What is chunking and why does chunk size matter in RAG?
š DEFINITION:
Chunking is the process of splitting documents into smaller segments for embedding and retrieval in RAG systems. Chunk size is a critical hyperparameter that affects retrieval precision, context utilization, and answer quality. Choosing the right chunk size requires balancing competing objectives based on document type and query patterns.
āļø HOW IT WORKS:
Documents are divided into chunks of specified size (typically 200-1500 tokens) with possible overlap (10-20%) to preserve boundary information. Chunking strategies: fixed-size (simple but may cut sentences), recursive (respect paragraph/sentence boundaries), semantic (split at topic changes using embeddings), and structure-aware (preserve document sections). Chunk size impacts: 1) Retrieval precision - smaller chunks more focused (higher precision), larger chunks contain more context but also noise (lower precision). 2) Context utilization - more small chunks needed to cover topic, consuming context window. 3) Cross-chunk reasoning - information split across chunks may be missed if not all retrieved. 4) Embedding quality - very small chunks lack context for good embeddings.
š” WHY IT MATTERS:
Chunk size choice can swing RAG performance by 10-20%. Too small (200 tokens): high precision but may split related information, require many chunks per query, exceed context window. Too large (1500 tokens): low precision, irrelevant information dilutes relevance, may miss specific answers within chunk. Optimal size depends on content: narrative documents (research papers) benefit from larger chunks preserving argument flow; factual documents (product specs) benefit from smaller chunks targeting specific facts. Testing different sizes on your data is essential. Common practice: 500-1000 tokens with 10% overlap, then tune based on retrieval metrics and answer quality.
š EXAMPLE:
Technical manual RAG with two chunk sizes. 300-token chunks: retrieval precision 0.85, recall 0.70, need 8 chunks (2400 tokens) per query, answer accuracy 80%. 1000-token chunks: precision 0.70, recall 0.85, need 3 chunks (3000 tokens), answer accuracy 82% (better due to preserving cross-section relationships). For FAQ-style queries about specific specs, 300-token chunks achieve 90% accuracy vs 75% for large chunks. No universal best - must test. This is why chunking experimentation is RAG best practice.
QUESTION 05
What are common chunking strategies (fixed-size, recursive, semantic, sentence)?
š DEFINITION:
Different chunking strategies optimize for different goals: computational efficiency, semantic coherence, or structural preservation. The choice of strategy affects how well chunks capture complete thoughts, how easily information can be retrieved, and how much context is preserved across chunk boundaries.
āļø HOW IT WORKS:
Fixed-size chunking - split documents into exact token counts (e.g., 500 tokens) regardless of content boundaries. Simplest, fastest, but may cut sentences or ideas in half, creating incomplete chunks. 2) Recursive chunking - use separators (paragraph breaks, sentence boundaries) to create chunks that respect natural language units while staying under size limit. Tries separators from largest (paragraph) to smallest (sentence) to find best split. 3) Semantic chunking - use embeddings to detect topic boundaries, splitting when content shifts significantly. More computationally expensive but creates thematically coherent chunks. 4) Sentence chunking - split at sentence boundaries, then group sentences until size limit. Preserves sentence completeness but may group unrelated sentences. 5) Structure-aware chunking - preserve document structure (sections, headers), keeping related content together.
š” WHY IT MATTERS:
Chunking strategy significantly impacts retrieval quality. Fixed-size may cut critical sentences in half, making retrieval impossible. Recursive chunking balances coherence and simplicity, making it the most common choice. Semantic chunking can improve retrieval for long, varied documents by keeping topics together. Sentence chunking works well for factual content where each sentence is self-contained. Structure-aware chunking is essential for documents where section context matters (legal contracts, research papers). The best strategy depends on document type and query patterns.
š EXAMPLE:
Research paper chunking. Fixed-size 500-token chunks: cuts the methods section across 3 chunks, losing methodology coherence - retrieval may find only part. Recursive chunking: keeps paragraphs together, methods section becomes 2 coherent chunks. Semantic chunking: detects when paper moves from methods to results, keeps each section as semantic unit. Structure-aware: preserves section headers, so retrieved chunk includes 'Methods' header providing context. For a query about experimental setup, structure-aware retrieval finds the complete methods section with header, enabling accurate answer. The more sophisticated strategy costs more compute but improves quality for this use case.
QUESTION 06
What is the retrieval step in RAG and how does it work?
š DEFINITION:
The retrieval step in RAG is the process of finding and selecting the most relevant document chunks from a knowledge base to augment the prompt for generation. It's the critical bridge between the user's query and the information needed to answer it, determining whether the generation component has access to the right context.
āļø HOW IT WORKS:
The retrieval process typically involves: 1) Query encoding - convert user query to a vector embedding using the same embedding model used for indexing documents. 2) Similarity search - query the vector database to find chunks with most similar embeddings (cosine similarity or dot product). Algorithms like HNSW or IVF enable fast approximate nearest neighbor search at scale. 3) Top-k selection - retrieve the k most similar chunks (typically 3-10). 4) Optional reranking - apply a more accurate (but slower) cross-encoder to reorder retrieved chunks by relevance. 5) Optional filtering - apply metadata filters (date, source, category) to narrow results. 6) Result formatting - prepare chunks with source metadata for inclusion in prompt. The entire retrieval step typically takes 50-200ms depending on database size and indexing method.
š” WHY IT MATTERS:
Retrieval quality directly determines RAG success. If retrieval misses relevant chunks, the model lacks information to answer correctly. If retrieval includes irrelevant chunks, the model may be distracted or confused. Metrics like recall@k (does it find relevant chunks?) and precision@k (are retrieved chunks relevant?) predict answer quality. Retrieval failures are the most common cause of poor RAG performance. Optimization involves: embedding model selection, chunking strategy, index tuning, hybrid search, and reranking.
š EXAMPLE:
Customer query: 'How do I reset my password?' Retrieval step: 1) Query embedded. 2) Vector search across 100k documentation chunks returns top-10: 3 chunks about password reset (relevant), 4 about account security (somewhat relevant), 3 about billing (irrelevant). 3) Reranker reorders: password reset chunks now top-3, billing chunks filtered out. 4) Top-5 chunks (all relevant after reranking) sent to generation. Result: accurate password reset instructions. Without reranker, generation might see billing info and get confused. This multi-stage retrieval maximizes relevance while maintaining speed.
QUESTION 07
What is the generation step in RAG and how does context get injected into the prompt?
š DEFINITION:
The generation step in RAG takes the retrieved document chunks and the user query, combines them into a prompt, and uses an LLM to generate a response grounded in the provided context. The prompt design is critical - it must instruct the model to use only the retrieved information, cite sources, and handle cases where the context lacks relevant information.
āļø HOW IT WORKS:
The generation process: 1) Context assembly - retrieved chunks are formatted with clear delimiters, source metadata, and possibly section headers. Chunks ordered by relevance, with most important first. 2) Prompt construction - system prompt establishes model persona and rules ('You are a helpful assistant. Only use information from the provided documents.'). Retrieved context inserted, often with instructions ('Documents: [chunks]'). User query added. 3) Generation - LLM processes prompt, generates response token by token, attending to both query and context. 4) Citation enforcement - model may be instructed to cite sources ('[1]' after claims). 5) Fallback handling - if context lacks answer, model should say so rather than hallucinate. 6) Output formatting - structure response as specified (JSON, bullet points, etc.).
š” WHY IT MATTERS:
The generation step determines whether retrieved information is actually used correctly. Poor prompts lead to models ignoring context, hallucinating despite good retrieval, or failing to cite sources. Key design elements: explicit instruction to use only provided documents, citation requirements to enable verification, handling of missing information to prevent hallucination, and format specifications for consistent output. Generation failures are often prompt issues, not model capability issues.
š EXAMPLE:
Generation prompt for customer support: 'System: You are a customer support assistant. Answer the user's question using ONLY the provided documents. If the documents don't contain the answer, say 'I cannot find this information in our documentation.' Cite the source document for each claim using [Doc X]. Documents: [Doc1] Password reset steps... [Doc2] Account recovery options... [User] How do I reset my password? [Assistant]' Model generates: 'To reset your password, go to the login page and click 'Forgot Password' (Doc1). You'll receive an email with reset instructions. If you don't have access to your email, try account recovery options (Doc2).' Grounded, cited, and complete. Without proper instructions, model might add steps from its own knowledge or fail to cite.
QUESTION 08
What is the difference between RAG and fine-tuning? When do you use each?
š DEFINITION:
RAG and fine-tuning are fundamentally different approaches to incorporating knowledge into LLM applications. RAG retrieves external information at inference time and adds it to context, keeping the model static. Fine-tuning updates model weights through additional training to internalize knowledge. Each has distinct strengths and use cases.
āļø HOW IT WORKS:
RAG: external knowledge base + retrieval + in-context learning. Knowledge is stored separately, retrieved per query, added to prompt. Model unchanged. Fine-tuning: additional training on domain data modifies model weights. Knowledge becomes part of parametric memory, accessible without retrieval. Trade-offs: RAG provides updatable knowledge (change knowledge base, change behavior), attribution (can cite sources), handles large knowledge bases (millions of documents). Fine-tuning provides faster inference (no retrieval latency), works in low-resource settings (no vector DB), can learn task style and format, may handle nuanced patterns better.
š” WHY IT MATTERS:
Choice depends on use case. Use RAG when: knowledge changes frequently, you need source attribution, knowledge base is large, you lack compute for fine-tuning, or you need to update information without model retraining. Use fine-tuning when: you need to teach task format or style, knowledge is stable and fits in model, you have limited context window, or you need lowest possible latency. Often combined: fine-tune for style/task mastery, RAG for factual knowledge.
š EXAMPLE:
Customer support for software product. Product documentation changes with each release (monthly). RAG: knowledge base updated monthly, model unchanged. Works perfectly. For legal contract analysis with stable terminology and required output format, fine-tune on 1000 examples to learn legal reasoning and JSON format. Then combine with RAG for specific contract text. The fine-tuning teaches the task; RAG provides the document. This hybrid approach leverages strengths of both.
QUESTION 09
What is naive RAG vs. advanced RAG?
š DEFINITION:
Naive RAG refers to the simplest implementation: chunk documents, embed them, retrieve top-k chunks based on query similarity, and stuff them into a prompt for generation. Advanced RAG incorporates multiple optimizations at each stage - pre-retrieval (query rewriting, expansion), retrieval (hybrid search, reranking), and post-retrieval (context compression, reordering) - to improve accuracy and reliability.
āļø HOW IT WORKS:
Naive RAG pipeline: chunk ā embed ā index ā retrieve top-k ā generate. Simple but has known failure modes: poor retrieval due to query-document mismatch, context window overflow, irrelevant chunks distracting model, missing cross-chunk information. Advanced RAG adds: Pre-retrieval: query rewriting (expand acronyms, correct spelling), query expansion (generate multiple query variants), HyDE (generate hypothetical document to retrieve similar). Retrieval: hybrid search (combine vector + keyword), metadata filtering, reranking with cross-encoder. Post-retrieval: context compression (extract relevant sentences), reorder chunks (put most relevant at edges to combat lost-in-middle), iterative retrieval (multi-hop).
š” WHY IT MATTERS:
Naive RAG works for simple applications but fails on complex queries, specialized domains, or large knowledge bases. Advanced RAG techniques can improve accuracy by 10-30% by addressing specific failure modes. Query rewriting helps when user queries are poorly phrased. Reranking ensures most relevant chunks used. Context compression fits more information in limited window. The right techniques depend on your data and query types - not all needed for every application.
š EXAMPLE:
Medical RAG system. Naive RAG: user query 'HTN treatment' retrieves chunks with 'HTN' but misses those with 'hypertension'. Fails. Advanced with query expansion: expands 'HTN' to 'hypertension' and 'high blood pressure', retrieves more relevant results. Reranking: cross-encoder ensures treatment guidelines prioritized over general information. Context compression: extracts only treatment sentences, fitting more guidelines in context. Result: comprehensive treatment recommendations vs partial or missed information in naive approach. The 20% accuracy improvement justifies additional complexity.
QUESTION 10
What are the main failure modes in a RAG pipeline?
š DEFINITION:
RAG systems can fail in multiple ways across the pipeline: retrieval may miss relevant information, retrieve irrelevant information, or return chunks that don't contain complete answers; generation may ignore retrieved context, hallucinate beyond it, or fail to synthesize across chunks. Understanding these failure modes is essential for systematic improvement.
āļø HOW IT WORKS:
Common failure modes: 1) Retrieval misses relevant chunks - due to poor embeddings, chunking that splits information, or query-document mismatch. Results in missing information. 2) Retrieval returns irrelevant chunks - low precision, distracting model with wrong information. 3) Context overflow - too many chunks exceed window, forcing truncation and loss. 4) Lost in the middle - model ignores relevant chunks placed in middle of context. 5) Hallucination - model adds information not in retrieved chunks. 6) Citation errors - model cites wrong source or makes up citations. 7) Synthesis failure - information spread across chunks not combined correctly. 8) Format errors - output not in required structure.
š” WHY IT MATTERS:
Each failure mode requires different fixes. Missed retrieval: improve embeddings, chunking, or add hybrid search. Irrelevant retrieval: add reranking, tune chunk size. Lost in middle: reorder chunks by relevance, use fewer chunks. Hallucination: strengthen prompt instructions, add citation requirements. Synthesis failure: use multi-hop retrieval or chain-of-thought prompting. Diagnosing which failure modes occur requires component-level evaluation: measure retrieval recall/precision separately from generation quality. This targeted approach is more efficient than random tweaking.
š EXAMPLE:
Legal RAG system evaluation reveals: Retrieval recall@5 is 85% (good), precision@5 is 60% (poor). Generation quality 70%. Analysis: low precision means model gets irrelevant chunks 40% of time, distracting it. Fix: add reranker, precision improves to 85%, generation quality to 85%. Another failure: when relevant chunks are in middle positions (positions 3-4), model often misses them. Fix: reorder chunks by relevance, putting most important first. Generation quality improves to 90%. Systematic diagnosis enabled targeted fixes that improved overall performance efficiently.
QUESTION 11
How does metadata filtering improve RAG retrieval quality?
š DEFINITION:
Metadata filtering in RAG uses structured information about documents (date, author, source type, category, etc.) to constrain retrieval before or after vector search, ensuring that only relevant subsets of the knowledge base are considered. This dramatically improves precision by eliminating obviously irrelevant documents and enables time-sensitive or category-specific queries.
āļø HOW IT WORKS:
During indexing, each chunk is stored with metadata fields: source document ID, date, document type (manual, FAQ, policy), category, author, version, access level, etc. During retrieval, the query is analyzed to extract metadata constraints (explicit or implicit). Filters are applied in two ways: pre-filtering (search only chunks matching metadata) or post-filtering (retrieve top-k, then filter by metadata). Pre-filtering is more efficient for large databases but may miss relevant chunks if filter too restrictive. Post-filtering ensures recall but may waste compute. Filters can be combined with vector search (e.g., 'find chunks similar to query where date > 2023 and type = "policy"').
š” WHY IT MATTERS:
Metadata filtering can dramatically improve retrieval quality. Without it, a query about '2024 return policy' might retrieve 2021 policies (similar text but outdated). With date filtering, only current policies considered. For enterprise RAG with documents from multiple sources, filtering by source ensures answers come from authoritative documents. For multi-tenant systems, filtering by access level ensures users only see documents they're authorized to view. Precision improvements of 20-30% are common with good metadata.
š EXAMPLE:
Customer support for electronics company with products from 2010-2024. User query: 'How do I update firmware on my 2023 TV?' Without metadata filtering, retrieval might return firmware update instructions for 2015 TV (similar query but different process). With metadata filtering: extract '2023' as date filter, search only 2023 documents. Results contain correct instructions for that model year. Accuracy improves from 70% to 95%. For enterprise legal search, filtering by 'jurisdiction = California' ensures answers based on correct state law. Metadata turns vector search from fuzzy similarity into precise, constrained retrieval.
QUESTION 12
What is the role of the embedding model in RAG?
š DEFINITION:
The embedding model in RAG converts both documents and queries into dense vector representations that capture semantic meaning. It's the foundation of retrieval quality - if embeddings don't capture relevant semantics, similar documents won't be found, and the entire RAG system fails regardless of generation quality.
āļø HOW IT WORKS:
During indexing, each document chunk is passed through the embedding model to produce a vector (typically 384-1536 dimensions). These vectors are stored in a vector database. During retrieval, the user query is embedded with the same model, and the database finds chunks with most similar vectors via cosine similarity or dot product. The embedding model must be trained to place semantically similar texts close together in vector space. Models vary in: training data (general vs domain-specific), dimensionality (affects storage and speed), and architecture (bi-encoder vs cross-encoder).
š” WHY IT MATTERS:
Embedding model choice is the most important RAG design decision. A general model (text-embedding-ada-002) works well for broad domains but may miss specialized terminology. A medical-tuned model (BioBERT) captures biomedical concepts accurately. Wrong choice can reduce retrieval recall by 20-30%. Trade-offs: larger models (1B parameters) are more accurate but slower and more expensive; smaller models (100M) are faster and cheaper but may miss nuance. Embedding dimensionality affects storage costs (10M documents Ć 1536-dim Ć 4 bytes = 61GB) and search speed. Regular evaluation on your domain is essential.
š Example: Legal RAG system comparing embedding models. General model (ada-002) recall@10: 0.75. Legal-tuned model (LegalBERT) recall@10: 0.88 - 13% improvement. For queries about 'tort liability', general model retrieves documents about 'personal injury' (related but not precise), legal model finds exact case law. The better retrieval directly improves answer accuracy from 70% to 85%. This is why embedding model selection and potentially fine-tuning are critical RAG investments.
QUESTION 13
What is chunk overlap and when should you use it?
š DEFINITION:
Chunk overlap is a technique where consecutive chunks in a document share a portion of text (typically 10-20% of chunk size). This ensures that information near chunk boundaries isn't lost and that retrieval can find relevant content even if it falls near the edge of a chunk.
āļø HOW IT WORKS:
When chunking a document with size S and overlap O, the first chunk contains tokens 1..S, the second chunk contains tokens S-O+1 .. 2S-O, and so on. For example, with 1000-token chunks and 10% overlap (100 tokens), chunk 1 covers tokens 1-1000, chunk 2 covers 901-1900. This ensures that content around token 950 appears in both chunks. Overlap can be implemented with token-level or sentence-level boundaries (overlapping sentences to maintain coherence).
š” WHY IT MATTERS:
Without overlap, information near chunk boundaries may be split and lost. If a query asks about a concept that spans from token 995 to 1005, without overlap it's split across two chunks, neither containing the complete context. With overlap, at least one chunk contains the full concept. Overlap also helps with retrieval: the same information may be embeddable in slightly different contexts, increasing chance of retrieval. However, overlap increases storage (more chunks) and potentially retrieves duplicate information. Typical overlap 10-20% balances benefits against costs.
š EXAMPLE:
Technical manual section on 'Safety Precautions' ends at token 1000, next section 'Installation Steps' starts at 1001. A query about 'safety during installation' needs information from both. Without overlap: safety section chunk ends at 1000, installation starts at 1001 - neither chunk contains both concepts. With 10% overlap: second chunk includes last 100 tokens of safety plus first 900 of installation. This chunk contains both concepts, enabling accurate answer. Retrieval finds this single chunk, generation synthesizes correctly. Overlap turned a failing case into success.
QUESTION 14
How do you handle tables, images, or structured data in a RAG pipeline?
š DEFINITION:
Handling non-text content in RAG requires specialized processing to extract and represent information from tables, images, and structured data in ways that preserve meaning and enable retrieval. Different modalities need different approaches: tables may be converted to text descriptions, images may require captioning or OCR, structured data may need serialization.
āļø HOW IT WORKS:
For tables: options include converting to markdown/text representation, summarizing table content, or using table-specific embeddings. For images with text (screenshots, scanned docs): OCR extracts text, then processed normally. For images without text: generate captions using vision-language models, embed captions. For complex structured data (JSON, XML): flatten to text with schema information, or create structured representations with metadata. For PDFs with mixed content: use libraries (PyMuPDF, Unstructured) to extract text by reading order, preserve structure with headers. For multimodal RAG, some systems use multimodal embeddings that can directly compare images and text.
š” WHY IT MATTERS:
Many real-world documents contain crucial information in non-text formats. Product specs in tables, charts in research papers, screenshots in user manuals - if this content isn't properly handled, RAG misses critical information. Poor table handling loses relationships between rows and columns. Missing image captions loses visual information. For enterprise RAG, up to 30% of document value may be in non-text content. Proper handling can mean the difference between useful and useless retrieval.
š EXAMPLE:
User asks: 'What were Q3 2023 sales figures for Europe?' Document contains a table with regions as rows, quarters as columns. Without table handling, retrieval might find surrounding text but miss the actual numbers. With table handling: table converted to text 'Region: Europe, Q3 2023: ā¬4.2M...' and embedded. Retrieval finds this chunk, generation provides exact figure. For an architectural question about building specs, a diagram might be essential - captioning extracts key information, enabling answer. This multimodal handling makes RAG truly document-comprehensive.
QUESTION 15
What is a retrieval threshold and how do you set it?
š DEFINITION:
A retrieval threshold is a minimum similarity score that retrieved chunks must meet to be considered relevant and included in context. Chunks below the threshold are discarded, even if they're in the top-k. This prevents the model from seeing irrelevant or marginally relevant information that could distract or mislead generation.
āļø HOW IT WORKS:
During retrieval, each chunk gets a similarity score (cosine similarity, dot product) with the query. A threshold T is set (e.g., 0.7 on a 0-1 scale). After retrieving top-k, chunks with score < T are filtered out. If fewer than k chunks remain, only those are used (or retrieval considered insufficient). Thresholds can be absolute (fixed value) or dynamic (percentile-based). Setting threshold requires analyzing score distributions for relevant vs irrelevant chunks on a validation set. Too high: miss relevant chunks, causing insufficient context. Too low: include irrelevant chunks, distracting model.
š” WHY IT MATTERS:
Retrieval thresholds improve RAG reliability by preventing low-quality context. Without threshold, the 5th chunk with similarity 0.3 might be completely irrelevant but still included, potentially confusing the model. With threshold, only truly relevant chunks (score >0.6) are used. This is especially important when knowledge base contains diverse documents - not all top-k are equally useful. Threshold also enables 'I don't know' responses: if no chunks above threshold, model can be instructed to say it doesn't have information rather than hallucinate.
š EXAMPLE:
Customer support RAG with 100k documents. Query about 'refund policy for damaged items'. Retrieval scores: chunk1 0.89 (relevant), chunk2 0.82 (relevant), chunk3 0.75 (relevant), chunk4 0.55 (general terms), chunk5 0.30 (unrelated product). Threshold 0.7: only chunks 1-3 used. Generation gets focused relevant information. Without threshold: all 5 chunks included, model might be distracted by unrelated content in chunk5. For query about rare product, all scores might be <0.7 - model responds 'I cannot find specific information about this product in our documentation' rather than hallucinating. This safe behavior is enabled by thresholding.
QUESTION 16
How does RAG handle questions that require information from multiple documents?
š DEFINITION:
Multi-document questions require synthesizing information across multiple sources, which RAG handles through retrieval that brings relevant chunks from different documents into context, combined with generation that can integrate and reason across them. This capability distinguishes RAG from simple document lookup and enables complex reasoning tasks.
āļø HOW IT WORKS:
Process: 1) Retrieval configured to return top-k chunks (k typically 5-10) from across the knowledge base, not just one document. 2) Retrieved chunks may come from different documents, each containing part of the answer. 3) Context assembly includes all chunks with source metadata. 4) Prompt instructs model to synthesize across provided information. 5) Generation combines insights, resolves conflicts, and produces integrated answer. For complex questions, multi-hop retrieval may be needed: retrieve initial chunks, extract entities or new queries, retrieve additional chunks, then synthesize.
š” WHY IT MATTERS:
Real-world questions rarely reference single documents. 'Compare the Q3 earnings of Apple and Microsoft' needs data from two different reports. 'What are the side effects and benefits of this drug?' may come from clinical trials, patient reports, and regulatory documents. RAG's ability to handle multi-document synthesis is what makes it useful for research, analysis, and decision support. The key challenges: retrieving all relevant pieces (not missing any), synthesizing accurately without contradiction, and citing sources correctly.
š EXAMPLE:
User asks: 'How does the 2023 tax law affect small businesses and what deductions are available?' Retrieval returns: chunk1 from IRS publication (tax rates), chunk2 from small business guide (deduction types), chunk3 from accountant blog (examples), chunk4 from recent news (effective dates). Generation synthesizes: 'Under the 2023 tax law, small businesses (under $10M revenue) have a 21% corporate rate (IRS doc). Available deductions include Section 179 equipment purchases up to $1.16M (Small Business Guide) and home office deductions (Accountant Blog). These provisions took effect January 1, 2023 (News).' The answer draws from multiple sources, integrated coherently.
QUESTION 17
What is the context stuffing problem in RAG?
š DEFINITION:
Context stuffing occurs when too many retrieved chunks are packed into the prompt, overwhelming the limited context window and potentially causing the model to lose focus on the most relevant information. This can happen when retrieval returns many chunks, chunks are too large, or the system tries to include everything without prioritization.
āļø HOW IT WORKS:
Context window limits (e.g., 4K-200K tokens) constrain how much retrieved information can be included. If retrieval returns 20 chunks of 500 tokens each (10K tokens) but window is 8K, only some can fit. Even if all fit, too much information can dilute attention - the model may focus on less relevant parts and miss crucial information. Symptoms: answers become generic, miss key details, or hallucinate due to insufficient focus on correct chunks. The lost-in-the-middle problem exacerbates context stuffing - information in the middle of a long context is most likely ignored.
š” WHY IT MATTERS:
Context stuffing degrades RAG quality. Simply retrieving more is not better - beyond a point, additional context harms performance. Optimal number of chunks depends on: chunk size (smaller chunks allow more), task complexity (complex tasks need more context), and model capabilities (larger models handle more context better). Solutions: rerank to put most relevant chunks first, compress chunks (extract key sentences), use sliding window approaches, or implement iterative retrieval that brings in additional chunks only when needed.
š EXAMPLE:
Legal RAG with 32K context. Retrieval returns 20 chunks of 1500 tokens each (30K total) - fits. But answer quality is worse than with 8 chunks (12K). Analysis shows: model attends to first 3 chunks and last 2 chunks (lost-in-middle). Critical information in chunk 10 ignored. Solution: rerank by relevance, put top-8 chunks first, discard bottom-12. Quality improves. Another approach: compress each chunk to 300 tokens (extract key sentences), now 20 compressed chunks fit (6K) and are all relevant. Context stuffing solved by quality over quantity.
QUESTION 18
How do you handle confidential or access-controlled documents in a RAG system?
š DEFINITION:
Handling confidential documents in RAG requires implementing access controls at multiple levels: document indexing (ensure only authorized documents are indexed for each user), retrieval (filter results based on user permissions), and generation (prevent leakage of restricted information). This is essential for enterprise deployments where data security and compliance are critical.
āļø HOW IT WORKS:
Multi-layer approach: 1) Indexing with metadata - each chunk stored with access control metadata (user roles, departments, clearance levels). 2) Pre-filtering - during retrieval, apply metadata filters based on user's permissions before vector search. Only search chunks user is authorized to see. 3) Post-filtering - if pre-filtering not possible, retrieve then filter, but may waste compute. 4) Generation safeguards - instruct model not to reveal restricted information, though this is less reliable. 5) Audit logging - track all retrieval and generation for compliance. 6) Data isolation - for highest security, maintain separate indexes per tenant or user group. 7) PII redaction - remove sensitive information from chunks before indexing when appropriate.
š” WHY IT MATTERS:
Without access controls, RAG systems can leak confidential information. An employee query about 'salary bands' might retrieve executive compensation documents they shouldn't see. A cross-tenant query in multi-tenant SaaS might expose customer A's data to customer B. Beyond privacy, legal requirements (HIPAA, GDPR, CCPA) mandate strict access controls. Proper implementation enables secure enterprise RAG deployment. The challenge: balancing security with usability - too restrictive prevents legitimate access.
š EXAMPLE:
Enterprise RAG for a law firm with document access based on case assignment. Attorney queries about 'precedent for contract disputes' should only see documents from cases they're assigned to. System: each document chunk indexed with case ID and attorney list. During retrieval, user's case list used as filter: search only chunks where case ID in user's cases. Query returns relevant, authorized documents only. Another attorney on different cases sees different results. Audit log shows who accessed what. This enables secure, compliant deployment in sensitive legal environment.
QUESTION 19
What latency and cost trade-offs should you consider when designing a RAG system?
š DEFINITION:
RAG system design involves balancing latency (response time) and cost across components: embedding, retrieval, reranking, and generation. Each component can be optimized or scaled, but improvements in one dimension often degrade another. Understanding these trade-offs is essential for building systems that meet user expectations within budget constraints.
āļø HOW IT WORKS:
Key components and their latency/cost profiles: 1) Embedding (query) - 10-100ms, $0.0001-0.001 per query. Larger models more accurate but slower and costlier. 2) Vector search - 20-200ms, $0.00001-0.0001 per query. Faster with smaller indexes, approximate search. 3) Reranking - 50-200ms, $0.001-0.01 per query. Improves precision but adds latency and cost. 4) Generation - 500-5000ms, $0.001-0.1 per query. Dominates latency and cost. Larger models slower and costlier but better quality. 5) Caching - can reduce latency/cost for repeated queries. Trade-offs: more retrieved chunks (better recall) increases context size, slowing generation and costing more. Better embedding model improves retrieval but adds latency. Reranking helps precision but adds component.
š” WHY IT MATTERS:
Different applications have different requirements. Chatbots need low latency (<1s) - optimize for speed: smaller embedding model, skip reranking, use smaller generation model, cache aggressively. Offline batch processing cares about cost, not latency - use larger models, full reranking, optimize for accuracy. Real-time APIs need balance - typical p95 latency <2s. Understanding trade-offs enables right-sizing: don't use GPT-4 and reranking if a 7B model with good retrieval meets quality needs at 10% cost.
š EXAMPLE:
Customer support chatbot with 1M queries/month. Option A (high quality): ada-002 embedding (10ms), HNSW search (50ms), cross-encoder reranking (100ms), GPT-4 (2s generation). Latency 2.16s, cost $0.05/query = $50k/month. Option B (balanced): MiniLM embedding (5ms), HNSW search (50ms), no reranking, 7B model (500ms). Latency 555ms, cost $0.003/query = $3k/month. Quality difference 5% (measured by user satisfaction). For this use case, Option B better - 94% cost reduction for slight quality loss. Trade-off analysis guides decision.
QUESTION 20
How would you explain RAG to a non-technical executive?
š DEFINITION:
RAG is a way to give AI assistants access to your company's specific information - like giving an incredibly smart intern access to your entire document library. Instead of relying on what the AI learned from the internet (which may be outdated or wrong about your business), RAG lets it look up the actual facts in your own documents before answering.
āļø HOW IT WORKS:
Think of it like a two-step process. First, when someone asks a question, the system quickly searches through all your company documents (manuals, policies, past emails) to find the most relevant information - like a super-fast research assistant. Then, it gives both the question and those relevant documents to the AI, which reads them and formulates an answer based specifically on what it found. The AI can even tell you which document it got each piece of information from, so you can verify it.
š” WHY IT MATTERS:
For your business, RAG solves three big problems. First, accuracy: the AI answers based on your actual documents, not guesses. Second, freshness: when your policies change, we just update the document library - no need to retrain the AI. Third, trust: you can see exactly where information came from. This means you can confidently use AI for customer support (always giving correct policy information), employee training (finding specific procedures), or research (pulling insights from your data). It turns general-purpose AI into a specialized tool that actually knows your business.
š EXAMPLE:
Imagine a customer asks your support chatbot: 'What's your return policy for electronics bought during Black Friday?' Without RAG, the AI guesses based on general knowledge - maybe wrong. With RAG, it first searches your actual policy documents, finds the Black Friday electronics return policy, and answers: 'Electronics purchased during Black Friday can be returned until January 15th, per our Holiday Returns Policy (Section 3.2).' The answer is correct, up-to-date, and traceable to an actual company document. That's what RAG delivers - AI that actually knows your business.