TechByteByByte

Production RAG

The final module of this course: observability, cost, latency, caching, the complete decision framework, common mistakes, case studies, and the final, unified mental model for everything RAG.

#RAG#AI#Production#Interview Prep#Level 8

Begin with the problem

A demo answers one question; production RAG survives changing documents, many users, failures, security checks, cost limits, and continuous quality measurement.

evaluation set + production traces โ†’ metrics โ†’ diagnosis โ†’ safer improvement

What you will learn

  • Explain Production RAG in simple language before using its technical details.
  • Follow the mechanism step by step through a small RAG example.
  • Connect this topic to the modules before and after it.
  • Decide when to use it, when not to use it, and what to measure in production.

Current real-system grounding: OpenAIโ€™s evaluation guide and Googleโ€™s File Search documentation ground the production practices discussed here. Limits, costs, and supported models change, so verify them before deployment.

The product example proves that the pattern is used in a real system. It does not mean every provider uses the same hidden algorithm, defaults, limits, or pricing.

1. The problem this module solves

This is the final module of a 33-module course. Its job is threefold: cover the remaining real production concerns (observability, cost, latency, caching), consolidate the architectural decision framework this course has built module by module, and close with the complete, unified mental model tying everything together.


2. RAG Observability โ€” What to Log

For EVERY request, a production system should really log:

request_id, user_id, query, retrieved_chunks, similarity_scores,
reranker_scores, final_context, prompt, model, response, citations,
latency, token_usage, cost

Without this level of observability, you know an answer was wrong. WITH it, you can really apply Module 24โ€™s diagnostic process directly against real logged data โ€” distinguishing a retrieval failure from a ranking failure from a generation failure, using ACTUAL production evidence, not guesswork.


3. RAG Cost โ€” Where It Actually Comes From

Embedding generation (Module 10) -- cost PER document AND per query
Vector database storage and search (Module 12-14)
Reranking (Module 18) -- really more expensive per comparison
Query rewriting/decomposition (Modules 19-20) -- ADDITIONAL LLM calls
LLM input tokens (the constructed context, Module 21 -- can be
                  substantial)
LLM output tokens
Multiple retrieval calls (Corrective/Agentic RAG, Modules 28-29)

Every โ€œadvancedโ€ technique this course covered โ€” hybrid search, reranking, query transformation, decomposition, self-correction โ€” adds REAL cost on top of naive RAGโ€™s baseline. This is precisely why Module 28โ€™s naive-vs-advanced framing matters practically: use the advanced techniques where they REALLY earn their cost, not by default everywhere.


4. RAG Latency โ€” Breaking Down the Total

Query processing (Module 19)
+ Embedding (Module 10)
+ Retrieval (Modules 13-14)
+ Reranking (Module 18)
+ LLM generation (Module 22)
===
Total latency
Optimization levers:

- CACHING (Section 5, next)
- Smaller/faster embedding models where accuracy allows
- Efficient ANN indexing (Module 14's HNSW tuning)
- Reasonable top-k (Module 15) -- not excessively large
- Streaming the final generation (your Generative AI course, Module
  25)

5. RAG Caching โ€” Four Real Layers

QUERY caching:      identical query -> reuse the cached RESULT
                   entirely

EMBEDDING caching:      identical TEXT -> reuse the embedding,
                       don't recompute

RETRIEVAL caching:          identical query -> reuse the retrieved
                           CHUNKS (skip the search step, but still
                           run generation fresh)

RESPONSE caching:               identical FULL REQUEST -> reuse the
                              complete response, where REALLY
                              safe (be careful: this can serve stale
                              answers if the underlying knowledge
                              base has since changed, Module 26)

6. The Complete Architectural Decision Framework โ€” This Course,

Consolidated

Is the knowledge STRUCTURED (precise, computable)?
   YES -> SQL / structured query (Module 30)

Is it UNSTRUCTURED, but fundamentally about RELATIONSHIPS?
   YES -> Graph RAG (Module 29)

Is it UNSTRUCTURED, conceptual/explanatory content?
   YES -> Standard RAG (Modules 5-27)

Do EXACT terms really matter (IDs, codes)?
   YES -> Include BM25 / sparse retrieval (Module 16)

Does SEMANTIC meaning really matter?
   YES -> Include dense/vector retrieval (Modules 10-14)

Do BOTH matter?
   YES -> Hybrid search (Module 17)

Is initial retrieval REALLY noisy or imprecise?
   YES -> Add reranking (Module 18)

Are QUESTIONS really complex or multi-part?
   YES -> Query decomposition (Module 20)

Do RELATIONSHIPS between entities really matter?
   YES -> Graph RAG (Module 29)

Does retrieval QUALITY need active verification?
   YES -> Self-RAG / Corrective RAG (Module 28)

7. When NOT to Use RAG

Really SIMPLE, small, static knowledge base that fits directly in
a prompt -> just include it directly, no retrieval infrastructure
needed

Data already available DIRECTLY through a clean API -> call the API,
skip the retrieval layer entirely

PURE creative generation, no external knowledge needed -> RAG adds
NOTHING here

Problem is fundamentally about CONSISTENT STYLE/behavior -> fine-
                                                            tuning
                                                            (Module
                                                            3) is the
                                                            really
                                                            better
                                                            fit

Donโ€™t add RAG because itโ€™s popular โ€” add it because the specific problem REALLY requires external, current, or large-scale knowledge, exactly Module 1โ€™s original problem statement.


8. The Consolidated Mistake Catalog

1. Fixed chunk size for EVERY document type (Module 7)
2. Ignoring document structure when chunking (Module 8)
3. Using ONLY vector search (Module 16-17)
4. Retrieving too many or too few chunks (Module 15)
5. No reranking for really noisy retrieval (Module 18)
6. No metadata filtering (Module 15)
7. Ignoring access control (Module 27)
8. Search-then-filter instead of filter-then-search (Module 15)
9. Sending raw retrieved text directly into prompts (Module 21)
10. Ignoring conflicting documents (Module 25)
11. Assuming RAG eliminates hallucination (Module 25)
12. Not evaluating retrieval and generation SEPARATELY (Module 32)
13. No citations, or trusting citations without verification (Module
    23)
14. No document versioning/freshness strategy (Module 26)
15. No observability (Section 2, this module)
16. Treating every problem as a vector-embedding problem (Module 30)
17. Naively flattening tables (Module 6, 30)
18. Trusting retrieved content as inherently safe (Module 27)
19. Full reindexing when incremental would really suffice (Module
    26)
20. Using RAG when fine-tuning or direct prompting is the really
    better fit (Module 3, this module's Section 7)

9. Case Study โ€” A Complete, Worked Enterprise RAG System

TechCorp HR Assistant, built using THIS ENTIRE course:

1. INGESTION (Module 5): HR policies, engineering wikis, loaded with
   full metadata

2. CHUNKING (Modules 7-9): structure-aware for policies (respects
   headings), recursive for less-structured content

3. EMBEDDING + INDEXING (Modules 10-14): HNSW index for low-latency
   search at real scale

4. HYBRID SEARCH (Modules 16-17): BM25 + dense retrieval, combined
   via RRF

5. RERANKING (Module 18): cross-encoder-style refinement of the
   top-20 candidates

6. QUERY TRANSFORMATION (Modules 19-20): rewrites conversational
   questions, decomposes really multi-part ones

7. CONTEXT CONSTRUCTION (Module 21): deduplicated, filtered, ordered
   with most-relevant-first

8. PROMPT CONSTRUCTION (Module 22): grounding instructions, citation
   requirements

9. GENERATION + VERIFICATION (Module 23): groundedness-checked before
   the answer is shown

10. CONFLICT HANDLING (Module 25): metadata-based resolution when
    policies disagree

11. VERSIONING (Module 26): superseded policies automatically
    excluded from default search

12. ACCESS CONTROL (Module 27): permission-filtered BEFORE retrieval

13. EVALUATION (Module 32): golden dataset, re-run before every
    deployment

Every SINGLE module in this course maps to a REAL, functioning
component of this ONE system.

10. Interview Masterclass โ€” Scenario-Based Questions

Q: Design a production-grade RAG system for a company with 5 million documents, strict access control requirements, and real latency sensitivity.

Ans: Iโ€™d start with format-specific ingestion loaders (Module 5) capturing full metadata including access permissions. For chunking, Iโ€™d use structure-aware chunking for well-formatted documents and recursive chunking as a fallback (Modules 7-8). At 5 million documents, brute-force search is really too slow, so Iโ€™d use HNSW indexing (Module 14) for its favorable latency characteristics, combined with hybrid search (Module 17) to handle both conceptual and exact-match queries.

Access control would be enforced as a mandatory filter-then-search step (Modules 15, 27) before any similarity ranking occurs. Iโ€™d add reranking (Module 18) on the narrowed candidate set for precision, construct context with deduplication and relevance-ordering (Module 21), and maintain golden-dataset evaluation (Module 32) to catch regressions before deployment.

Q: How would you diagnose a RAG system that started giving worse answers after a knowledge base update?

Ans: Iโ€™d apply Module 24โ€™s backward diagnostic process using logged data (Section 2, this module) โ€” first checking whether really relevant chunks are still being retrieved at all after the update, then whether theyโ€™re ranking highly enough to survive top-k, then whether context construction is including them, and finally whether generation is using them correctly. Iโ€™d also specifically check for Module 26โ€™s versioning concern โ€” whether the update properly superseded old content or left conflicting old and new versions both searchable (Module 25โ€™s conflict scenario).

Q: When would you recommend against using RAG for a given application?

Ans: If the knowledge base is small enough to fit entirely in a prompt, if the needed data is already cleanly available through a direct API, if the task is purely creative generation without real external knowledge needs, or if the actual requirement is consistent behavioral style rather than factual grounding (better suited to fine-tuning) โ€” in any of these cases, RAGโ€™s real architectural complexity isnโ€™t justified by the problemโ€™s actual requirements.

Q: Walk through how youโ€™d evaluate whether switching from IVF to HNSW indexing is worth it for a specific application.

Ans: Iโ€™d measure both approachesโ€™ recall and latency against a representative sample of the actual production data and query patterns (Module 13โ€™s speed/recall trade-off, Module 14โ€™s specific algorithms), since the right choice depends on real characteristics of the data and workload, not a universal default. Iโ€™d also weigh HNSWโ€™s typically lower latency and higher memory usage against IVFโ€™s simpler, more memory-efficient structure (Module 14โ€™s comparison), considering which trade-off really matters more for this specific applicationโ€™s real constraints.


11. The Final, Unified Mental Model

                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚      Documents       โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ†“
                    Ingestion (Module 5) + Parsing (Module 6)
                               โ†“
                    Chunking (Modules 7-9)
                               โ†“
                    Embedding (Module 10)
                               โ†“
                    Vector Index -- HNSW/IVF (Modules 12-14)
                               โ”‚
User Question โ”€โ”€โ†’ Query Transformation (Modules 19-20)
                               โ†“
                    Metadata Filtering (Module 15, 27)
                               โ†“
                    Hybrid Retrieval (Modules 16-17)
                               โ†“
                    Reranking (Module 18)
                               โ†“
                    Context Construction (Module 21)
                               โ†“
                    Prompt Construction (Module 22)
                               โ†“
                    LLM Generation
                               โ†“
                    Groundedness Verification (Module 23)
                               โ†“
                    Citations
                               โ†“
                    Answer

Surrounded throughout by: Evaluation (Module 32), Observability
(Section 2), Security (Module 27), Versioning (Module 26), Conflict
Handling (Module 25)

The single most important idea from this entire course, stated one final time: RAG is not โ€œput documents into a vector database and ask an LLM.โ€ It is a real, end-to-end information retrieval and generation SYSTEM โ€” where quality is the product of ingestion quality, chunking quality, retrieval quality, ranking quality, context quality, generation quality, evaluation, and security, TOGETHER. A good LLM cannot compensate for a weak link anywhere else in this chain.


12. Code โ€” A Final, Integrated Architecture Recommendation Tool

๐ŸŽฏ Target of this example: implement Section 6โ€™s complete decision framework as one working recommendation function, directly demonstrating how the entire courseโ€™s architectural guidance combines into a single, practical decision-support tool for a really realistic system requirement.

Example 1 โ€” Simple

def recommend_rag_architecture(
    knowledge_base_size: str, needs_current_facts: bool, needs_relationships: bool,
    has_structured_data: bool, latency_sensitive: bool,
) -> dict:
    """Directly implements Section 6's complete decision framework --
    combining EVERY module's architectural guidance into one
    recommendation function."""
    components = []

    if needs_relationships:
        components.append("Graph RAG (Module 29)")
    if has_structured_data:
        components.append("Text-to-SQL (Module 30)")
    if needs_current_facts:
        components.append("Standard vector RAG (Modules 10-18)")

    if knowledge_base_size == "large":
        components.append("ANN indexing -- HNSW or IVF (Module 14)")
        components.append("Hybrid search (Module 17)")
        components.append("Two-stage reranking (Module 18)")

    if latency_sensitive:
        components.append("Prioritize HNSW over IVF for lower latency (Module 14)")

    return {"recommended_components": components}

result = recommend_rag_architecture(
    knowledge_base_size="large", needs_current_facts=True, needs_relationships=False,
    has_structured_data=True, latency_sensitive=True,
)
for component in result["recommended_components"]:
    print(f"  - {component}")

Expected Output:

  - Text-to-SQL (Module 30)
  - Standard vector RAG (Modules 10-18)
  - ANN indexing -- HNSW or IVF (Module 14)
  - Hybrid search (Module 17)
  - Two-stage reranking (Module 18)
  - Prioritize HNSW over IVF for lower latency (Module 14)

What we conclude from this example: this single function draws together guidance from Modules 14, 17, 18, and 30 into one coherent recommendation, based purely on a systemโ€™s real, stated requirements โ€” exactly the kind of decision support Section 6โ€™s consolidated framework enables.

Example 2 โ€” Intermediate

from dataclasses import dataclass

@dataclass
class SystemRequirements:
    knowledge_base_size: str
    needs_current_facts: bool
    needs_relationships: bool
    has_structured_data: bool
    latency_sensitive: bool
    handles_sensitive_data: bool

def recommend_full_architecture(req: SystemRequirements) -> dict:
    """Extends Example 1 with Section 8's mistake-avoidance checks
    and Module 27's security requirement, producing a MORE complete
    recommendation."""
    components = []
    warnings = []

    if req.needs_relationships:
        components.append("Graph RAG (Module 29)")
    if req.has_structured_data:
        components.append("Text-to-SQL (Module 30)")

    components.append("Standard vector RAG (Modules 10-18)")

    if req.knowledge_base_size == "large":
        components.append("ANN indexing -- HNSW or IVF (Module 14)")
        components.append("Hybrid search (Module 17)")
        components.append("Two-stage reranking (Module 18)")

    if req.handles_sensitive_data:
        components.append("MANDATORY access control filtering (Module 15, 27)")
        warnings.append("Sensitive data detected -- access control is NON-NEGOTIABLE, not optional.")

    if req.latency_sensitive and req.knowledge_base_size == "large":
        components.append("Caching layer (Section 5, this module)")

    return {"components": components, "warnings": warnings}

requirements = SystemRequirements(
    knowledge_base_size="large", needs_current_facts=True, needs_relationships=False,
    has_structured_data=False, latency_sensitive=True, handles_sensitive_data=True,
)

result = recommend_full_architecture(requirements)
print("Recommended components:")
for c in result["components"]:
    print(f"  - {c}")
print("\nWarnings:")
for w in result["warnings"]:
    print(f"  โš ๏ธ  {w}")

Expected Output:

Recommended components:
  - Standard vector RAG (Modules 10-18)
  - ANN indexing -- HNSW or IVF (Module 14)
  - Hybrid search (Module 17)
  - Two-stage reranking (Module 18)
  - MANDATORY access control filtering (Module 15, 27)
  - Caching layer (Section 5, this module)

Warnings:
  โš ๏ธ  Sensitive data detected -- access control is NON-NEGOTIABLE,
  not optional.

What we conclude from this example: the recommendation function explicitly flags access control as non-negotiable when sensitive data is involved, directly reflecting Module 27โ€™s real security requirement rather than treating it as just another optional component โ€” exactly the kind of guardrail a real architectural decision tool should enforce.

Example 3 โ€” Production Grade

from dataclasses import dataclass, field
from enum import Enum

class ArchitectureComponent(Enum):
    GRAPH_RAG = "Graph RAG (Module 29)"
    TEXT_TO_SQL = "Text-to-SQL (Module 30)"
    VECTOR_RAG = "Standard vector RAG (Modules 10-18)"
    ANN_INDEXING = "ANN indexing (Module 14)"
    HYBRID_SEARCH = "Hybrid search (Module 17)"
    RERANKING = "Two-stage reranking (Module 18)"
    ACCESS_CONTROL = "Mandatory access control (Module 15, 27)"
    EVALUATION = "Golden dataset evaluation (Module 32)"
    VERSIONING = "Document versioning/freshness (Module 26)"

@dataclass
class ArchitectureRecommendation:
    components: list = field(default_factory=list)
    should_use_rag_at_all: bool = True
    rationale_if_not: str = ""

def full_course_recommendation(
    knowledge_base_size: str, is_small_and_static: bool, has_direct_api_access: bool,
    needs_relationships: bool, has_structured_data: bool, handles_sensitive_data: bool,
    documents_change_frequently: bool,
) -> ArchitectureRecommendation:
    """The FINAL, most complete recommendation function --
    incorporating Section 7's 'when NOT to use RAG' check FIRST,
    before recommending any RAG-specific architecture at all."""

    # Section 7's check, applied FIRST
    if is_small_and_static:
        return ArchitectureRecommendation(
            should_use_rag_at_all=False,
            rationale_if_not="Knowledge base is small and static -- just include it directly in the prompt.")
    if has_direct_api_access:
        return ArchitectureRecommendation(
            should_use_rag_at_all=False,
            rationale_if_not="Data is already available through a clean API -- call it directly, skip retrieval infrastructure.")

    components = [ArchitectureComponent.VECTOR_RAG]

    if needs_relationships:
        components.append(ArchitectureComponent.GRAPH_RAG)
    if has_structured_data:
        components.append(ArchitectureComponent.TEXT_TO_SQL)
    if knowledge_base_size == "large":
        components.extend([ArchitectureComponent.ANN_INDEXING, ArchitectureComponent.HYBRID_SEARCH,
                            ArchitectureComponent.RERANKING])
    if handles_sensitive_data:
        components.append(ArchitectureComponent.ACCESS_CONTROL)
    if documents_change_frequently:
        components.append(ArchitectureComponent.VERSIONING)

    components.append(ArchitectureComponent.EVALUATION)  # ALWAYS recommended

    return ArchitectureRecommendation(components=components, should_use_rag_at_all=True)

# TechCorp's HR assistant -- this course's recurring case study
recommendation = full_course_recommendation(
    knowledge_base_size="large", is_small_and_static=False, has_direct_api_access=False,
    needs_relationships=False, has_structured_data=False, handles_sensitive_data=True,
    documents_change_frequently=True,
)

print(f"Should use RAG: {recommendation.should_use_rag_at_all}")
print("Recommended architecture:")
for component in recommendation.components:
    print(f"  - {component.value}")

Expected Output:

Should use RAG: True
Recommended architecture:
  - Standard vector RAG (Modules 10-18)
  - ANN indexing (Module 14)
  - Hybrid search (Module 17)
  - Two-stage reranking (Module 18)
  - Mandatory access control (Module 15, 27)
  - Document versioning/freshness (Module 26)
  - Golden dataset evaluation (Module 32)

What we conclude from this example: this final function integrates Section 7โ€™s โ€œshould we even use RAGโ€ check with the full architectural recommendation โ€” for TechCorpโ€™s actual HR assistant requirements (large knowledge base, sensitive data, frequently changing documents), it correctly recommends the exact combination of components this entire course built up to, module by module, with evaluation ALWAYS included as a really non-negotiable practice regardless of other requirements.


13. What You Should Remember โ€” The Complete Course

  • RAG is a real end-to-end information retrieval and generation system โ€” ingestion, parsing, chunking, embedding, indexing, retrieval, ranking, context construction, generation, verification, and security, working together.
  • A good LLM cannot compensate for a weak link anywhere earlier in this chain โ€” the single most important diagnostic and design principle from this entire course.
  • Production readiness requires observability, cost/latency awareness, caching, and real ongoing evaluation โ€” verified directly through a complete architecture recommendation tool synthesizing every moduleโ€™s guidance into one working decision system.

14. Course Complete

This concludes the 33-module Retrieval-Augmented Generation course. From Module 1โ€™s foundational question โ€” why canโ€™t an LLM alone answer questions about private, current, or large-scale knowledge โ€” through chunking, embeddings, vector indexing (including the real, layer-by-layer mechanics of HNSW and IVF), hybrid search, reranking, grounded generation, advanced architectures, and finally production engineering, you now have a complete, mechanism-grounded understanding of RAG โ€” really ready to design, build, evaluate, and responsibly operate real retrieval-augmented systems, and to reason confidently about how every component fits together as one coherent whole.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed