Retrieval Augmented Generation (RAG)
Retrieval Augmented Generation (RAG) combines the knowledge access capabilities of information retrieval systems with the natural language understanding and generation abilities of large language models. RAG creates an architecture that can access, process, and incorporate information from diverse external sources—including databases, documents, APIs, and structured knowledge—before generating responses, creating more accurate, up-to-date, and verifiable AI outputs.
At its core, RAG addresses the fundamental limitations of traditional LLMs: their knowledge is frozen at training time, they lack source citations, and they're prone to hallucinations (confidently stating incorrect information). By grounding responses in retrieved contextual information, RAG significantly reduces these issues while maintaining the fluent, contextual understanding that makes LLMs so powerful. This approach enables AI systems to reason over private data, specialized domain knowledge, and real-time information that wasn't part of their original training.
The implementation layer provides pre-built components that can be assembled into functional RAG systems with minimal custom code. These frameworks handle the core functionalities of document processing, retrieval, and generation, allowing developers to focus on application-specific needs rather than rebuilding foundational components.
Frameworks like LangChain and LlamaIndex represent the most efficient path to learning and implementing RAG systems. Rather than wrestling with low-level vector operations, embedding generation, and context management, these frameworks provide battle-tested abstractions that dramatically accelerate development:
Framework Comparison: LangChain vs. LlamaIndex
While there is significant overlap between these frameworks, they have different strengths and focus areas:
- LangChain excels at building modular, composable pipelines and workflows. Its strength lies in orchestrating end-to-end LLM applications with extensive integration capabilities across various tools, APIs, and services. LangChain provides robust abstractions for creating agents, managing complex chains of operations, and connecting different components in a flexible architecture.
- LlamaIndex (formerly GPT Index) specializes in sophisticated data indexing and retrieval mechanisms. Its core strength is in document processing, chunking strategies, and creating efficient data structures optimized for semantic search. LlamaIndex offers advanced query routing, transformation, and response synthesis specifically designed for knowledge-intensive applications.
Many production RAG systems leverage both frameworks together—using LlamaIndex for the indexing and retrieval components while employing LangChain for the broader application structure and integration with external systems.
Chunking strategies and embedding models are only half of a RAG system. Something has to physically hold the documents, and something has to hand the right pieces of them to the model at question time. That storage-and-retrieval layer is the part that decides whether your system is accurate, fresh, affordable, and auditable — and there are two fundamentally different ways to build it.
Approach 1 — Document store + tools (agentic retrieval): keep documents whole in blob storage, expose a small set of tools (list, search, read) to the model, and let it navigate the corpus the way a person would. Retrieval becomes an action the model takes, decided at inference time.
Approach 2 — Vector store (embedding retrieval): chunk everything ahead of time, embed each chunk, and store the vectors in an index. A similarity search runs before the model does, and the top matches are pasted into the prompt. Retrieval becomes a preprocessing step.
Neither is a strict upgrade over the other. They fail in different places, cost money in different places, and are correct for different kinds of questions. The rest of this section covers how each one is actually built, and how their characteristics compare.
Blob (object) storage — Amazon S3, Google Cloud Storage, Azure Blob Storage, Cloudflare R2, or self-hosted MinIO — is where the raw bytes of your corpus belong: the original PDFs, HTML captures, transcripts, spreadsheets, and images. It is cheap per gigabyte, effectively unlimited, extremely durable, and versioned, which makes it the natural source of truth that every other component points back to.
The critical property to understand is what blob storage cannot do. It is a key-value store: you can GET a key and you can LIST a prefix. You cannot ask it a question about content. That means your key layout is your index — the folder structure, naming convention, and accompanying metadata are the entire navigational surface that a retrieval tool (or an LLM) will have to work with.
Key layout: design paths so that a prefix listing is already a meaningful filter. A layout like tenant/source/domain/year/doc-id/version/file.pdf lets you scope a search to one customer, one data source, or one year with a single LIST call, and it makes per-prefix IAM policies possible.
Metadata catalog: pair the bucket with a row-per-document table in a database you can actually query (Postgres, DynamoDB, SQLite for small corpora). Store the object key, title, source URL, author, created/updated timestamps, MIME type, size, content checksum, tags, access-control labels, and pointers to derived artifacts. Filtering, sorting, and permission checks happen here — never by scanning the bucket.
Derived artifacts: ingestion is a pipeline, and each stage should be persisted next to the original: raw/report.pdf → extracted/report.md → chunks/report.jsonl. Parsing a PDF is slow and expensive; do it once, store the result, and let every downstream consumer read the clean text.
Access and lifecycle: serve documents through short-lived presigned URLs rather than making buckets public, scope IAM credentials per prefix, enable object versioning so a re-ingest never destroys history, and use lifecycle rules to move cold originals to archival tiers while keeping the extracted text hot.
What belongs where — a division of responsibility that keeps systems recoverable:
- Blob storage: the bytes. The only place the original document exists. Immutable and versioned.
- Metadata catalog: the facts you filter, sort, and authorize on. Cheap to query, easy to reindex.
- Vector store: embeddings plus a pointer back to the blob key and character offsets — an index, not a database of record.
The rule that saves you: never let the vector store be the only copy of anything. If you can rebuild the entire index from blob storage plus the catalog with one job, then changing your chunking strategy or your embedding model is a Tuesday afternoon. If you can't, it's a migration project.
Storing images and multimodal content: blobs handle this naturally — the image itself stays in the bucket, while the catalog holds a generated caption or description, and the vector store (if used) embeds that description. The model receives the caption during search and can fetch the actual image by key when it needs to look at it.
Implementation Example:
In the tool-calling approach, nothing is retrieved before the model runs. Instead the model is handed a small set of functions over your document store and decides for itself what to look at — searching, reading, noticing a reference, and following it, exactly the way a research assistant would work through a filing cabinet.
The minimal tool set covers four verbs, and almost every agentic RAG system is some variation of them:
list_documents(prefix, filters, limit)— browse the corpus structure. Returns titles, IDs, dates, and sizes so the model can orient itself before reading anything.search_documents(query, filters, limit)— find candidates by keyword, full-text (BM25), or metadata match. Returns snippets with document IDs, not whole files.read_document(doc_id, offset, limit)— read actual content, paginated. This is the tool people get wrong: without offset/limit, a single 300-page PDF blows the context window and ends the conversation.get_metadata(doc_id)— authorship, dates, source, version, related documents. Cheap, and often enough to rule a document in or out without reading it.
Structure is what makes this work. The model has no embeddings to lean on, so the organisation of the corpus is the retrieval quality. A manifest or table-of-contents file it reads first, descriptive file names, a short README per folder, consistent front matter (title, date, summary, tags) at the top of each document, stable IDs, and section anchors so reads can be targeted rather than sequential — these do the work that an embedding index would otherwise do.
The loop: the model emits a tool call, your code executes it against blob storage and the catalog, the result comes back as a tool result, and the model either calls another tool or answers. Two to five iterations is typical; a good system prompt telling the model what the corpus contains and how it is organised cuts that number substantially.
Advantages:
- No ingestion pipeline: no chunking, no embedding costs, no index to rebuild. Point the tools at the store and it works.
- Always fresh: the model reads the file as it exists right now. There is no staleness window between a document changing and answers changing.
- Exact lookups are exact: "the Q3 invoice from Acme Corp" or "error code E-4471" resolves by name or by literal match — precisely the queries where vector similarity is weakest.
- Multi-hop reasoning: the model can read a document, notice it cites another, and go fetch that one. A single vector search cannot follow a reference.
- Permissions at read time: authorization is checked when the tool executes, using the caller's identity — not baked into an index that was built with someone else's access.
- Whole-document context: the model reads a section in place, with its surrounding headings and caveats intact, instead of an isolated chunk stripped of its context.
- Auditable: the transcript shows exactly which files were opened and in what order. Debugging a bad answer means reading the tool calls.
Disadvantages:
- Latency and token cost: each tool call is a full round trip to the model. Three iterations of search-then-read can mean several seconds and tens of thousands of input tokens.
- Depends on model reasoning: a weaker model searches badly, gives up early, or reads the wrong file. The retrieval quality is only as good as the planner.
- Needs a good search tool or a bounded corpus: browsing works for hundreds or thousands of well-organised documents. Across ten million files with no search index, the model is lost.
- Non-deterministic: the same question can take different paths on different runs, which complicates evaluation and caching.
Best Use Cases: codebases and repositories, structured document libraries with clear naming, legal and financial records where the exact document matters more than a semantically similar one, corpora that change constantly, and any system where per-user permissions must be enforced at query time.
Implementation Example:
MCP (Model Context Protocol) is the emerging standard for exposing exactly this kind of tool surface. Rather than hand-wiring tools into one application, you run a server that publishes list/search/read over your document store, and any MCP-compatible client can use it. See the dedicated section at the end of this article.
The vector store approach front-loads the work. Documents are chunked and embedded during ingestion, and at query time the user's question is embedded with the same model and matched against the index by vector similarity. The top-k results are injected into the prompt before the model ever sees the question. One embedding call plus one index lookup, and retrieval is done.
What a record actually contains — four parts, and only the first is a vector:
- The vector: a fixed-length float array (384 to 3072 dimensions, depending on the embedding model) that positions this chunk in semantic space.
- The chunk text: the passage itself, stored alongside so retrieval doesn't require a second round trip to fetch what it just found.
- The metadata payload:
doc_id, source, date, author, tenant, tags, ACL labels — everything you need to filter on. - The pointer home: the blob key and character offsets, so the model (or a follow-up tool call) can always go read the full document the chunk came from.
Choosing a store falls into two camps. Dedicated vector databases — Pinecone, Qdrant, Weaviate, Milvus, Chroma, Vespa — offer the best index performance, hybrid search, and scaling story. Extensions to a database you already run — pgvector for Postgres, Elasticsearch/OpenSearch, Redis, MongoDB Atlas, sqlite-vec or FAISS for local work — avoid a new piece of infrastructure and keep vectors transactionally consistent with the rest of your data. For most teams under a few million chunks, pgvector in the Postgres you already operate is the right answer, and moving later is straightforward because you can rebuild the index from blob storage.
Index structures determine the speed/accuracy/memory triangle: flat (exact brute force, perfect recall, fine up to ~100k vectors), HNSW (graph-based, fast and high-recall, but the whole graph lives in RAM), and IVF-PQ (clustered plus compressed, enabling billion-scale search on modest hardware at some recall cost). These are covered in depth under Retrieval Mechanics later in this article.
Metadata filtering is where production systems live or die. Filtering after the similarity search (post-filter) can return nothing when the top-k all fail the filter; filtering before or during the search (pre-filter) is what you want, and support varies sharply between stores. For multi-tenancy, use separate collections or namespaces per tenant rather than relying on a metadata field — a filter bug leaks another customer's documents, a namespace boundary does not.
Operational realities that surprise people:
- The re-embedding problem: change your embedding model and every vector in the store is worthless. Old and new vectors are not comparable. Plan for full rebuilds — write into a new collection, verify, then swap — which is only painless if blob storage still holds every original.
- Idempotent upserts: derive chunk IDs deterministically (
sha256(doc_id + chunk_index)) so re-ingesting a document overwrites its chunks instead of duplicating them. Duplicated chunks silently degrade results by filling top-k with the same passage. - Deletes are a real workflow: when a document is removed or superseded, its chunks must be deleted by
doc_idfilter. Systems that forget this happily cite documents that no longer exist. - Freshness lag: there is always a window between a document changing and the index reflecting it — minutes for a good pipeline, days for a nightly batch. Users experience this as the system "lying" about recent changes.
- Memory is the cost driver: an HNSW index of 10 million 1536-dimension vectors is roughly 60 GB of RAM before overhead. Quantization (scalar or binary) cuts this dramatically for a few points of recall.
- Recall is tunable, not fixed:
ef_search(HNSW) andnprobe(IVF) trade latency for accuracy at query time. Ship with a measured evaluation set, not a default value someone copied from a blog post.
Advantages: sub-100ms retrieval regardless of corpus size, one predictable model call per question, genuine semantic matching (finds "terminated the agreement" when the user asked about "cancelling a contract"), scales to hundreds of millions of chunks, and deterministic enough to evaluate and cache.
Disadvantages: an ingestion pipeline to build and operate, embedding costs on every document and every re-index, staleness, weakness on exact identifiers and numbers, no ability to follow a reference or iterate, chunks that arrive stripped of surrounding context, and permissions that must be encoded into the index at build time.
Implementation Example:
The two approaches are not competing implementations of the same idea — they have genuinely different shapes, and comparing them dimension by dimension is the fastest way to know which one your problem needs.
Query style
- Tool calling: iterative and exploratory. The model can search, look at what came back, and refine — or read one document and then go find the one it cites.
- Vector store: single-shot. One query vector produces one ranked list. Follow-up requires a second application-level round.
Latency
- Tool calling: 2–10 seconds typical, because each iteration is a full model round trip. Latency grows with question difficulty.
- Vector store: 50–200 ms for retrieval, effectively constant no matter how large the corpus grows.
Cost model
- Tool calling: no ingestion cost at all; cost is paid per query in input tokens, and long documents are expensive to read.
- Vector store: heavy upfront cost (embed every chunk) plus ongoing index hosting/RAM; per-query cost is small and stable.
Freshness
- Tool calling: perfect. The tool reads the current file.
- Vector store: lagging by however long your ingestion pipeline takes — and silently wrong in the gap.
Exact matches (invoice numbers, error codes, product SKUs, names, dates)
- Tool calling: excellent. Literal search and direct addressing are what it does.
- Vector store: notoriously weak. Embeddings encode meaning, and identifiers have no meaning to encode — this is the main reason hybrid BM25 + vector search exists.
Conceptual/fuzzy questions ("what are our obligations if a customer churns early?")
- Tool calling: dependent on the model guessing the right search terms and file names.
- Vector store: excellent, and the entire reason embeddings were adopted — it matches paraphrases and synonyms the user never typed.
Scale
- Tool calling: comfortable to roughly thousands of well-organised documents; needs a strong search backend beyond that.
- Vector store: hundreds of millions of chunks with mature, well-understood operational patterns.
Setup and maintenance
- Tool calling: hours. Write four tools over storage you already have.
- Vector store: weeks. Chunking strategy, embedding pipeline, index tuning, re-embedding runbook, evaluation set.
Permissions
- Tool calling: enforced at read time with the caller's identity — the natural, safe model.
- Vector store: encoded into the index at build time. Workable with namespaces and pre-filters, but every permission change is an index concern.
Context quality
- Tool calling: the model sees passages in situ, with headings, caveats, and neighbouring sections intact.
- Vector store: isolated chunks, which is precisely why contextual retrieval and hierarchical chunking (covered elsewhere in this article) exist as mitigations.
Explainability
- Tool calling: the transcript is the audit trail — you can see every file it opened.
- Vector store: you get similarity scores, which tell you that something ranked highly but not why it was the right document.
Characteristic failure mode
- Tool calling: the model searches badly, gives up, and answers from its own parametric knowledge — confidently and without citations.
- Vector store: the right chunk exists but ranks 21st with
k=20, and the model answers from irrelevant context it was handed.
Choosing, in practice:
- Start with tools over blob storage when your corpus is under a few thousand documents, is well-named and well-structured, changes frequently, requires per-user permissions, or when exact-document lookup is the dominant query. It is dramatically cheaper to build and you will learn what your users actually ask before committing to a chunking strategy.
- Reach for a vector store when the corpus is large, queries are conceptual rather than nominal, latency budgets are tight (sub-second, user-facing chat), volume makes per-query token cost prohibitive, or content is unstructured enough that names and folders carry no information.
- Watch for the crossover signals: agentic retrieval that regularly needs more than four or five tool calls per question is telling you it needs a semantic index; a vector system where users keep asking for documents by name is telling you it needs tools.
- A useful sequencing heuristic: build the tool-based version first because it is a day's work, measure which questions it fails, and add the vector index for exactly those. The reverse order tends to produce a large embedding pipeline serving queries that a filename lookup would have answered.
Mature RAG systems stop treating this as a choice. The two approaches compose cleanly, because they occupy different layers: blob storage holds truth, the vector index provides fast semantic lookup over that truth, and tools are the interface the model actually uses — with semantic search as simply one of the tools available to it.
The layered architecture:
- Blob storage holds every original document plus its extracted text. Nothing else is a system of record.
- The metadata catalog holds the queryable facts — titles, dates, sources, tenants, ACLs — and drives every filter.
- The vector store indexes chunks of the extracted text, each carrying a
doc_id,blob_key, and character offsets pointing back to layer 1. It is fully rebuildable, so it can be thrown away and regenerated at will. - The tool layer exposes
semantic_search(backed by the vector index),search_documents(backed by full-text/BM25),list_documentsandread_document(backed by blob storage plus the catalog). The model picks the right instrument for the question it was asked.
Retrieve then verify is the pattern that makes the combination worth more than either half. Semantic search is used for what it is genuinely good at — finding candidates — and its results are treated as pointers rather than as answers. When a chunk looks relevant, the model calls read_document on the surrounding span and reads the passage in its real context before citing it. That single move eliminates the chunk-context problem that most of the mitigations in this article exist to work around: the chunk finds the page, the document supplies the truth.
Routing keeps the cost sane. Cheap classification of the incoming question — does it name a specific document, contain an identifier, or ask a conceptual question? — decides whether to go straight to a direct lookup, run a lexical search, run a semantic search, or hand the model the full toolset for an open-ended investigation. Most production traffic is repetitive, and routing the easy 80% to a single fast path leaves the latency and token budget available for the questions that genuinely need multi-step retrieval.
What this buys you: exact lookups stay exact, conceptual questions still get semantic matching, permissions are enforced when a document is read rather than when it was indexed, the index can be rebuilt from scratch after any change to chunking or embedding models, and every answer traces back to a versioned object in blob storage that you can actually go and open.
Effective RAG systems require breaking documents into manageable segments that balance retrieval accuracy with generation context. This segmentation process is crucial because LLMs have limited context windows, and retrieving overly long segments can dilute relevance.
The choice of segmentation strategy significantly impacts retrieval performance. Simple approaches offer implementation simplicity but may break contextual relationships, while advanced methods preserve semantic coherence at the cost of additional complexity. Many production RAG systems start with simple approaches and evolve toward more sophisticated segmentation as requirements become clearer.
Fixed-size chunking is the simplest segmentation approach, dividing text into uniform segments based on character count or token length. This straightforward method offers implementation simplicity but can break logical units of information:
Core Mechanism: Text is divided into chunks of predetermined size (typically 256-1024 tokens), regardless of content structure. When a chunk reaches the maximum size, the segmentation creates a new chunk and continues the process.
Advantages:
- Simplicity: Extremely easy to implement with minimal computational overhead
- Predictable Memory Usage: Consistent chunk sizes enable reliable resource allocation
- Uniform Processing: Standardized segment lengths simplify downstream handling
Disadvantages:
- Semantic Disruption: Often cuts through sentences, paragraphs, and conceptual units
- Context Loss: Related information may be arbitrarily split across different chunks
- Retrieval Inefficiency: Can lead to irrelevant sections being included in chunks
Implementation Example:
Best Use Cases: Fixed-size chunking works adequately for homogeneous content with uniform structure, initial prototyping, and when processing speed is prioritized over retrieval quality. It's often the starting point for RAG systems before more sophisticated approaches are implemented.
Paragraph-based segmentation uses natural paragraph breaks as chunk boundaries, respecting the author's original organization of ideas. This approach aligns with how humans structure information, typically grouping related thoughts within paragraph units:
Core Mechanism: The text is split at paragraph boundaries (usually identified by double line breaks or other formatting indicators). Paragraphs can be kept as individual chunks or combined until they approach a maximum size threshold.
Advantages:
- Content Coherence: Preserves logically related content as intended by the author
- Natural Boundaries: Uses existing document structure rather than imposing arbitrary divisions
- Implementation Simplicity: Relatively straightforward to detect paragraph breaks in most formatted text
Disadvantages:
- Variable Chunk Sizes: Can produce very short or very long chunks depending on document formatting
- Format Dependency: Requires reliable paragraph markers in the source document
- Inconsistent Length: May create inefficient embeddings for extremely short paragraphs
Implementation Example:
Best Use Cases: Paragraph-based segmentation works well for well-structured documents like articles, blog posts, and reports where paragraphs contain discrete ideas. It's particularly effective for content where paragraph boundaries meaningfully separate different concepts or topics.
Sentence-based segmentation creates chunks containing complete sentences, preserving the smallest coherent units of thought while controlling chunk size. This approach balances semantic integrity with size consistency:
Core Mechanism: Text is first split into individual sentences using natural language processing techniques (punctuation rules, language models, etc.). These sentences are then grouped into chunks up to a maximum size threshold, ensuring no sentence is split mid-way.
Advantages:
- Semantic Preservation: Maintains complete thoughts as expressed in sentences
- Flexible Grouping: Can combine related sentences while respecting maximum size limits
- Language Awareness: Properly handles various sentence structures and punctuation patterns
Disadvantages:
- Context Limitations: May separate closely related sentences across chunks
- Processing Overhead: Requires more sophisticated text analysis than simpler methods
- Inconsistency with Complex Sentences: Very long sentences can still create challenges
Fixed-Size with Overlap Enhancement:
Many implementations add overlapping content between adjacent chunks (typically 10-20% of chunk size). This technique helps maintain context across chunk boundaries by including the end of the previous chunk at the beginning of the next one. Overlapping is particularly valuable for sentence and paragraph-based approaches as it helps preserve the flow of ideas that might span chunk boundaries.
Best Use Cases: Sentence-based segmentation excels for question-answering applications where complete sentences provide important context. It's also effective for content with complex ideas developed across multiple short sentences, where preserving sentence integrity is more important than paragraph structure.
Recursive chunking uses a hierarchical approach to segmentation, attempting to split text at the highest-level boundaries first (chapters, sections) before progressively moving to finer-grained separators (paragraphs, sentences) as needed:
Core Mechanism: The algorithm tries to split text using a prioritized list of separators (e.g., section breaks, then paragraphs, then sentences). If using a high-level separator would create chunks that exceed the maximum size, it recursively attempts using the next separator in the hierarchy.
Advantages:
- Structure Awareness: Respects document hierarchy and logical organization
- Adaptive Granularity: Uses the most appropriate level of splitting for each section
- Balance: Maintains chunk size constraints while preserving as much context as possible
Disadvantages:
- Implementation Complexity: More sophisticated logic than simpler approaches
- Separator Dependency: Effectiveness depends on well-defined document structure
- Processing Overhead: Requires multiple passes through the text
Implementation Example:
Best Use Cases: Recursive chunking is ideal for complex, structured documents like technical documentation, research papers, and books with clear hierarchical organization. It's particularly effective when document structure varies throughout the content, requiring different segmentation approaches for different sections.
Semantic segmentation divides text into conceptually meaningful units rather than using arbitrary fixed-length chunks. This approach ensures that related information stays together, dramatically improving retrieval relevance:
Core Concept: Unlike mechanical splitting that might cut through important concepts, semantic segmentation identifies natural boundaries where topics shift. This preserves the coherence of ideas and prevents critical context from being fragmented across different chunks.
Implementation Approaches:
- Topic-Based Segmentation: Identifies shifts in subject matter using statistical methods or embedding similarity changes
- Hierarchical Segmentation: Creates nested segments from document → section → paragraph → sentence
- LLM-Guided Segmentation: Uses language models to identify logical breakpoints in content
Benefits for RAG:
- Improved Retrieval Precision: Returns complete concepts rather than partial information
- Reduced Context Pollution: Minimizes irrelevant content in retrieved passages
- Better Answer Generation: Provides LLMs with coherent units of information
In practice, semantic segmentation often yields significant improvements in RAG quality, particularly for complex documents where context preservation is critical. For technical documentation, research papers, or any content with interconnected concepts, semantic approaches prevent the fragmentation of ideas that can lead to incomplete or misleading retrieval results.
LLM-guided segmentation leverages the language understanding capabilities of large language models to identify natural conceptual boundaries in text. This approach treats chunking as an intelligent task rather than a mechanical one:
Core Mechanism: A language model is prompted to analyze the document and identify logical break points where conceptual shifts occur. These LLM-identified boundaries are then used to create chunks that align with the semantic structure of the content.
Advantages:
- Semantic Understanding: Identifies conceptual boundaries that might not align with formatting
- Content-Aware: Adapts to document style and subject matter automatically
- Higher Retrieval Quality: Creates chunks that align with how information is conceptually organized
Disadvantages:
- Computational Cost: Requires LLM inference, adding significant processing overhead
- Latency Concerns: Much slower than rule-based approaches, especially for large documents
- Consistency Challenges: May produce different results for similar content depending on model behavior
Best Use Cases: LLM-guided segmentation is particularly valuable for complex, nuanced content where conceptual boundaries don't align neatly with formatting. It excels with philosophical texts, creative writing, and documents where ideas develop across structural boundaries. Due to its cost, it's often reserved for high-value content where retrieval quality is paramount.
Embedding-based clustering segments text by analyzing semantic similarity patterns within the content. This data-driven approach groups related content based on meaning rather than structural features:
Core Mechanism: The document is first split into small units (sentences or paragraphs), which are then embedded into vector space. Clustering algorithms identify groups of semantically similar segments, which are combined to form coherent chunks up to a maximum size.
Advantages:
- Semantic Coherence: Groups content based on meaning rather than structural boundaries
- Adaptive to Content: Naturally identifies topic clusters regardless of formatting
- Conceptual Organization: Creates chunks that align with actual information relationships
Disadvantages:
- Computational Intensity: Requires embedding generation and clustering algorithms
- Parameter Sensitivity: Results depend heavily on clustering parameters and embedding quality
- Unpredictable Chunk Sizes: May create imbalanced chunks based on topic distribution
Best Use Cases: Embedding-based clustering excels for documents with diverse topics, research papers covering multiple concepts, and content where semantic relationships aren't clearly indicated by structure. It's particularly valuable for knowledge bases, encyclopedic content, and technical documentation where information relationships are complex.
Hierarchical chunking maintains multiple levels of segmentation simultaneously, creating a nested structure that enables multi-level retrieval. This approach preserves both document organization and detailed content:
Core Mechanism: The document is segmented at multiple granularity levels (document, section, paragraph, sentence) with appropriate metadata connecting the levels. Retrieval can then happen at different levels of specificity based on the query needs.
Advantages:
- Context Preservation: Maintains relationships between segments at different levels
- Flexible Retrieval: Enables retrieving both specific details and broader context
- Structural Awareness: Preserves document organization in the retrieval system
Disadvantages:
- Implementation Complexity: Requires sophisticated data structures and retrieval logic
- Storage Overhead: Creates multiple representations of the same content
- Query Complexity: Needs logic to determine appropriate retrieval level
Best Use Cases: Hierarchical chunking is ideal for complex, structured documents like technical manuals, educational content, and legal documents. It's particularly valuable when queries might require different levels of context—from specific details to broad overviews—or when the relationship between document sections is important for understanding the content.
Before diving into segmentation strategies, start by understanding the problem and context: What type of documents are you processing? How is your content structured? What retrieval goals are you prioritizing? These fundamental questions should guide your approach to chunking. For well-structured documents with clear sections and headings, structure-aware approaches like recursive or hierarchical chunking naturally align with the author's organization of ideas. Conversely, when working with unstructured text that lacks clear formatting, semantic approaches like embedding-based clustering or LLM-guided segmentation can uncover the hidden conceptual boundaries that formatting doesn't reveal.
The nature of your content significantly influences optimal chunking strategies. Technical or scientific material often contains dense, interconnected concepts that should remain unified, making semantic preservation crucial. If you're working with technical manuals where precise definitions matter, semantic or sentence-based chunking ensures key concepts stay intact. Narrative content, meanwhile, typically flows in thoughtfully constructed paragraphs, making paragraph-based approaches that respect the author's rhythm more appropriate. Reference materials might benefit from hierarchical approaches that maintain the relationships between concepts at different levels of specificity.
Practical constraints further shape your strategy selection. When building real-time applications where speed is critical, fixed-length chunks with overlap offer a pragmatic trade-off between processing efficiency and context preservation. For applications where accuracy is paramount and latency less concerning, more sophisticated approaches like LLM-guided segmentation can dramatically improve retrieval quality. For large-scale systems processing millions of documents, computational efficiency becomes increasingly important, potentially favoring simpler approaches with targeted enhancements for high-value content.
Rather than seeking the perfect strategy immediately, consider an evolutionary approach to implementation. Begin with basic approaches like fixed-size chunking with overlap or paragraph-based segmentation for initial prototyping. As you identify specific failure cases, implement more sophisticated approaches for particular content types or especially valuable documents. Many production systems ultimately employ hybrid approaches, applying different segmentation strategies to different document types within their collection. The key is continuous evaluation—regularly assessing retrieval quality and refining your approach based on real-world performance.
For most general-purpose RAG applications, start by asking: What's the smallest unit of text that can stand alone while preserving the information needed for accurate retrieval? A recursive chunking approach with significant overlap (15-20%) often provides an excellent balance of semantic preservation and implementation simplicity, respecting document structure while maintaining reasonable processing efficiency. From this foundation, you can iterate and enhance based on the specific requirements and challenges that emerge in your particular use case.
Contextual retrieval is the process of identifying and retrieving relevant information from a knowledge base or document corpus based on the specific context of a user's query. This step is crucial in RAG systems, as it ensures that the AI model has access to the most pertinent information when generating responses.
The retrieval process typically involves converting both the query and documents into vector representations using embeddings, allowing for efficient similarity search. The retrieved documents are then used to provide context for the language model, enabling it to generate more accurate and relevant responses.
Vector embeddings are the foundation of effective RAG systems - they determine what information gets retrieved when a user asks a question. Better embeddings mean better answers.
Consider this example: A user asks "What are the side effects of aspirin?" With poor embeddings, the system might retrieve passages about "effects of medication on the side of the body" or generic drug information instead of specific aspirin side effects. The LLM can only work with what's retrieved, so even the best model will produce irrelevant or incomplete answers with bad embeddings.
Modern embedding models differ in accuracy, speed, and resource requirements. General-purpose embeddings work adequately for common topics but struggle with specialized domains like medicine or law. Domain-specific fine-tuning can dramatically improve results by teaching the embeddings to understand specialized terminology and concepts.
Embedding dimensionality represents a key tradeoff - higher dimensions capture more meaning but require more storage and processing power. Lower dimensions are faster but may miss subtle connections between concepts.
While many focus on improving language models or retrieval algorithms, embedding quality often provides the biggest performance gains for RAG systems. No matter how sophisticated your other components are, they can't work with information that wasn't properly retrieved in the first place.
Retrieval systems find relevant information from potentially massive document collections. Their efficiency and effectiveness determine both system performance and response quality.
Finding exact nearest neighbors in high-dimensional spaces becomes computationally prohibitive at scale. ANN algorithms make this practical:
Hierarchical Navigable Small World (HNSW):
- Creates multi-layer graphs connecting vectors by similarity
- Enables logarithmic-time search by navigating from distant to close neighbors
- Balances speed and accuracy through adjustable parameters
Inverted File Index with Product Quantization (IVF-PQ):
- Partitions vector space into clusters for coarse filtering
- Compresses vectors through product quantization to reduce memory requirements
- Enables billion-scale vector search on standard hardware
Other Common Approaches:
- Locality-Sensitive Hashing (LSH): Hash-based techniques for approximate matching
- Random Projection Trees: Tree structures for partitioning vector space
ANN algorithms involve trade-offs between search speed, memory usage, and result accuracy that must be tuned based on application requirements.
Combining multiple retrieval approaches can overcome the limitations of individual methods:
Sparse-Dense Hybrid Retrieval:
- Dense retrieval: Vector similarity captures semantic relationships
- Sparse retrieval: Keyword matching (BM25) captures exact terminology
- Hybrid approaches combine both signals for improved relevance
Reciprocal Rank Fusion (RRF):
- Merges results from multiple retrieval methods
- Weights items based on their rank in each result set
- Provides robust performance across diverse query types
ColBERT and Late-Interaction Models:
- Represent texts as sets of contextualized token embeddings
- Perform fine-grained matching between query and document terms
- Balance computational efficiency with matching precision
Fusion approaches provide robustness against the weaknesses of individual retrieval methods, handling both semantic concepts and specific terminology effectively.
User queries often don't match document phrasing, creating retrieval challenges that can be addressed through query transformation:
Pseudo-Relevance Feedback (PRF):
- Performs initial retrieval to find potentially relevant documents
- Extracts key terms from these documents to expand the original query
- Creates a more comprehensive query that matches relevant document terminology
Neural Query Expansion:
- Uses LLMs to generate alternative phrasings of the original query
- Creates multiple search queries from a single user question
- Improves recall by covering different ways information might be expressed
Hypothetical Document Content (HyDE):
- Uses an LLM to generate an ideal answer document
- Retrieves real documents similar to this hypothetical document
- Bridges the query-document vocabulary gap effectively
These techniques transform user questions into more effective retrieval queries, significantly improving the ability to find relevant information even when expressed differently.
Augmented generation is the process where an AI combines retrieved information with its own knowledge to create accurate, helpful responses. It's like giving the AI research materials before asking it to write an essay.
This critical phase takes the relevant information found during retrieval and carefully incorporates it into the AI's response. Through special prompting techniques and control mechanisms, the AI can produce answers that are both factually accurate and naturally written.
Good prompting helps AI use the information it finds when creating answers:
In-Context Learning (ICL):
- Puts retrieved information directly in the prompt as examples
- Tells the AI to use these examples when answering
- Makes context look different from instructions
- Example: 'Here are some passages about diabetes. Use this information to answer the question: What are the common symptoms of Type 2 diabetes?'
Chain-of-Thought (CoT) Prompting:
- Asks the AI to think step-by-step through its reasoning
- Directs the AI to examine the retrieved facts before drawing conclusions
- Makes answers more accurate for hard questions that need multiple thinking steps
- Example: 'First, review the information about climate change in these documents. Then, identify the key factors mentioned. Finally, explain how these factors contribute to rising sea levels.'
Retrieval-Augmented Prompting Patterns:
- Context segregation: Clearly separating found information from instructions (Example: 'CONTEXT: [retrieved documents] QUESTION: [user query]')
- Source attribution: Keeping track of where information came from for citations (Example: 'According to document #2 from the company handbook...')
- Relevance assessment: Asking the AI to first check if the information is helpful before using it (Example: 'Review these passages and determine which are relevant to the question before answering')
How you structure your prompt greatly affects how well the AI uses the retrieved information in its final answer.
Techniques to ensure generated content remains faithful to retrieved information:
Constrained Decoding:
- Entity Grounding: Ensuring mentioned entities appear in retrieved context
- Citation Alignment: Generating inline citations linked to specific sources
- Factual Anchoring: Requiring statements to be traceable to retrieved content
Factual Consistency Checks:
- NLI-Based Verification: Using natural language inference to verify claims
- Self-Consistency: Generating multiple responses and identifying consensus
- Uncertainty Expression: Encouraging models to express uncertainty when information is ambiguous
Two-Stage Generation:
- First extracting relevant facts from retrieved documents
- Then synthesizing these facts into coherent responses
- Separating information extraction from text generation
These controls balance the creative capabilities of LLMs with the factual constraints of retrieved information, reducing hallucination while maintaining fluent, natural responses.
RAG systems can be substantially enhanced through strategic optimizations and extensions that dramatically improve performance, expand functional capabilities, and adapt to specialized domains with precision. These advanced techniques help make RAG systems faster, more accurate, and better suited for specific use cases.
Making RAG systems faster and more cost-effective is crucial for real-world applications. Here are key approaches to achieve this:
Search Parameter Tuning:
- efConstruction: Controls how carefully the system builds its search index - higher values create better indexes but take longer
- efSearch: Determines how thoroughly the system searches - higher values find more relevant results but take more time
- M: Sets how many connections each data point has in the search network - more connections improve accuracy but use more memory
Vector Compression Techniques:
- Scalar Quantization: Simplifies vector numbers (like rounding 3.14159 to 3.1) to save space while keeping most accuracy
- Product Quantization: Breaks vectors into smaller pieces that can be stored more efficiently using lookup tables
- Dimensionality Reduction: Keeps only the most important information in vectors, like compressing a photo while preserving the main details
Model Optimization:
- Knowledge Distillation: Teaches smaller, faster models to mimic the behavior of larger, more powerful ones
- Smaller Models: Uses compact models like DistilBERT that require less computing power while maintaining good performance
- Quantization: Converts model calculations to use simpler number formats that process faster on computers
Smart Storage Strategies:
- Query Result Caching: Remembers answers to common questions so they don't need to be calculated again
- Embedding Caching: Stores already-calculated vector representations to avoid repeating work
- Multi-Tier Retrieval: Uses quick, simple filters first before using more resource-intensive methods
These optimizations can reduce computing costs by 10-100 times while maintaining 95% or more of the original quality, making RAG systems practical for everyday applications.
Expanding RAG beyond just text allows systems to work with images, audio, and other types of content. This creates more versatile and powerful applications:
Connecting Different Content Types:
- CLIP: A system that understands both images and text in the same way, letting you search for images using words or find text related to images
- ImageBind: Takes this further by connecting six different types of content (like text, images, audio) in a unified way
- LLaVA/GPT-4V: Advanced systems that can look at images and understand them in context with text
Unified Storage Approach:
- Creates a single storage system where different types of content (text, images, etc.) can be searched together
- Allows searching across different content types with consistent results (like finding images related to text queries)
- Supports documents that mix text, images, and other media while keeping their relationships intact
Processing Mixed-Media Documents:
- Extracts text from visual elements like charts, graphs, and diagrams
- Creates text descriptions of visual content so it can be searched and understood by the system
- Preserves important relationships between text and nearby images or graphics
Creating Rich Responses:
- Generates answers that include both text and relevant visuals when appropriate
- Selects helpful images or diagrams to better explain concepts
- Creates charts or visualizations based on retrieved information to make it easier to understand
These multimodal capabilities make RAG systems 40-60% more effective in fields that rely heavily on visual information, such as medicine, design, scientific research, and media content.
Customizing RAG systems for specific fields like medicine, law, or technical support can dramatically improve their performance for specialized tasks:
Specialized Training:
- Medical RAG: Adapts to medical terminology and connects with healthcare knowledge databases for accurate clinical information
- Legal Document Analysis: Learns to understand complex legal language and how legal documents reference each other
- Technical Support: Focuses on understanding step-by-step procedures and specific troubleshooting approaches
Custom Document Processing:
- Creates specialized ways to break down documents based on how information is structured in specific fields
- Identifies important field-specific terms and concepts that general systems might miss
- Preserves important details in specialized document formats that would otherwise be lost
Learning from Expert Feedback:
- Improves through training on examples reviewed and approved by domain experts
- Focuses on measures of success that matter in the specific field, not just general relevance
- Aligns responses with professional standards and best practices in the specialized domain
Knowledge Framework Integration:
- Combines AI retrieval with structured knowledge about how concepts in the field relate to each other
- Enables more sophisticated reasoning about complex relationships between ideas in the domain
- Provides deeper context by connecting information to established knowledge in the field
With these specialized adjustments, RAG systems can achieve 85-95% agreement with human experts in specialized fields, making them valuable professional tools rather than just general-purpose assistants.
Cutting-edge RAG designs go beyond basic approaches to create systems with significantly enhanced capabilities:
Multi-Step Retrieval:
- Coarse-to-fine approach: First quickly identifies potentially relevant information, then carefully examines only those candidates
- Smart ranking systems: Uses specialized models to sort results by true relevance rather than just keyword matching
- Iterative searching: Refines searches based on initial findings, similar to how humans adjust their research approach
Self-Improving RAG:
- Creates systems that decide when to use their built-in knowledge versus when to look up external information
- Implements internal verification systems that evaluate retrieved context relevance and reliability
- Deploys adaptive retrieval mechanisms that dynamically adjust strategies based on query complexity
Agent-Based RAG:
- Creates autonomous systems that decompose complex queries into structured retrieval plans
- Integrates specialized tools that combine retrieval with computational processing and external API calls
- Implements recursive reasoning frameworks with strategic information gathering and hypothesis testing
Long-Context Adaptation:
- Implements specialized attention mechanisms for efficiently processing 100K+ token contexts
- Deploys hierarchical information organization systems for optimal context utilization
- Applies sophisticated coherence-preserving techniques across extensive retrieved information sets
These advanced architectures represent state-of-the-art RAG implementations that achieve 30-50% improved performance on complex tasks, transforming basic QA systems into sophisticated research assistants capable of handling intricate information needs.
MCP (Model Context Protocol) enables LLMs to utilize tools effectively, allowing them to access external data and perform actions beyond their training.