beginner 25 min read

RAG Fundamentals

Learn what Retrieval-Augmented Generation is, why it matters, and how RAG systems work step by step.

Learning Objectives

  • Understand what Retrieval-Augmented Generation is
  • Learn why LLMs need external knowledge
  • Understand the complete RAG pipeline
  • Identify the key components of a RAG system
  • Recognize common RAG failure modes

What is Retrieval-Augmented Generation?

Retrieval-Augmented Generation (RAG) is an architecture pattern that enhances Large Language Model (LLM) responses by retrieving relevant information from external knowledge sources before generating an answer.

Instead of relying solely on what a model learned during training, RAG systems first search through a collection of documents, databases, or other knowledge sources to find information relevant to the user's question. This retrieved context is then provided to the LLM alongside the question, enabling it to generate more accurate, up-to-date, and verifiable responses.

The term "Retrieval-Augmented Generation" was introduced by Patrick Lewis et al. in their 2020 research paper, which demonstrated that combining a retrieval mechanism with a generative model significantly improves factual accuracy.

Key Insight
Think of RAG as giving an LLM an open-book exam instead of a closed-book exam. The model can look up information before answering, rather than relying entirely on memorized knowledge.

Why Do AI Models Need External Knowledge?

Large Language Models are trained on massive datasets, but they have fundamental limitations that RAG addresses:

Knowledge Cutoff: LLMs only know information up to their training date. They cannot access information about events, updates, or new research that occurred after training. A model trained in 2023 cannot answer questions about events in 2024.

Hallucination: When LLMs encounter questions outside their training data or about topics they have limited knowledge of, they may generate plausible-sounding but factually incorrect information. This is known as hallucination.

Domain-Specific Knowledge: LLMs have broad but shallow knowledge. They may not have deep expertise in your specific company's policies, proprietary documentation, or niche technical domains.

No Source Attribution: Standard LLM responses don't tell you where the information came from. RAG enables citation of specific source documents, making answers verifiable.

Data Privacy: Some organizations cannot send proprietary data to train or fine-tune external models. RAG allows using private data at inference time without modifying the model itself.

How RAG Works Step by Step

A RAG system operates in two main phases: Indexing (preparing your knowledge base) and Retrieval & Generation (answering questions). Click any step card or diagram node to explore

Ingestion Phase Step 1
← click any node or step card to explore

Under The Hood

Tools:
LIVE PAYLOAD

                
Phase 1 · Indexing — offline
1 Document Loading Ingest raw documents from various sources — PDFs, web pages, databases, APIs, or text files. Each document loader handles format-specific parsing and text extraction. Show in diagram ↑ 2 Text Chunking Split documents into smaller, semantically meaningful pieces. Large documents are broken into chunks of typically 200–1000 tokens with some overlap between consecutive chunks to preserve context at boundaries. Show in diagram ↑ 3 Embedding Generation Convert each text chunk into a dense numerical vector (embedding) using an embedding model. These vectors capture the semantic meaning of the text in a high-dimensional space. Show in diagram ↑ 4 Vector Storage Store the embedding vectors along with the original text and metadata in a vector database (such as ChromaDB, Pinecone, or FAISS) optimized for fast similarity search. Show in diagram ↑
Phase 2 · Retrieval & Generation — online
5 Query Embedding When a user asks a question, embed the query using the same embedding model used during indexing. This converts the query into a vector in the same semantic space as the stored document chunks. Show in diagram ↑ 6 Context Construction & LLM Generation Search the vector database for the top 3–10 chunks most similar to the query, assemble them into a structured prompt, and send it to the LLM to generate a grounded answer. Show in diagram ↑ 7 Response Delivery Return the answer to the user, optionally with citations pointing to the specific source documents that were retrieved and used to ground the response. Show in diagram ↑

RAG Architecture Components

A RAG system consists of several interconnected components:

Document Loaders: Responsible for ingesting data from various sources. Frameworks like LangChain and LlamaIndex provide loaders for PDFs, CSVs, HTML, databases, and dozens of other formats.

Text Splitters: Break documents into chunks. Key parameters include chunk size (number of characters or tokens), chunk overlap (how much consecutive chunks share), and splitting strategy (character-based, sentence-based, or semantic).

Embedding Models: Neural networks that convert text into dense vectors. Popular choices include OpenAI's text-embedding-ada-002 (1536 dimensions), Cohere's embed models, and open-source options from Sentence Transformers (e.g., all-MiniLM-L6-v2 with 384 dimensions).

Vector Stores: Specialized databases for storing and searching embeddings. Options range from simple in-memory stores (FAISS) to managed cloud services (Pinecone) and self-hosted solutions (Qdrant, Weaviate).

Retrievers: Components that take a query and return relevant documents from the vector store. They may apply additional filtering, reranking, or hybrid search strategies.

LLMs: The generative models that produce the final answer. These can be API-based (GPT-4, Claude) or locally hosted (Llama, Mistral via Ollama).

Prompt Templates: Structured templates that combine the user's question with retrieved context into an effective prompt for the LLM.

Retrieval Evaluation

Evaluating the quality of a RAG system involves measuring both retrieval and generation quality:

Retrieval Metrics:
• Precision@K: Of the top K retrieved documents, how many are actually relevant?
• Recall@K: Of all relevant documents in the corpus, how many appear in the top K results?
• Mean Reciprocal Rank (MRR): How high does the first relevant document appear in the results?
• Normalized Discounted Cumulative Gain (NDCG): Measures ranking quality, giving higher weight to relevant documents that appear earlier.

Generation Metrics:
• Faithfulness: Does the generated answer stay true to the retrieved context?
• Answer Relevancy: Does the answer actually address the user's question?
• Context Relevancy: Were the retrieved documents actually relevant to the question?

Evaluation Frameworks: Tools like RAGAS, DeepEval, and TruLens provide automated evaluation of RAG systems across these dimensions.

Tip
Start by manually evaluating 20–50 question-answer pairs before investing in automated evaluation. This helps you understand your system's failure patterns.

Common RAG Failure Modes

Understanding common failure modes helps you diagnose and improve your RAG system:

Poor Chunking: Chunks that are too small lose context; chunks that are too large dilute relevant information with irrelevant text. Chunks that split mid-sentence or mid-paragraph can confuse the retriever and the LLM.

Embedding Model Mismatch: Using an embedding model that doesn't understand your domain's terminology. For example, a general-purpose embedding model may not accurately represent highly technical medical or legal terms.

Irrelevant Retrieval: The retrieved documents don't contain information needed to answer the question. This can happen when the question uses different terminology than the documents, or when the vector space doesn't capture the relevant semantic relationships.

Context Window Overflow: Retrieving too many documents can exceed the LLM's context window or dilute relevant information with less relevant chunks.

Lost in the Middle: Research shows that LLMs pay less attention to information in the middle of long contexts. Important information retrieved in the middle of the context may be effectively ignored.

Answer Not in Documents: The user asks a question that genuinely cannot be answered from the available documents. A good RAG system should recognize this and say so, rather than hallucinating.

Outdated Information: If documents aren't regularly updated, the RAG system may provide stale information while appearing authoritative.

Warning
The most common RAG failure is not a technical bug — it's poor chunking strategy. Spend time experimenting with chunk sizes and overlap before optimizing other components.

Key Takeaways

  • ✓RAG enhances LLMs by providing external knowledge at query time
  • ✓The pipeline has two phases: offline Indexing and online Retrieval & Generation
  • ✓Embeddings convert text into vectors that enable semantic similarity search
  • ✓Good chunking strategy is the most impactful factor in RAG quality
  • ✓Evaluate both retrieval quality and generation faithfulness separately