agentic.dev

RAG Pipeline Architecture: Chunking, Embedding and Retrieval

Published 2026-02-21

RAG Pipeline Architecture: Chunking, Embedding and Retrieval

The promise of Large Language Models (LLMs) is immense, but their inherent limitation – a fixed knowledge cutoff – quickly becomes a bottleneck in practical applications. Training LLMs from scratch on constantly updating information is prohibitively expensive and slow. Fine-tuning helps, but it’s still resource intensive and prone to catastrophic forgetting. That’s where Retrieval-Augmented Generation (RAG) comes in. RAG isn’t just a workaround; it’s a fundamental architectural pattern for building intelligent, knowledge-aware agents. In 2026, RAG is the standard for connecting LLMs to dynamic data sources, and understanding its nuances – particularly the chunking, embedding, and retrieval stages – is crucial for any serious AI developer. I’ve spent the last year deep in the RAG weeds, building everything from internal knowledge bases for autonomous agents to customer-facing AI assistants, and I’m going to share what I’ve learned – the good, the bad, and the surprisingly complex.

The Core RAG Loop: A Deep Dive

At its heart, RAG is a simple loop: a user query is received, relevant documents are retrieved from a knowledge base, those documents are combined with the query, and then fed to the LLM to generate an answer. The magic – and the difficulty – lies in making that “retrieval” step truly effective. It’s not enough to just throw everything into a vector database and hope for the best. The quality of the retrieved context directly dictates the quality of the LLM's response. We’re heavily reliant on the performance of the three stages I’ll cover: chunking, embedding, and retrieval.

Let’s start with an example. Imagine building a RAG pipeline for a company’s internal documentation. A user asks, “What is the company policy on remote work expenses?” The ideal RAG pipeline should find the relevant policy document, extract the section on remote work expenses, and present that information to the LLM. A poorly designed pipeline might return documents about vacation time, office supplies, or even competitor analysis – leading to a confused or incorrect response.

Chunking Strategies: More Than Just Splitting Text

The first step in preparing your knowledge base is chunking – breaking down your documents into smaller, manageable pieces. This seems trivial, but it’s where many RAG implementations stumble. A naive approach – simply splitting text by a fixed number of characters or lines – often results in chunks that are either too large (losing focus) or too small (lacking context).

In 2026, we've moved beyond basic splitting. Here are some strategies I’ve found particularly effective:

  • Semantic Chunking: This involves identifying natural breaks in the text based on meaning. Tools like sentence-transformers (still a powerhouse, even with newer models) can be used to calculate sentence embeddings and identify boundaries where semantic similarity drops significantly. This is more computationally expensive, but yields far better results.
  • Recursive Character Text Splitter (LangChain): This is a good starting point. It attempts to split on a hierarchy of characters (e.g., paragraphs, sentences, words) until chunks reach a desired size. It’s relatively easy to implement and provides a decent balance between simplicity and effectiveness.
  • Metadata-Aware Chunking: This is critical for structured documents. Instead of purely text-based splitting, incorporate document metadata (e.g., headings, sections, tables) to create logically coherent chunks. For example, you might chunk an entire section of a technical manual based on its heading.
  • Here’s a simple Python example using LangChain’s RecursiveCharacterTextSplitter:

    from langchain.text_splitter import RecursiveCharacterTextSplitter
    

    text = """ This is a long document about remote work policies. It covers eligibility, expense reimbursement, and security guidelines.

    Eligibility

    Employees must meet certain criteria to be eligible for remote work. These include a minimum performance rating and a suitable home office setup.

    Expense Reimbursement

    The company will reimburse employees for reasonable expenses incurred while working remotely, such as internet access and office supplies. Specific limits apply.

    Security Guidelines

    Employees working remotely must adhere to strict security guidelines to protect company data. """

    text_splitter = RecursiveCharacterTextSplitter( chunk_size=200, chunk_overlap=20, length_function=len, )

    chunks = text_splitter.split_text(text)

    for i, chunk in enumerate(chunks): print(f"Chunk {i+1}:\n{chunk}\n---")

    Tradeoffs: The ideal chunk size depends heavily on the content and the LLM you're using. Generally, 200-500 tokens is a good range to start with, but experimentation is key. Larger chunks provide more context but can dilute the relevant information. Smaller chunks are more focused but may lack the necessary context for the LLM to understand. Chunk overlap (the chunk_overlap parameter above) helps to mitigate information loss at chunk boundaries, but increases the size of your vector database.

    Embedding Models: Choosing the Right Representation

    Once you have your chunks, you need to convert them into numerical vectors – embeddings – that capture their semantic meaning. These embeddings are then stored in a vector database for efficient similarity search. The choice of embedding model is paramount.

    In 2026, the landscape has shifted. While OpenAI's text-embedding-ada-002 remains a popular choice due to its cost-effectiveness and performance, several open-source alternatives are now competitive, and often preferable for data privacy and control.

  • Sentence Transformers (all-mpnet-base-v2): Still a fantastic all-rounder. It’s readily available, performs well on a variety of tasks, and can be run locally.
  • E5 Models (EMBEDDING-WEAVELarge-v2): Developed by Microsoft, E5 models are specifically trained for retrieval tasks and often outperform Sentence Transformers in benchmark tests. They require more computational resources.
  • Voyage AI Embeddings: A newer player, Voyage AI’s embedding models are claiming state-of-the-art performance, particularly in long-context retrieval. They are a commercial offering, but the results are impressive.
  • The key is to choose a model that is aligned with your data and your retrieval goals. For example, if your knowledge base consists primarily of technical documentation, you might want to consider a model that has been specifically trained on technical text.

    Cost Considerations: Embedding costs can add up quickly, especially for large knowledge bases. OpenAI charges per token embedded, and even relatively inexpensive models can become costly at scale. Self-hosting open-source models requires significant GPU infrastructure and engineering effort. We found that optimizing chunk size before embedding dramatically reduced our embedding costs.

    Retrieval Strategies: Beyond Simple Similarity Search

    Simply storing embeddings in a vector database and performing a nearest neighbor search isn’t enough. You need to refine your retrieval strategy to ensure that you’re getting the most relevant context.

  • Similarity Search (Cosine Similarity): This is the foundation of most RAG pipelines. It measures the cosine of the angle between the query embedding and the document embeddings, identifying documents that are semantically similar.
  • Metadata Filtering: Combining similarity search with metadata filtering allows you to narrow down the results based on specific criteria. For example, you might filter by document type, author, or date. This is essential* for large, diverse knowledge bases.
  • Hybrid Search: Combining vector search with traditional keyword search (using tools like BM25) can improve recall. Keyword search can identify documents that contain specific terms that might be missed by vector search.
  • Re-ranking: After retrieving an initial set of documents, re-ranking them using a more sophisticated model (like a cross-encoder) can further improve relevance. Cross-encoders consider the query and the document together* when calculating relevance, providing more accurate results. This is compute-intensive, but often worthwhile.
  • Query Transformation: Modifying the user query before embedding it can significantly improve retrieval performance. Techniques include query expansion (adding related terms) and query rewriting (rephrasing the query for clarity). Tools like HyDE (Hypothetical Document Embeddings) are becoming popular for generating more effective queries.
  • Vector Database Choice: The choice of vector database is also important. Pinecone is a fully managed option that offers excellent performance and scalability, but it can be expensive. WeaveAI and ChromaDB are popular open-source alternatives. Milvus is also gaining traction, particularly for applications that require high throughput. In 2026, many companies are opting for cloud-native vector databases offered directly by AWS (OpenSearch), Google Cloud (Vertex AI Vector Search), and Azure (Azure AI Search) for tighter integration with their existing infrastructure.

    Key Takeaways

  • Chunking is King: Don't underestimate the importance of effective chunking. Experiment with different strategies to find what works best for your data. Semantic chunking and metadata-aware chunking are significant upgrades over naive splitting.
  • Embedding Model Selection Matters: Carefully consider the tradeoffs between cost, performance, and data privacy when choosing an embedding model. Open-source options are now highly competitive.
  • Retrieval is an Iterative Process: Start with simple similarity search and gradually add more sophisticated techniques like metadata filtering, hybrid search, and re-ranking. Continuously monitor and evaluate your retrieval performance to identify areas for improvement.

  • Published on agentic.dev.