AI From Zero · AI for Developers

Retrieval-Augmented Generation (RAG)

Learn how Retrieval-Augmented Generation combines information retrieval with generative AI to produce responses using relevant external information.

Estimated learning time: 55 minutes

What You'll Learn

  • Understand what Retrieval-Augmented Generation means
  • Learn why AI applications need external information
  • Understand the basic RAG workflow
  • Learn how embeddings and vector search support RAG
  • Understand document ingestion and retrieval
  • Learn how retrieved information becomes context for an AI model
  • Understand grounding and source attribution
  • Recognize common RAG limitations and failure modes

1. Introduction

Generative AI models can produce useful answers, but an application often needs to work with information that is outside the models built-in knowledge.

A company may have internal policies, product documentation, support articles, contracts, manuals, or other private information that an AI model needs to use when answering questions.

Retrieval-Augmented Generation, commonly called RAG, is an approach that combines information retrieval with generative AI.

Instead of asking the AI model to answer entirely from its existing knowledge, the application first retrieves relevant information and provides that information as context for the response.

2. What Does RAG Mean?

The name describes the basic process:

  • Retrieval — find relevant information.
  • Augmented — add that information to the model input as context.
  • Generation — generate a response using the supplied context.

In simple terms:

User question → retrieve relevant information → provide context → generate response.

3. Why RAG Is Useful

Many AI applications need information that changes frequently or belongs to a specific organization.

Examples include:

  • Company policies
  • Product documentation
  • Customer support knowledge
  • Internal procedures
  • Research documents
  • Technical manuals
  • Course materials

RAG allows an application to connect a generative AI model with an external information source without requiring the model itself to contain all of that information.

4. RAG Does Not Mean Retraining the Model

A common misunderstanding is that adding company documents to a RAG system means retraining the AI model.

Normally, that is not what happens.

The documents are stored separately. Relevant information is retrieved when a user asks a question and is then included in the request sent to the AI model.

This means the source information can often be updated without retraining the underlying generative model.

5. The Basic RAG Architecture

A simplified RAG system contains several components:

  1. Source documents
  2. Document processing system
  3. Embedding model
  4. Vector database or retrieval system
  5. Application logic
  6. Generative AI model
  7. User interface

Each component has a different responsibility.

6. Step One: Collect Source Information

The first stage is identifying the information that the application should retrieve.

Sources may include PDF files, web pages, databases, text files, manuals, knowledge bases, or other business systems.

The quality of the source material directly affects the quality of retrieval and responses.

7. Step Two: Prepare the Documents

Documents often need to be cleaned and divided into useful sections.

Large documents can be divided into smaller chunks so that retrieval can identify specific relevant passages.

The application should preserve useful information about where each chunk originated.

8. Step Three: Generate Embeddings

An embedding model converts each document chunk into a numerical vector.

These vectors provide a representation that can be compared with vectors generated from future user queries.

This allows the retrieval system to search for information based on semantic similarity.

9. Step Four: Store the Information

The vectors can be stored in a vector database or another system that supports vector search.

The application should also store useful metadata and a reference to the original content.

For example, a record could contain:

  • Embedding vector
  • Document identifier
  • Chunk identifier
  • Document title
  • Department
  • Version
  • Access information

10. Step Five: Receive a User Question

When a user asks a question, the application receives the query.

For example:

What is the company policy for working from home?

The application now needs to locate the relevant information.

11. Step Six: Create a Query Embedding

The application can send the user query to the embedding model and obtain a vector representation.

The query vector can then be compared with vectors representing the stored document chunks.

12. Step Seven: Retrieve Relevant Information

The retrieval system searches for document chunks that are semantically relevant to the query.

The application may retrieve the top few results and apply additional filtering.

Metadata filters can be important when the knowledge base contains information belonging to different departments, users, regions, or access levels.

13. Step Eight: Build the Model Context

The retrieved information is then added to the input sent to the generative AI model.

The application can provide instructions explaining how the model should use the supplied context.

For example, the application may instruct the model to answer using the provided company documentation and to indicate when the available information does not contain an answer.

14. Step Nine: Generate the Response

The generative AI model processes the user question together with the retrieved context.

It then produces a response that can use the retrieved information.

The application can display the response to the user and may also provide references to the source documents.

15. Grounding

Grounding means connecting an AI response to relevant information supplied by the application.

RAG can improve grounding because the model receives current or organization-specific information as part of the request.

However, retrieval does not guarantee that every generated statement is correct.

16. Source Citations

A RAG application can provide source references alongside an answer.

For example, a company assistant could identify the document and section used to answer a policy question.

Source references can help users verify important information and can make the system easier to audit.

17. RAG and Hallucinations

RAG can reduce some hallucination problems by supplying relevant external information, but it does not eliminate hallucinations.

The retrieval system may return the wrong information. The retrieved information may be incomplete. The model may misunderstand the context or generate unsupported statements.

Therefore, RAG should be treated as a system that needs evaluation and controls rather than as a guarantee of factual accuracy.

18. Retrieval Quality Matters

If the correct information is not retrieved, the generative model may not have the information needed to produce a good answer.

This means RAG quality depends heavily on retrieval quality.

Developers should test whether relevant documents are consistently retrieved for representative questions.

19. Chunking and RAG

Chunking is an important part of RAG design.

If chunks are too large, they may contain too much unrelated information.

If chunks are too small, important context may be separated across multiple chunks.

Chunking should therefore be based on the structure and meaning of the source content rather than using an arbitrary size for every document type.

20. Metadata Filtering

Metadata can significantly improve retrieval.

For example, a company assistant might filter documents by:

  • Department
  • Country
  • Document type
  • Version
  • Date
  • Permission level

This can reduce irrelevant results and help enforce appropriate retrieval boundaries.

21. RAG and Access Control

Access control is a critical part of a production RAG system.

A user should not receive confidential information simply because a private document is semantically similar to the question.

The application should determine which information the user is authorized to access before that information becomes available for generation.

22. RAG With Databases

RAG does not require every source to be a document.

An application can retrieve information from structured databases or other systems and provide appropriate results as context to a generative model.

Traditional databases remain useful for exact structured queries, while semantic retrieval can help locate relevant unstructured information.

23. Hybrid Retrieval

Some systems combine multiple retrieval methods.

For example, a system may combine keyword search with vector similarity search.

This can be useful when exact terms such as product codes, legal references, error codes, or account identifiers are important.

24. Reranking Retrieved Results

A retrieval system may initially return several candidate chunks.

A second ranking stage can then evaluate those candidates and determine which ones are most useful for the final response.

This process is called reranking.

25. Context Size

The AI model cannot necessarily receive an unlimited amount of retrieved information.

If an application retrieves too many large chunks, the request can become unnecessarily expensive or exceed model context limits.

Good retrieval systems therefore aim to provide relevant information rather than simply sending as much information as possible.

26. RAG and Fresh Information

One advantage of RAG is that external information can be updated independently from the generative model.

For example, a company can update a policy document and regenerate embeddings for the affected content.

The next retrieval operation can then use the updated information.

27. RAG Evaluation

A RAG system should be evaluated at multiple levels.

Retrieval evaluation asks whether the correct information was found.

Generation evaluation asks whether the final response accurately uses the retrieved information.

System evaluation can also consider latency, cost, security, user satisfaction, and failure behavior.

28. Common RAG Failure Modes

  • Relevant document was not retrieved
  • Incorrect document was retrieved
  • Retrieved chunks lack necessary context
  • Outdated information was retrieved
  • Unauthorized information entered the context
  • Too much irrelevant context was supplied
  • The model generated unsupported information
  • Source references were missing or incorrect

29. Monitoring a RAG System

Production systems should monitor both retrieval and generation behavior.

Useful measurements can include:

  • Retrieval latency
  • Generation latency
  • Empty retrieval rate
  • Similarity scores
  • Retrieval relevance
  • Model response quality
  • Token usage
  • Error rate
  • User feedback

30. Example: Company Policy Assistant

Imagine a company has an internal AI assistant.

Employees ask:

How many days of remote work are allowed each month?

The RAG system can search the company policy documents, retrieve the relevant policy section, provide that section as context to the AI model, and generate an answer based on the retrieved information.

The response can also identify the policy document used as the source.

31. RAG Does Not Replace Application Logic

RAG should not be treated as a replacement for authentication, authorization, validation, business rules, or database logic.

For example, an AI assistant might retrieve a document explaining how refunds work. The application should still enforce the actual permissions and business rules before processing a refund.

32. Security Considerations

RAG systems can introduce security risks if retrieved information is not controlled properly.

Important considerations include:

  • Access control
  • Data isolation
  • Protection of sensitive documents
  • Prompt injection within retrieved content
  • Secure logging
  • Data retention
  • Source validation

33. Prompt Injection in Retrieved Content

Retrieved documents should not automatically be treated as trusted instructions.

A malicious or compromised document could contain text designed to manipulate the AI model.

The application should clearly separate retrieved data from trusted application instructions and apply appropriate controls.

34. RAG Costs

A RAG request may involve several operations:

  1. Generate a query embedding.
  2. Search the retrieval system.
  3. Retrieve source content.
  4. Send context to the generative model.
  5. Generate the response.

Each stage can affect system cost and latency.

Efficient chunking and retrieval can reduce unnecessary context and improve overall performance.

35. Common Beginner Mistakes

  • Assuming RAG automatically prevents hallucinations
  • Ignoring retrieval quality
  • Using poor document chunks
  • Retrieving too much context
  • Ignoring document freshness
  • Failing to enforce access controls
  • Treating retrieved text as trusted instructions
  • Failing to evaluate the complete RAG pipeline

36. Developer Mental Model

A useful mental model is:

Source data → chunks → embeddings → retrieval → relevant context → AI model → validated response.

Every stage can affect the final result.

37. Where This Module Goes Next

The next lesson moves from retrieval architecture to building AI-powered applications. It will examine how developers combine AI models, application logic, data, interfaces, and supporting services into complete software features.

Conclusion

Retrieval-Augmented Generation connects generative AI with external information. It retrieves relevant content, supplies that content as context, and allows a generative model to produce a response using the retrieved information.

RAG is powerful, but it is not automatic knowledge or a guarantee of accuracy. Good RAG systems require high-quality source data, effective retrieval, appropriate chunking, metadata, access controls, evaluation, monitoring, and careful application design.

Key Takeaways

• RAG combines retrieval with generative AI • Retrieved information is supplied to the model as context • Embeddings and vector search are common components of RAG • Document quality and chunking strongly affect retrieval quality • Metadata and access controls are important for secure retrieval • RAG can improve grounding but does not guarantee factual accuracy • Retrieved content should not automatically be treated as trusted instructions • RAG systems need evaluation and monitoring across retrieval and generation

Try It Yourself

Design a conceptual RAG system for an internal company policy assistant. Describe the source documents, document-chunking approach, embedding process, vector storage, metadata filters, user-query flow, retrieved context, and final AI response. Identify three possible failure modes and one security control for each.

Test Your Knowledge

You've reached the end of this lesson.

Test what you've learned with the Lesson 121 Quiz: Retrieval-Augmented Generation (RAG).

Take the Quiz
← Vector Databases
Building AI-Powered Applications →
Back to Course