Retrieval Augmented Generation

Retrieval-Augmented Generation (RAG) is the architecture pattern that enhances LLM responses by first retrieving relevant documents from an external knowledge base and injecting them into the prompt context before generation — grounding the model's output in factual, up-to-date information rather than relying solely on parametric knowledge learned during pretraining, dramatically reducing hallucination and enabling domain-specific expertise.

RAG Pipeline

1. User Query: "What is the maximum operating temperature for the XYZ-3000 chip?"
2. Retrieval: Query embedded → similarity search against vector database → top-K relevant documents retrieved.
3. Augmentation: Retrieved documents prepended to the prompt as context.
4. Generation: LLM generates answer grounded in the retrieved context.

Components

ComponentPurposeExamples
Embedding ModelConvert text to vectorsOpenAI ada-002, BGE, E5, GTE
Vector DatabaseStore and search embeddingsPinecone, Weaviate, Qdrant, Chroma, pgvector
Chunking StrategySplit documents into retrievable units256-1024 tokens, overlap 10-20%
RerankerRe-score retrieved results for relevanceCohere Rerank, BGE-reranker, cross-encoder
LLMGenerate final answer from contextGPT-4, Claude, LLaMA, Mistral

Chunking Strategies

StrategyChunk SizeOverlapBest For
Fixed-size512 tokens50 tokensGeneral text
Sentence-based3-5 sentences1 sentencePrecise retrieval
SemanticVariableNoneCoherent topics
Recursive character1000 chars200 charsLangChain default
Parent-childSmall retrieve, large returnN/ABest of both worlds

Advanced RAG Techniques

  • Hybrid Search: Combine vector similarity with keyword (BM25) search → better recall.
  • HyDE: Generate hypothetical answer first → use it as retrieval query → better embedding match.
  • Multi-Query: LLM generates multiple query variations → retrieve for each → union results.
  • Reranking: Initial retrieval (fast, approximate) → reranker scores (slow, accurate) → top results.
  • Agentic RAG: LLM decides when and what to retrieve iteratively — not just single retrieval.

Evaluation Metrics

MetricWhat It Measures
FaithfulnessIs the answer supported by retrieved context?
Answer RelevanceDoes the answer address the question?
Context RelevanceAre retrieved documents relevant to the query?
Context RecallDid retrieval find all necessary information?

RAG vs. Fine-Tuning

  • RAG: Dynamic knowledge, no retraining needed, traceable sources, handles knowledge updates.
  • Fine-tuning: Bakes knowledge into weights, better for style/format changes, no retrieval latency.
  • Best practice: Use both — fine-tune for domain style, RAG for factual knowledge.

RAG is the standard architecture for production LLM applications — by separating knowledge storage (database) from reasoning (LLM), it solves the core limitations of LLMs: outdated knowledge, hallucination, and lack of domain expertise, making it essential for enterprise AI deployments.

Take retrieval augmented generation further

Ask the copilot about this term, or have our engineers assess it against your process.