Retrieval-Augmented Generation (RAG) is a technique that lets a large language model (LLM) look up relevant information from your own documents before it answers. Instead of relying only on what the model memorized during training, RAG gives it fresh, specific context at question time. The result is more accurate, more current and easier-to-verify answers.

In this guide you will learn what RAG is, how a RAG pipeline works step by step, how it differs from fine-tuning, the mistakes that most often break it, and how to build a minimal version in Python.

What Is RAG? (Quick Answer)

RAG combines two things:

  1. Retrieval: searching a knowledge base (PDFs, web pages, databases, FAQs) for the passages most relevant to a question.
  2. Generation: passing those passages to an LLM so it writes the answer using them.

Think of it as an open-book exam. A plain LLM answers from memory. A RAG system lets the model open the right page of the book first.

Why Do LLMs Need RAG?

LLMs are powerful, but they have limits that RAG directly addresses:

  • Knowledge cutoff: a model knows nothing about events or documents after its training data ends.
  • No access to private data: it has never seen your company policies, product catalog or support tickets.
  • Hallucinations: when a model doesn't know, it may still produce a confident but wrong answer.
  • No source tracking: a plain model can't tell you where an answer came from.

RAG reduces these problems by grounding the answer in documents you control, and it lets you show citations to the user.

How a RAG Pipeline Works, Step by Step

A RAG system has two phases: an indexing phase (done ahead of time) and a query phase (done on every question).

Indexing phase

  1. Load documents. Collect your content: PDFs, web pages, Word files, database rows.
  2. Clean and split into chunks. Large documents are cut into smaller passages, because retrieving a whole 50-page PDF for one question is wasteful.
  3. Create embeddings. An embedding model converts each chunk into a list of numbers (a vector) that represents its meaning. Chunks with similar meaning end up close together.
  4. Store in a vector database. The vectors, plus the original text and metadata, are saved so they can be searched quickly.

Query phase

  1. Embed the question. The user's question is converted to a vector with the same embedding model.
  2. Retrieve the top matches. The system finds the chunks whose vectors are closest to the question vector.
  3. Build the prompt. The retrieved chunks are inserted into a prompt along with the question and instructions.
  4. Generate the answer. The LLM writes a response based on that context, ideally with citations.

RAG Architecture at a Glance

ComponentWhat it doesExamples
Document loaderReads source filesPDF, HTML, DOCX, database connectors
ChunkerSplits text into passagesFixed-size, sentence-based, heading-based
Embedding modelTurns text into vectorsOpen-source or API-based embedding models
Vector storeStores and searches vectorsPinecone, Qdrant, Weaviate, pgvector, FAISS
RetrieverFinds the best chunksSimilarity search, hybrid search, re-ranking
LLMWrites the final answerAny chat or instruction-tuned model

Chunking: The Step Most Beginners Get Wrong

Chunking has a large effect on answer quality.

  • Chunks too small lose context. A sentence like "It expires after 30 days" is useless without knowing what "it" is.
  • Chunks too large dilute meaning and waste prompt space on irrelevant text.
  • No overlap can cut an important sentence in half.

A practical starting point is chunks of a few hundred words with a small overlap, split along natural boundaries such as headings and paragraphs. Then test with real questions and adjust. There is no universal best size, because it depends on your documents.

A Simple RAG Example in Python

This minimal example uses the sentence-transformers library and plain NumPy, so you can see the idea without a vector database. For production you would swap NumPy for a vector store.

from sentence_transformers import SentenceTransformer
import numpy as np

model = SentenceTransformer("all-MiniLM-L6-v2")

chunks = [
    "Refunds are available within 14 days of purchase.",
    "Support is open Monday to Friday, 9 AM to 6 PM IST.",
    "Premium plans include priority email support.",
]

# Indexing: embed every chunk once
chunk_vecs = model.encode(chunks, normalize_embeddings=True)

def retrieve(query, k=2):
    q = model.encode([query], normalize_embeddings=True)[0]
    scores = chunk_vecs @ q              # cosine similarity
    top = np.argsort(scores)[::-1][:k]
    return [chunks[i] for i in top]

def build_prompt(query):
    context = "\n".join(retrieve(query))
    return (
        "Answer using only the context below. "
        "If the answer is not in the context, say you don't know.\n\n"
        f"Context:\n{context}\n\nQuestion: {query}"
    )

print(build_prompt("How long do I have to ask for a refund?"))

Send the printed prompt to any LLM and it will answer from your data. Notice the instruction "say you don't know". That single line reduces made-up answers.

RAG vs Fine-Tuning

RAGFine-tuning
Best forAdding knowledge and factsChanging style, format or behavior
Updating knowledgeRe-index documents (fast)Retrain the model (slow)
Source citationsEasyDifficult
Private data controlData stays in your storeData is baked into model weights
Upfront effortLowerHigher

They are not rivals. Many production systems use RAG for facts and light fine-tuning, or careful prompting, for tone and format.

Common RAG Mistakes (and Fixes)

  • Poor document cleaning. Headers, footers and navigation text pollute chunks. Clean before indexing.
  • Ignoring metadata. Store source, date and category so you can filter results and show citations.
  • Retrieving too few or too many chunks. Start with 3 to 5 and tune against real questions.
  • Vector-only search. Pure semantic search can miss exact terms like product codes. Hybrid search (keyword plus vector) often helps.
  • No re-ranking. A re-ranker re-scores the top results and often improves relevance noticeably.
  • No evaluation. Build a small test set of real questions with expected answers and check it after every change.
  • Stale index. If your documents change, schedule re-indexing so answers don't go out of date.

How to Evaluate a RAG System

Judge the two halves separately:

  • Retrieval quality: did the right chunk appear in the top results?
  • Answer quality: is the answer correct, complete and faithful to the retrieved text (not invented)?

If retrieval is bad, fix chunking, embeddings or search. If retrieval is good but answers are bad, fix the prompt or the model.

Real-World Use Cases

  • Customer support chatbots that answer from help-center articles
  • Internal knowledge assistants for HR, IT and company policies
  • Document Q&A for contracts, manuals and research papers
  • E-commerce and WhatsApp assistants that answer from a product catalog
  • Voice assistants that look up information while the caller is talking

Frequently Asked Questions

What does RAG stand for?

RAG stands for Retrieval-Augmented Generation.

Is RAG better than fine-tuning?

Neither is universally better. RAG is usually the better choice when you need the model to use specific, changing information. Fine-tuning is better for changing how a model writes or behaves.

Does RAG stop hallucinations completely?

No. It reduces them by grounding answers in retrieved text, but a model can still misread or ignore the context. Clear prompts, good retrieval and evaluation are still needed.

Do I need a vector database for RAG?

Not for small projects. A simple in-memory search, as shown above, works for a few hundred chunks. For larger collections, a vector database gives you fast search, filtering and scaling.

What is the difference between embeddings and RAG?

Embeddings are one building block: they turn text into vectors for searching. RAG is the whole system that uses retrieval plus an LLM to produce answers.

Can RAG work with my own PDFs and websites?

Yes. That is one of its most common uses. You load the files, chunk them, embed them and store them in a searchable index.

Conclusion

RAG is one of the most practical ways to make an LLM useful with your own information. Start simple: clean documents, sensible chunks, good embeddings and a prompt that tells the model to stay within the context. Then measure, improve retrieval, and add hybrid search and re-ranking as needed.

If you found this guide useful, explore more AI and engineering tutorials on BuildWithJit.

✳ ✳ ✳