What is RAG?
Learn how Retrieval-Augmented Generation connects search with an LLM so answers can use relevant external knowledge.
By the end of this lesson, you will be able to:
- Explain Retrieval-Augmented Generation in simple terms.
- Understand why an LLM can benefit from retrieved information.
- Describe the main stages of a RAG pipeline.
- Understand documents, chunks, embeddings, retrieval, context, and generation.
- Build a simple Python mental model of retrieval followed by generation.
- Explain how RAG differs from relying only on an LLM's internal knowledge.
- Recognize common RAG limitations and mistakes.
What Is RAG?
RAG stands for Retrieval-Augmented Generation. It is a pattern where an application first retrieves relevant information from an external knowledge source and then gives that information to a generative AI model as context for its answer.
An employee asks about an internal leave policy. RAG retrieves the relevant policy section and places it in the LLM context before generating the answer.
A customer asks how to configure a device. RAG retrieves the matching manual section and the LLM explains the steps using that evidence.
Why Do We Need RAG?
An LLM is powerful, but a general model does not automatically know every private, current, or domain-specific document your application needs.
- Uses information available to the model
- May lack private company knowledge
- May not contain your latest documents
- Can answer without showing your source context
- Retrieves application-specific information
- Can use updated documents
- Supplies relevant context to generation
- Can return source references alongside answers
RAG does not magically make an LLM correct. It gives the model a better information source for the question.
The RAG Pipeline
In a real system, there are usually additional steps around this flow: document ingestion, chunking, embedding, indexing, filtering, reranking, prompt construction, citations, evaluation, and monitoring.
RAG Has Two Major Parts
Retrieval
Find useful information from an external collection such as documents, a vector database, a search index, or another knowledge source.
Generation
Give the retrieved information to a generative model and ask it to produce a useful answer.
Relevant evidence
The retriever should bring back chunks that can actually help answer the user's question.
Useful response
The LLM should use the supplied context appropriately rather than inventing unsupported details.
Where Does the Knowledge Come From?
RAG can retrieve from many types of external knowledge. The source depends on the application.
Manuals, reports, policies, research papers, and guides.
Public documentation, help centers, or approved website content.
Structured or semi-structured application information.
Internal knowledge bases, procedures, and project documentation.
Tickets, FAQs, troubleshooting guides, and product knowledge.
A system can combine several approved knowledge collections.
Documents Become Chunks
Large documents are usually not placed into a vector index as one giant block. They are divided into smaller chunks so retrieval can find the specific parts that matter.
Good chunking is important because retrieval quality depends heavily on whether each stored chunk contains enough useful meaning without carrying too much unrelated text.
Embeddings and Retrieval
In semantic RAG systems, chunks are commonly converted into embeddings. An embedding represents text as a vector so that semantically related queries and documents can be compared.
User question
βHow can I reset my password?β
Stored knowledge
βTo reset your password, open Account Settings and select Change Password.β
A vector search can find the second text even when the query and document do not use exactly the same words.
What Happens During a User Query?
The exact implementation can differ. Some systems also use keyword search, metadata filters, hybrid search, reranking, query rewriting, or other retrieval strategies before generation.
Simple Python RAG Mental Model
The code below intentionally keeps retrieval and generation simple. It shows the core idea without requiring a specific vector database or LLM provider.
documents = [
"You can reset your password from Account Settings.",
"Shipping usually takes 3 to 5 business days.",
"Invoices are available from the Billing page.",
]
query = "How do I reset my password?"
# Simplified retrieval for teaching purposes.
keywords = set(query.lower().replace("?", "").split())
retrieved = sorted(
documents,
key=lambda doc: len(keywords & set(doc.lower().split())),
reverse=True,
)[:2]
context = "\n".join(retrieved)
prompt = f"""Answer the question using the context below.
Context:
{context}
Question:
{query}
"""
print(prompt)In production, the retrieval step would normally use embeddings/search and the final prompt would be sent to an LLM API.
RAG Context Goes Into the Prompt
The retrieved chunks are not the answer by themselves. They become context that the generation model can use.
context = """
Password reset is available from Account Settings.
A password must contain at least 12 characters.
"""
question = "How do I reset my password?"
prompt = f"""
Use only the supplied context when answering.
If the context does not contain the answer, say that you do not have enough information.
Context:
{context}
Question:
{question}
"""
print(prompt)This pattern is commonly called grounding: the application supplies relevant external information that the model can use when generating its response.
RAG vs Fine-Tuning
- Retrieves external information at query time
- Good for changing knowledge
- Good for private document collections
- Knowledge can be updated by changing the source/index
- Changes model behavior through training
- Useful for style, format, or specialized behavior
- Does not simply act as a live document lookup system
- Updating knowledge can require another training process
They solve different problems and can also be used together. RAG is primarily about supplying relevant external information at inference time.
RAG Does Not Mean the LLM Knows Everything
If the correct chunk is not retrieved, the LLM may not have the evidence it needs.
Irrelevant context can distract the model.
Sending many unnecessary chunks can increase cost and reduce focus.
Incorrect or outdated documents can produce poor answers.
The prompt should clearly explain how the model should use retrieved context.
RAG reduces some risks but does not guarantee factual answers.
How RAG Fits With the Topics You Already Learned
Prepare knowledge
Split documents into retrievable units.
Represent meaning
Convert text into vectors for semantic comparison.
Store and search
Keep vectors and metadata so relevant chunks can be found.
Find evidence
Return candidate chunks for the user's question.
Improve ordering
Score candidates more precisely when needed.
Answer
Give selected context to the LLM and generate the response.
What Makes a Good RAG System?
When Is RAG Useful?
Answer questions over company or application-specific information.
Use a maintained knowledge source instead of depending only on static model knowledge.
Ask questions over manuals, policies, reports, or collections of documents.
Retrieve approved troubleshooting and product information.
Help teams find useful information across a knowledge base.
Retrieve relevant sources before generating a summary or explanation.
Common RAG Mistakes
Do not assume retrieval is good because the demo looks impressive.
Very large chunks can contain too much unrelated information.
Very small chunks can lose the context needed to answer a question.
More context is not automatically better.
Filters such as access, product, language, or date can be important.
Retrieved content should come from sources your application is allowed to use.
How to Evaluate RAG
Evaluate the retrieval stage and the final answer separately. A good LLM cannot compensate for consistently missing evidence.
Practice: Design a Simple RAG System
Imagine you have 100 company policy documents and employees can ask questions about them. Design the flow from document ingestion to the final answer.
Build the RAG pipeline on paper
Write down what happens at each stage: document loading, chunking, embeddings, indexing, query processing, retrieval, context construction, and generation.
Quick Quiz
1. What does RAG stand for?
Retrieval-Augmented Generation.
2. What happens before generation?
The system retrieves relevant external information and prepares it as context for the LLM.
3. Why are documents usually chunked?
Smaller meaningful chunks make it easier to retrieve the specific information relevant to a query.
4. Does RAG guarantee a correct answer?
No. Poor source data, retrieval errors, irrelevant context, or model behavior can still lead to incorrect answers.
30-Second Recap
Find relevant external evidence.
Give useful evidence to the model.
Ask the LLM to answer using the context.
Measure retrieval and final answer quality.
Key Takeaways
- RAG combines retrieval with generative AI.
- The retriever finds relevant external information; the LLM uses that information to generate an answer.
- Documents are commonly chunked, embedded, and indexed before users ask questions.
- At query time, retrieval finds candidate chunks that become context for generation.
- RAG is useful for private, domain-specific, and frequently changing knowledge.
- RAG is not a guarantee against hallucinations or incorrect answers.
- Good RAG requires strong source data, chunking, retrieval, ranking, context construction, generation, and evaluation.