Retrieval-Augmented Generation (RAG) is an AI architecture that retrieves relevant information from an external knowledge source and supplies it to a large language model (LLM) when generating a response. Instead of relying only on what the model learned during training, a RAG application can ground an answer in documents selected for the current question.
A typical RAG pipeline has two stages: prepare and index source material ahead of time, then retrieve relevant passages at question time and pass them to the model as context. The pattern can make an application’s knowledge easier to update without retraining the model, but answer quality still depends on the source material, retrieval, and generation working well together.
The RAG architecture at a glance
Ingestion turns source documents into searchable chunks. Retrieval finds the chunks most relevant to a question. Generation combines the question and retrieved context to produce an answer.
Documents → Extract text → Split into chunks → Create embeddings → Index chunks
Question time
User question → Embed query → Search index → Retrieve relevant chunks
Retrieved context + user question → LLM → Generated answer
The index is normally prepared before a user asks a question. At query time the system embeds the question using a compatible embedding model, searches for similar document chunks, and supplies selected results with the original question as context.
1. Prepare documents in an ingestion pipeline
The ingestion pipeline converts source content—such as PDFs, office documents, web pages, or database records—into a form that can be searched and cited later. A typical workflow is:
- Extract text: Parse the source files and preserve useful structure, such as headings, page numbers, and metadata.
- Split into chunks: Divide long content into passages small enough to retrieve and fit into the model’s context.
- Generate embeddings: Convert each chunk into a numerical vector that represents aspects of its meaning.
- Store and index: Save vectors, chunk text, and relevant metadata so the application can search and return the source passages.
When documents change, the application needs a strategy for updating or replacing the affected indexed chunks. Keeping source identifiers and metadata alongside each chunk makes it easier to trace results back to their documents.
2. Retrieve relevant context for a question
At query time, the application converts the user’s question into an embedding and searches the index for similar document vectors. It selects a useful set of chunks and passes their text, along with the question, to the language model.
Vector similarity is a way to find semantically related material, not proof that a passage answers the question. Retrieval quality depends on the source data, chunk boundaries, embedding model, search settings, and any filters applied. Production systems should evaluate retrieved passages against representative questions instead of assuming the top results are always correct.
3. Generate an answer from the retrieved material
The language model receives the user’s question and retrieved context, then generates a response. A well-designed application can instruct the model to stay grounded in the supplied sources, distinguish evidence from uncertainty, and say when the retrieved material is insufficient.
RAG can reduce unsupported answers by giving the model relevant source material, but it does not eliminate hallucinations. The model can misunderstand the context, retrieved documents can be incomplete or out of date, and search can miss the right evidence. Test the complete pipeline and, where useful, show users the sources behind an answer.
Choosing a document chunking strategy
Chunking affects what the retriever can find and how much context the model receives. The best approach depends on the structure of the source documents and the kinds of questions users ask.
- Fixed-size chunks: Split text into similarly sized pieces, sometimes with overlap. This is straightforward to implement, but a chunk boundary can separate a heading from its explanation or split a relevant idea across passages.
- Structure-aware or hierarchical chunks: Split along document boundaries such as sections, paragraphs, or sentences, and preserve their hierarchy. This often retains useful context in structured documents, but requires reliable parsing and sensible handling of sections that are too long or short.
- Semantic chunks: Group text around changes in meaning rather than a fixed token count. This can keep related ideas together, but adds processing choices and should be validated against the application’s retrieval needs.
There is no universally optimal chunk size. Compare strategies using actual queries and measure whether retrieved passages contain the information needed to answer them. Also account for the context-window size, response latency, and embedding and storage costs.
Embeddings and vector databases
An embedding model maps text into vectors so the application can search for passages with related meanings. Use compatible embedding behavior when indexing documents and embedding queries; changing the embedding model may require rebuilding or migrating the index.
A vector database or search engine stores and retrieves vectors. The following are examples from the ecosystem, not a ranking or recommendation:
| Component | Examples | Typical role |
|---|---|---|
| RAG and indexing frameworks | LangChain, LlamaIndex, Haystack | Connect data loading, chunking, retrieval, and model calls. |
| Embedding models | OpenAI text-embedding-3-large, Google Gemini Embeddings, Sentence Transformers | Convert document chunks and user queries into vector representations. |
| Vector search stores | ChromaDB, FAISS, Pinecone, Elasticsearch | Index or search vectors; capabilities and hosting models vary. |
Choose components based on your data, scale, operational constraints, deployment environment, security requirements, and measured retrieval quality. A vector index is only one part of a RAG system; document parsing, authorization, updates, evaluation, and monitoring matter too.
Benefits—and what RAG does not guarantee
- Knowledge can be refreshed: Update the indexed source material without fine-tuning the language model for each content change.
- Responses can use relevant source context: Retrieved passages give the model evidence specific to the current question.
- Data can stay in controlled systems: The architecture can retrieve from private sources, but privacy depends on access controls, deployment choices, logging, and how retrieved context is sent to the model provider.
- There are trade-offs: Document processing, embeddings, storage, search, and model calls all add cost and operational complexity. RAG is not automatically cheaper or more accurate than every alternative.
Key takeaway: RAG works by preparing documents for retrieval and supplying relevant passages to a language model alongside a user’s question. Strong results require good source data, thoughtful chunking, reliable retrieval, suitable access controls, and evaluation of the complete answer pipeline.