Published: September 2024
Updated: August 2026
TL;DR: RAG (Retrieval-Augmented Generation) is an architecture that grounds a large language model in your own data by retrieving relevant information before it generates an answer. It makes GenAI responses specific and accurate without exposing sensitive data. You build it by preparing, chunking, and vectorizing your data. Most databases now support vectors natively, so a separate vector database is often unnecessary.
Retrieval-Augmented Generation (RAG) is a technique that combines the power of large language models with external knowledge retrieval that allows you to take generative AI from generalized to specific. It’s the pattern that orchestrates the AI to do something very focused without jeopardizing data security.
Generative AI (GenAI) is revolutionizing industries, enabling businesses to automate tasks, generate content, and create personalized experiences at scale. But to unlock its full potential, you need the right architecture. This blog will help you understand what RAG really is, the steps involved, why RAG architecture matters, and how to build a system that works seamlessly while avoiding the most common mistakes.
What is GenAI and RAG, and Why Do They Matter?
At its core, Generative AI (GenAI) refers to artificial intelligence systems that produce content based on learned data patterns. This content can range from text to images to music, depending on the model’s training. While powerful on its own, GenAI often struggles with precision when tasked with delivering highly specific or specialized content it wasn’t previously trained on.
That’s where RAG comes into play. It augments the generative capabilities of AI by allowing it to pull specific, relevant data points from external sources to create a targeted response. This concept is called “grounding” and refers to the process of anchoring the language model’s outputs to factual, retrieved information based on specific, relevant facts from the retrieved information rather than solely on its pre-trained knowledge.
This architecture combines a pre-trained LLM model with your organization’s data or “special sauce” (see what I did there with schema sauce?) ensures the AI can retrieve detailed information that isn’t out in the public, making it far more accurate for business use cases like customer service, finding the right HR document, or summarizing an email chain.
In short, while GenAI produces content, RAG ensures that content is specific, relevant, and useful without letting your sensitive data out into the wild. If you want your AI to provide tailored results rather than generalized responses, RAG is essential.
Best Practices for Building a RAG System
Building a successful RAG system isn’t just about deploying a large language model and hoping for the best. It requires a structured approach to ensure scalability, performance, and security.
1. Collect, Clean, Prep Your Data
Gather relevant documents, databases, or other data sources you want to ground your responses in. Remove irrelevant information, formatting issues, or noise. Correct errors or inconsistencies in the data. For PDFs, images, or other non-plain text formats, extract the text content (if necessary).
2. Chunk Your Data
Whether it is structured data (rows, columns from tables) or unstructured data (media or documents), it needs to be chunked up into bite sized pieces the LLM can ingest.
Chunking is the process of dividing larger pieces of information into smaller, manageable units. Text is split into chunks based on sentence structure, token limits, or semantic meaning. Each chunk is then tokenized by the embedding model (converts into tokens) then the embedding model encodes the tokenized chunk into a vector representation for storage in a vector database. This is crucial for RAG systems to effectively retrieve and process information.
Ways to define chunk sizes:
- Fixed-Length Token Count: chunks are defined based on a fixed number of tokens to ensure compatibility with embedding model limits.
- Fixed-Length Character Count: chunks are split based on predefined character limit (e.g. 1000 characters). This is simpler but less accurate since word and token lengths will vary.
- Sentence-Based Chunking: text is split at sentence boundaries using Natural Language Processing (NLP) tools to preserve meaning BUT may create incosistent chunk sizes.
- Paragraph-Based Chunking: keeps related sentences together for better context and works well for documents like articles or reports that are well organized.
- Recursive Overlapping Sliding Windows (BEST PRACTICE): uses overlapping chunks (e.g. 256 tokens per chunk with 50-token overlap) to help retain context between adjacent chunks and improve retrieval accuracy. This is useful for avoiding loss of meaning when context is split between two chunks.
- Semantic Chunking: uses NLP techniques to split content based on meaning. This sounds great, but is more computationally expensive to run and requires an LLM-based chunking strategy like sentence similarity.
Different models often require different chunk sizes due to their varying context window sizes. Here’s why:
- Context Window: The maximum number of tokens a model can process at once. It includes both the input (prompt) and output (response) tokens.
- Varies by model: older models like BERT handled 512 tokens while modern frontier models handle 128K to over 1M tokens.
- Chunk Size Considerations:
- Should be smaller than the model’s context window.
- Leaves room for prompts and generated text.
- Size chunks for retrieval precision, not window limits. Modern windows are huge but small focused chunks still retrieve best.
Context windows are measured in tokens, not words. A token is a piece of text that the model treats as a single unit. It can be a word, part of a word, or a character, depending on the tokenization method used.
Examples:
- In simple word-based tokenization: “hello” and “world” are separate tokens.
- In subword tokenization: “unhappy” might be split into “un” and “happy”.
- Punctuation marks are often separate tokens.
Purpose:
- Tokens help convert text into a format that embedding models can process to turn into vector representations.
- They serve as the basic units for model input and output.
Once you have defined the parameters and your data is sufficiently chunked into the right amount of tokens to fit the context window, it’s time for the next step UNLESS you want to get really fancy.
After the data is chunked, you can add meta-data to the chunks (e.g. data source, date, category) to give even more properties for the LLM to make inferences from. This meta-data lives alongside the vector embeddings which we will dive into the next step.
3. Vectorize Your Data
To make RAG work, your data needs to be in a format that the language model can understand. This is done by converting the raw data into vector embeddings, which represent the data in a mathematical form the AI can process. Ensuring compatibility between your vectorized data and the model is critical. Mismatched formats will prevent the system from functioning properly.
Vector embeddings are like giving objects special number tags that describe their features. These tags help computers organize and compare things by how similar their features are, just like you might group toys based on their color, size, and shape.
You use an Embedding Model to process the chunks into vector embeddings. The choice of embedding model should be compatible with your retrieval system and somewhat aligned with your LLM, but it doesn’t necessarily have to be the same as your LLM.
Common choices include models like:
- Sentence transformers (e.g., all-MiniLM-L6-v2)
- OpenAI’s text-embedding-3 family
- Open source options like BGE and Nomic Embed on Hugging Face
The LLM itself is typically not used for creating these embeddings, as it would be computationally expensive and unnecessary.
In summary: You choose an embedding model, then use it to vectorize your chunks. These vectors are what get stored and later used for retrieval when working with your chosen LLM in the RAG system.
Yes… the amount of different types of models, new terms, and components is dizzying! Don’t worry… we are ALMOST there 🙂
4. Choose Your Database (Plot Twist: It Probably Already Does Vectors)
Back when I first wrote this post, “vector database” was a product category. You picked a specialized tool or you were doing it wrong.
That era is OVER. Vector is now just a data type. Even Db2 LUW, the distributed workhorse that has been running enterprise workloads for 30+ years, added a native VECTOR type and DiskANN indexing. SQL Server 2025 ships vectors in the box. Postgres has pgvector. Everyone brought vectors to the party.
So the question is no longer “which vector database should I buy?” It’s “should I use a purpose-built vector database, or the database I already have?”
Purpose-built vector databases (Pinecone, Milvus, Qdrant, Weaviate) do one thing all day long: store and search embeddings at massive scale. If your app IS vector search, with billions of embeddings and strict latency targets, these still earn their keep.
General-purpose databases with vector support (SQL Server, Postgres, Cosmos DB, MongoDB, Db2 LUW, and more) let your embeddings live right next to your business data. One security model. One backup strategy. One less system to babysit. For most enterprise RAG apps, this is the winning move.
Full disclosure: I have lived on both sides of this fence. I worked on Astra DB at DataStax and was involved with Db2 at IBM. No horse in the race anymore. Just opinions built from seeing what actually works.
The variables that actually differentiate databases now:
- Where your data already lives. Moving data to a separate system adds pipelines, latency, and security headaches. Staying put is a feature.
- Scale and latency requirements. Billions of vectors with millisecond targets? Purpose-built. Millions of vectors for internal RAG? Your existing database is fine.
- Hybrid search quality. Combining vector similarity with keyword search and metadata filters is where real retrieval quality comes from.
- Operational model. Managed vs self-hosted, and whether your team wants another system to run.
- Cost. A new SaaS bill versus a feature you already pay for.
Run a small proof of concept before committing. Test with YOUR data and YOUR queries. The benchmark that matters is yours.
5. Index Creation
Now that your data is in a readable format for an LLM, it’s time for indexing! The purpose is the same as indexing in Excel or a traditional database: speed up retrieval so you find the right data fast.
My favorite way to explain it is with Excel. VLOOKUP scans row by row from the top until it finds a match. Brute force. Fine for small tables, painfully slow for big ones. INDEX-MATCH is smarter. It uses binary search on sorted data and only touches the columns it needs. Same answer, way less work.
Vector indexes apply that same leap to embeddings. Comparing a query against millions of vectors one by one is too slow. An index organizes vectors into a searchable structure so the system finds the nearest neighbors without checking every single one.
Key concepts:
- Approximate nearest neighbor (ANN) search: trades a tiny bit of accuracy for massive speed gains. This is the backbone of vector indexing, and DiskANN and HNSW are the big names here.
- Clustering: groups similar vectors together so searches skip irrelevant neighborhoods.
- Quantization: compresses vectors into fewer bits to save memory and speed up comparisons.
The good news? You rarely build indexes by hand anymore:
- Most modern vector databases create and update indexes automatically when you ingest data.
- Many support real-time indexing, so data is searchable the moment it lands.
- Some tools accept raw text and handle chunking, embedding, and ingestion on the fly.
Think of an index as a smart phonebook for your embeddings. Your RAG system flips straight to the right page instead of reading every entry. Faster retrieval, happier users.
6. Quality Check
Just like it’s crucial to have quality data, it’s also crucial to have strong quality checks in place for the data you have pulled into your vector database and indexed. You can do this by:
- Manual review of retrieved results for sample queries.
- Automated testing with predefined query-result pairs.
- Fine-tune chunk size if context is insufficient or redundant.
- Experiment with different embedding models if semantic matching is poor.
- Adjust index parameters (e.g., number of clusters, search depth) for better precision/recall balance.
This is typically the “uh-oh” point for many projects as you realize your data isn’t as clean as you expected it to be. Common issues that get surfaced are:
- Inconsistent formatting: Even within the same dataset, you might find multiple date formats, inconsistent spacing, or varying representations of the same information
- HTML artifacts: Remnants of web scraping like , incomplete tag stripping, or encoded characters that slipped through
- Duplicate content with slight variations: The same information presented with minor differences that creates nearly identical vectors and noise in your results
- Missing context: Chunks that made sense in the original document but lose critical context when separated
- OCR errors: If dealing with scanned documents, misread characters or formatting that creates nonsensical content
- Boilerplate pollution: Headers, footers, and standard disclaimers that add noise to your vector space
- Versioning confusion: Multiple versions of the same document without clear indicators of which is most current
- Inconsistent metadata: Missing or contradictory tags, categories, or other metadata that should help filter results
The challenge is that these issues often don’t become apparent until you’re actually testing your retrieval results, and fixing them usually requires revisiting your entire data processing pipeline. This can be a MAJOR bummer, but you aren’t alone! Many organizations face these issues. Overcoming them will take dedicated teamwork.
7. Choose Your Language Model
We have made it to the point where we can finally start using in-vogue terms like LLM! What does an LLM mean and what choices do we have at this point?
Large Language Models (LLMs) are models like GPT, Claude, and Gemini. These are massive models with 100B+ parameters. They are excellent at complex reasoning, understanding context, and generating human-like responses (not sentient!). LLMs are typically accessed through cloud APIs and have higher latency and cost compared to smaller models. Given they are API based it is easy to spin up the keys and implement the model to your RAG architecture.
Best for: complex reasoning tasks, creative content generation, nuanced understanding
Small Language Models (SLMs) are models like Microsoft’s Phi-4 and Google’s Gemma. These models can run on edge devices or local hardware, and yes even your laptop! They have faster inference times and lower cost which make them enticing although they have limited context window and reasoning capabilities compared to LLMs.
Best for: edge processing, real-time applications, specific domain tasks
Examples of edge processing use cases:
- Local chat bots without internet connectivity
- IoT devices requiring natural language processing
- Privacy-sensitive applications where data can’t leave the device
- Mobile applications where low latency is crucial
Foundation Models are pre-trained models that serve as a base for fine-tuning. These models can be adapted to specific domains or tasks through additional training with examples like GPT, BERT, RoBERTa, and T5. These models provide a starting point for creating specialized models which (shocker) are expensive and require access to GPU computational power. Most organization’s don’t go down the route of training or fine-tuning their models because of its resource intensiveness. This is typically done by ISVs and SaaS companies.
Now you may ask, where do I find these models? How do I compare them? Here are the most common.
Model Hubs and Marketplaces:
- Hugging Face Hub: Largest repository of open-source models
- Microsoft Foundry (formerly Azure AI Studio): Enterprise-ready models with Azure integration
- Amazon SageMaker: AWS’s collection of pre-trained models
- Google Cloud Model Garden: Collection of Google’s foundation models
- Anthropic’s Claude Models: Specialized for safety and reasoning
- GitHub repositories of major AI labs
- Meta’s open source models and libraries
Hugging Face is the most common with tons of analytics on each model to help you choose the right one.
Considerations for Model Selection:
- Inference costs and pricing models
- Hardware requirements and deployment constraints
- Licensing and usage restrictions
- Community support and documentation
- Fine-tuning capabilities and requirements
- Model update frequency and maintenance
- Ethical considerations and bias evaluations (IMPORTANT!!)
8. Test, Tune, Iterate
Now we can FINALLY put it all together!! Testing, tuning, and iterating is the name of the game to put your chosen architecture to the test and making sure the models you chose effectively chunked your data and provide the most relevant response outputs. This is where tools like Langchain and Langflow help the most. Here are the things to consider for each step.
Test: It is best to use a diverse set of queries covering expected use cases and programmatically run these sets when testing outputs of your RAG system. Make sure to Include edge cases and potential failure modes you would expect to see how the LLM handles the situation. An easy example is asking the same prompt whether the RAG system knows you are a boy or girl. Do you get a different response? This is checking for bias. How about if you ask the same question twice? Or the same question, slightly differently? This is checking for consistency.
Here are evaluation metrics for you to track:
- Relevance of retrieved information.
- Quality and accuracy of LLM responses.
- Response time and system efficiency.
Tune: Refine each component based on test results. This might involve adjusting chunking strategies, trying different embedding models, adding guard rails, or fine-tuning prompts.
Iterate: back to testing and trying again! Once you have a good blueprint for your architecture you can plan for a production push.
What’s New in RAG (The 2026 Edition)
RAG did not sit still while this post aged. Three patterns now dominate the conversation.
Agentic RAG flips retrieval from a fixed pipeline into a decision. The model decides when to search, what to search for, and whether the results are good enough to answer. Bad retrieval? It rewrites the query and tries again. This is the difference between a librarian who hands you the first book they grab and one who keeps digging until they find THE book.
GraphRAG adds knowledge graphs to the mix. Instead of retrieving isolated chunks, the system follows relationships between entities to answer questions that span multiple documents. I wrote a whole post on knowledge graphs and RAG if you want to go deeper.
Hybrid search and reranking is the unsung hero. Pure vector similarity misses exact matches like product codes and names. Combining keyword search with vector search, then running a reranker over the results, beats either approach alone. If your retrieval quality is meh, start here.
And the elephant in the room: massive context windows did NOT kill RAG. Stuffing a million tokens into a prompt is slow, expensive, and buries the needle in the haystack. Retrieval remains the way to feed models the RIGHT information instead of ALL the information.
Common Mistakes to Avoid in GenAI App Deployment
Mistakes during the design and deployment of RAG infrastructure can lead to inefficiencies, security vulnerabilities, or outright system failures. Here are some of the most common mistakes and how to avoid them.
1. Misaligned Data and Models
Perhaps the most frequent error is failing to properly align your data with the model. For example, if your vector embeddings don’t match what the language model expects, the system won’t be able to interpret the data correctly. Think of it as trying to fit a square peg into a round hole. Without alignment, nothing works as it should.
2. Inefficient Orchestration
Poor orchestration can cripple your system. If the vector database and the LLM aren’t communicating smoothly, you’ll experience delays and incomplete responses. Ensure your orchestration engine is robust and capable of handling complex queries without losing speed or accuracy.
3. Overlooking Scalability
Many teams fail to plan for the inevitable growth of data and demand. Without scalable infrastructure, you could end up with bottlenecks that slow down performance or require expensive retrofits. Always plan for expansion, even if your current needs are modest.
4. Neglecting Security
With sensitive data being processed and retrieved, neglecting security is a surefire way to invite trouble. Weak encryption or poor access controls can expose your system to malicious attacks or data breaches. Prioritize security from the outset to protect both the system and the data it handles.
Overarching Concepts for Your RAG System
Optimize Orchestration
Orchestration is the glue that holds the RAG system together. It manages the flow between the language model and the vector database, ensuring that queries are processed smoothly. Whether you use Python scripts or specialized orchestration tools, the key is seamless communication between components to avoid bottlenecks or delays in response time.
Scale Thoughtfully
As data and usage grow, so will the demands on your infrastructure. Start with a system that can scale easily, whether that means leveraging cloud-based storage, high-performance databases, or advanced orchestration tools. A scalable infrastructure will prevent performance degradation as your datasets expand.
Prioritize Security
RAG systems often deal with sensitive data, so building in security measures from the beginning is non-negotiable. This includes encryption, secure access controls, and data governance policies to ensure compliance with privacy regulations. Don’t leave security as an afterthought; it should be baked into your infrastructure from day one.
A New Age of AI Infrastructure
Building and maintaining GenAI apps and RAG infrastructure requires more than just technical expertise. It involves a deep understanding of how data flows through the system, how to scale efficiently, and how to secure sensitive information. By mastering the best practices outlined here, and avoiding the common pitfalls, you’ll be well-positioned to deploy a system that maximizes the potential of both generative AI and RAG.
Conclusion: Is Your Data Ready for the Future?
PHEW!! In a world where AI systems are becoming integral to business operations, having a strong infrastructure is critical. Whether you’re just getting started with GenAI or looking to optimize your RAG setup, the key is to focus on alignment, orchestration, scalability, and security. Want to dive deeper into optimizing your AI systems? Get in touch with us to explore how we can help you build a future-proof infrastructure tailored to your needs.
What changed (August 2026): Rewrote the database section (vector is now a data type, not a category), refreshed model references, and added the modern RAG patterns that did not exist when this was first published.