RAG (Retrieval-Augmented Generation)
RAG is a technique that combines an LLM with an external knowledge retrieval system. Instead of relying solely on its training data, the model retrieves relevant documents at query time and uses them as context to generate more accurate, up-to-date responses.
What is Retrieval-Augmented Generation (RAG)?
Retrieval-Augmented Generation (RAG) is an AI architecture that enhances large language models by connecting them to external knowledge sources. Rather than generating answers purely from memorized training data, a RAG system first retrieves relevant chunks of information from a database, then augments the model's prompt with that context before generating a response.
How RAG Works
- Query encoding — The user's question is converted into a vector embedding.
- Retrieval — The embedding is used to search a vector database (e.g. Pinecone, ChromaDB) for the most semantically similar document chunks.
- Augmentation — The retrieved chunks are injected into the LLM's prompt as context.
- Generation — The LLM generates a response grounded in the retrieved context.
Why RAG Matters
RAG solves three critical problems with standalone LLMs:
- Knowledge cutoffs — LLMs have a training cutoff date. RAG gives them access to current information.
- Hallucination — Grounding responses in retrieved facts significantly reduces confabulation.
- Private data — You can connect the model to your own proprietary documents without fine-tuning.
RAG vs Fine-tuning
Fine-tuning bakes knowledge into model weights permanently. RAG keeps knowledge external, versioned, and updatable. For most enterprise use cases, RAG is faster, cheaper, and more maintainable than fine-tuning.
Common RAG Frameworks
- LangChain — Most popular; has extensive RAG chain abstractions.
- LlamaIndex — Optimised specifically for document retrieval pipelines.
- Haystack — Production-grade, especially for search-heavy use cases.
Example
A customer support bot uses RAG to retrieve the 5 most relevant paragraphs from your internal documentation before answering. This prevents hallucinated answers and keeps responses grounded in your actual product docs.