RAG Fundamentals

Learn why RAG retrieves source material before generating an answer, how knowledge base preparation and question answering work, and when to use it.

🎯 Introduction

Large language models can answer many general questions, but they may not know your company’s internal policies, project documentation, or product instructions updated yesterday.

For example, a user asks, “How long after purchasing the new membership can I request a refund?” The model may not know the latest policy. Attaching the entire product manual to every question would also become increasingly cumbersome.

RAG (Retrieval-Augmented Generation) offers another approach: first find relevant content in existing source material, then ask a large language model to use that content to generate an answer.

This article uses a membership refund example to explain the problem RAG solves, how a question is answered, and when RAG is useful. We will start with the overall workflow before writing a complete implementation.


🧱 What Is RAG?

Suppose your company’s product documentation says:

You can request a refund for the new membership within 7 days of purchase.

A user asks, “What is the refund window for a membership?”

If you send only the question to a large language model, it may not know this internal policy. It could return another product’s refund window or even guess a number.

If you first retrieve the passage above and send it along with the question, the model has a source to base its answer on:

Under the new membership policy, you can request a refund within 7 days of purchase.

This is the central idea of RAG: the model does not need to memorize all your business documents in advance; it receives relevant material when answering.

RAG does not suddenly give the model the ability to read company files on its own. In practice, our application usually retrieves the material and sends the results to the model along with the question.


🔍 How Does RAG Work?

RAG has two parts: prepare the knowledge base in advance, then retrieve and answer when a user asks a question.

First, look at the overall workflow. We will then examine each step.

1

Prepare the knowledge base

Product manuals and internal documents
Read documents and split the text into chunks
Generate embeddings
Store the source text and vector index
2

Retrieve and answer when a user asks

User asks: “How long after purchase can I request a refund?”
Retrieve relevant chunks from the knowledge base
Use the index and source text saved during preparation
Send the question and retrieved material to the model
Generate an answer grounded in the sources

Step 1: Prepare the Knowledge Base

Suppose the company has dozens of membership documents. Sending all of them to the model for every question is impractical, so we first organize them into searchable material.

This typically involves the following steps.

1. Read the documents

Extract text from files such as product manuals and refund policies to use as the knowledge base’s source content.

2. Split the text into chunks

A manual can be long and cover multiple topics, such as membership tiers, payment methods, and refund policies. Splitting it into smaller chunks makes it easier to find the part relevant to a particular question without retrieving the entire document each time.

For example, the refund policy could be a separate chunk:

You can request a refund for the new membership within 7 days of purchase.

3. Generate and store embeddings

To find content with similar meanings rather than just matching words, an embedding model can convert text into a sequence of numbers called a vector.

A vector is a representation that helps a program compare the semantic similarity of texts. For example, “What is the refund window?” and “How long do I have to get my money back?” use different wording but may express a similar meaning.

The program stores these vectors alongside the original chunks for later retrieval. Storage can be a database with vector search support or another indexing system.

For now, focus on what vectors are used for. Later articles explain how embeddings are generated and how similarity is calculated.

Once the documents are prepared, you do not need to reprocess all of them for every question. Only new or modified material requires updates to the corresponding content and index.

Step 2: Retrieve and Answer a User’s Question

The user now asks, “How long do I have to get a membership refund?”

1. Retrieve relevant chunks

The application searches the knowledge base using the user’s question. In a typical vector search setup, it first converts the question into a vector, then finds document chunks with similar meanings.

Suppose it retrieves:

You can request a refund for the new membership within 7 days of purchase.

2. Send the source material and question to the model

The application combines the user’s question, the retrieved material, and any necessary answer instructions, then sends them to the large language model. The model receives a refund policy to refer to alongside the question.

3. Generate an answer

Using that passage, the model answers, “You can request a refund for the new membership within 7 days of purchase.” If the application needs to show supporting sources, it can also return the stored document references.

That completes one RAG question-and-answer cycle. Keep the two roles in mind:

  • Retrieval finds material that may be relevant to the question.
  • Generation uses the question and material to compose an understandable answer.

The application retrieves the material; the model does not search the database itself. We will cover which model API to call and how to build request parameters in the implementation lessons.

What If No Answer Is Found?

Suppose the user asks, “Can I extend my membership by three months for free?” but the knowledge base contains no policy on this.

Using RAG does not guarantee a correct answer. When reliable supporting material is unavailable, the application should clearly say that the information is insufficient instead of letting the model invent a policy.

Even when material is found, retrieval results may be incomplete or the model may misinterpret them. RAG can provide supporting evidence, but it cannot guarantee that every answer is correct.


⚖️ How Does RAG Compare with Other Approaches?

Other approaches to working with business documents include:

ApproachHow it worksSuitable situations
Call an LLM directlySend only the question to the modelGeneral knowledge that does not require internal sources
Attach documents each timeManually provide relevant material with the questionA few documents and occasional queries
RAGAutomatically retrieve relevant content from a knowledge base and send it to the modelMany documents, frequent updates, or a need to cite sources
Fine-tuningAdjust model parameters using training dataAdapting a model to a specific task, format, or behavior

RAG and fine-tuning are particularly easy to confuse.

RAG supplies source material when a question is asked; fine-tuning changes the model itself through training.

For example, the company changes its refund window from 7 to 14 days. With RAG, you update the policy and its index in the knowledge base so subsequent questions can retrieve the new content. You do not need to retrain the model for this policy change.

Fine-tuning can also be used for some knowledge-related tasks, but it is usually not the first choice for keeping a model up to date with changing internal policies.


💡 When Should You Use RAG?

RAG is well suited to applications that need to find answers in a large body of existing material, such as:

  • Enterprise knowledge base assistants: Look up internal processes, employee handbooks, and business policies.
  • Product documentation assistants: Answer product usage questions based on user manuals.
  • Technical documentation assistants: Find information in project documentation and API references.

Not every question needs RAG, however.

For example, “Has my order shipped yet?” depends on the order system’s current state. Querying a business API directly is more appropriate than performing semantic search over historical documents.

If the knowledge base includes internal material with different employee access levels, the application must enforce access permissions during retrieval. Adding a document to the knowledge base must not make it available to every user.


❓ Knowledge Check

Q1: Does using RAG make the large language model remember company documents?

No. Typical RAG does not change model parameters. It retrieves relevant material for each question and supplies it as reference content for that answer.

Q2: Once documents are in the knowledge base, will the model always answer correctly?

No. The application may miss a key chunk or retrieve outdated content. Even with correct sources, the model can still misinterpret them. Check content updates, retrieval quality, and the evidence supporting the answer.


🧪 Exercise

Suppose the knowledge base contains these three passages:

  1. You can request a refund for the new membership within 7 days of purchase.
  2. Membership purchases support Alipay and WeChat Pay.
  3. Premium members can use exclusive themes.

Consider two user questions:

  • Question A: “What is the refund window for the new membership?”
  • Question B: “Does buying a membership include a free three-month extension?”

Which passage should be retrieved for question A? Is there enough material to answer question B? If not, how should the application respond?

Success criteria: Select passage 1 for question A and answer “within 7 days of purchase.” For question B, explain that the available material cannot confirm this, rather than inventing an extension policy.


🎉 Congratulations! You now understand the core idea of RAG, the roles of knowledge base preparation, retrieval, and generation, and the problems it is suited to solving.