Understanding Retrieval-Augmented Generation
This lesson explains why RAG is used, the ingestion and retrieval stages, and how filters, freshness and caching shape a retrieval design.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why RAG is preferred to fine-tuning for changing knowledge.
- Describe the stages of ingestion.
- Explain why permissions are enforced with filters.
- Choose a vector store for a scenario.
Why RAG
A language model answers from what it learned during training. It knows nothing about a company's products, policies or customers, and it cannot know about changes after its training date. Retrieval-augmented generation solves this by retrieving relevant passages from the company's data at question time and placing them in the prompt, with an instruction to answer from that context. The model then generates an answer grounded in current, authorized data and can cite its sources.
RAG is preferred to fine-tuning for knowledge that changes, because updating an index is fast and cheap while retraining a model is slow and expensive, and because retrieval can respect each user's permissions. The back-end developer's part of RAG, which this exam covers, is the data side: preparing, storing, indexing, filtering and retrieving the passages efficiently.
RAG or fine-tuning?
The ingestion stage
Ingestion turns source content into searchable records. It runs when content is created or changed, often as an event-driven pipeline.
-
Extract
Read text from the source, such as documents, product records or support tickets.
-
Chunk
Split long content into passages small enough to embed and to fit several into a prompt.
-
Enrich
Attach metadata such as source document, title, tenant, category, language, date and allowed groups.
-
Embed
Generate an embedding for each chunk with the chosen model, in batches.
-
Store
Write text, metadata and vector together, keyed so that a changed document replaces its old chunks.
Chunking is a quality decision. Chunks that are too large dilute the embedding with several topics and waste prompt space; chunks that are too small lose context. A common starting point is a few hundred tokens per chunk with some overlap between neighbours, adjusted by evaluation. Structure-aware chunking, which splits on headings or paragraphs, usually beats fixed-size splitting for well-structured documents. Short records, such as a product with a name and a description, are often embedded whole.
The ingestion pipeline
The retrieval stage
At question time the application embeds the question with the same model, queries the vector store for the top k most similar chunks, typically 3 to 10, and builds the prompt. Several techniques improve what is retrieved.
- Metadata filters restrict candidates before or during the vector search, for example to one tenant, one product line or documents the user may read. Filters are how RAG enforces permissions, which a prompt instruction cannot do.
- Hybrid search combines vector similarity with keyword (full-text) matching, which helps with exact identifiers such as part numbers and error codes that embeddings handle poorly. Results are often merged with Reciprocal Rank Fusion (RRF).
- Similarity thresholds discard weak matches so that the model is not given irrelevant context.
- Reranking reorders a larger candidate set with a more precise model before the top few are used.
Every retrieved chunk should carry its source identifier, so that the answer can cite it and so that the application can log which data supported each answer.
The following diagram compares retrieval with only a prompt instruction and retrieval with a tenant filter.

Improving retrieval
Freshness, caching and cost
Retrieved data is only as current as the index. Event-driven ingestion keeps it fresh: the Cosmos DB change feed, an Event Grid BlobCreated event or a Service Bus message triggers re-embedding of changed items within seconds, rather than a nightly batch. Deleted content must also be removed from the index, which is easy to forget.
Two kinds of cache reduce cost and latency. An ordinary cache in Azure Managed Redis stores retrieval results or answers for identical questions. A semantic cache stores question embeddings with their answers and returns a cached answer when a new question is close enough in meaning, so "What are your opening hours?" and "When are you open?" can share one model call. Semantic caches must be scoped by user or tenant where answers depend on permissions.
Freshness and caching
Choosing where to store vectors
All three data services in the exam can serve the retrieval stage. The choice normally follows where the operational data already lives, because keeping vectors next to their data avoids a second copy that must be synchronized.
Where should the vectors live?
Retrieval that leaks data between customers
A software company's support assistant serves many customer tenants from one vector collection. A customer reports that an answer quoted another customer's private support ticket. The system prompt already says "Only use documents that belong to the current customer".
A prompt instruction cannot enforce access control: the leaked passage was retrieved and given to the model, which used it. Each chunk must carry a tenant identifier in its metadata, and every retrieval query must include a filter on that identifier taken from the authenticated user's session, so that other tenants' chunks are never candidates. In Cosmos DB, making the tenant ID the partition key also scopes each query to one partition, which lowers cost.
Lowering the number of results would reduce but not eliminate the leak. Adding a stronger instruction to the prompt would still give the model the private data. Separate embedding models per tenant would not stop a query from searching every tenant's vectors.
Why is a metadata filter, rather than a prompt instruction, the right way to stop RAG from using documents a user may not see?
Show answer
A filter keeps those documents out of the candidates entirely, so the model never receives them. A prompt instruction still gives the model the data and can be ignored or overridden.
Exam Tips
Remember for the exam
- RAG retrieves current, authorized data at question time; prefer it to fine-tuning for changing knowledge.
- Chunk content, attach metadata, embed in batches and store text, metadata and vector together.
- Enforce permissions with metadata filters in the query, never with prompt instructions.
- Keep the index fresh with events such as the change feed, and remove deleted content.