RAG (Retrieval-Augmented Generation): How LLMs Use External Knowledge

· by Pavel Kiyatkin · Beginner

RAG (Retrieval-Augmented Generation) combines a search system with a language model. The model retrieves relevant passages from your knowledge base, then generates an answer grounded in those passages — instead of relying only on what it memorized during training.

Why RAG exists

A standalone LLM has two problems for production:

  1. Knowledge cutoff. Models are trained on data up to a fixed date. They cannot answer questions about events, products, or documents that appeared after that date.
  2. Hallucination. Without grounding, models confidently invent plausible-sounding but false details.

RAG addresses both: the LLM only sees text you supplied, and you can update that text without retraining the model.

The architecture in 5 steps

  1. Index your corpus. Split documents into chunks (300-1000 tokens typical). Encode each chunk as a vector embedding using a model like text-embedding-3-large or bge-large.
  2. Store vectors. Save embeddings in a vector database (pgvector, Pinecone, Qdrant, Weaviate, Chroma).
  3. Query time: embed the question. Use the same embedding model.
  4. Retrieve top-K. Approximate nearest neighbor search returns the K most relevant chunks (typically K = 4-20).
  5. Generate with context. Pass the retrieved chunks + the user question to the LLM in a single prompt: “Given these documents: [chunks], answer: [question].”

When RAG beats fine-tuning

ScenarioRAGFine-tuning
Frequently changing knowledge (docs, prices, news)✓✗
Need to cite sources for compliance/trust✓✗
Inject company-specific terminologypartial✓
Change model behavior/style✗✓
Cost per queryhigher (retrieval + larger context)lower
Setup costlowhigh (training run + dataset prep)

Rule of thumb: RAG for facts that change, fine-tuning for skills that persist. Often you want both.

Where RAG fails in practice

Production checklist

RAG is not a model technique. It is an information architecture decision. The LLM is interchangeable; the corpus, the chunking, and the retrieval pipeline are where the work lives.