Skip to content

12 โ€” LlamaIndex RAG Pipelines

Learn how to build, customize, evaluate, and productionize Retrieval-Augmented Generation pipelines using LlamaIndex, from simple vector RAG to enterprise-grade retrieval, context construction, response synthesis, citations, metadata filtering, and observability.


๐Ÿ“– Overview

Retrieval-Augmented Generation (RAG) combines information retrieval with Large Language Models.

Instead of relying only on the model's internal knowledge:

User Query
    โ†“
Retrieve Relevant Information
    โ†“
Build Context
    โ†“
LLM
    โ†“
Grounded Response

LlamaIndex provides abstractions for building this pipeline around enterprise data.

A simplified LlamaIndex RAG architecture is:

                    Enterprise Data
                          โ†“
                    Data Ingestion
                          โ†“
                       Documents
                          โ†“
                         Nodes
                          โ†“
                    Index / Storage
                          โ†“
                       Retriever
                          โ†“
                   Relevant Context
                          โ†“
                 Response Synthesizer
                          โ†“
                          LLM
                          โ†“
                     Final Answer

The objective of a production RAG pipeline is not simply:

Retrieve โ†’ Generate

but rather:

Retrieve the right information
        +
Respect security boundaries
        +
Construct useful context
        +
Generate a grounded response
        +
Provide traceability

๐ŸŽฏ Learning Objectives

After completing this chapter, you will be able to:

  • Understand the architecture of LlamaIndex RAG pipelines
  • Build a basic LlamaIndex RAG application
  • Understand ingestion and retrieval boundaries
  • Configure vector-based RAG
  • Configure retrievers
  • Use query engines
  • Understand response synthesis
  • Apply metadata filtering
  • Build citation-aware RAG
  • Understand source attribution
  • Design multi-stage RAG pipelines
  • Combine retrieval with post-processing
  • Separate retrieval from generation
  • Evaluate RAG pipelines
  • Monitor RAG pipelines in production
  • Optimize RAG latency and cost
  • Design enterprise-grade RAG architectures
  • Understand common LlamaIndex RAG failure patterns

1. What Is RAG?

Retrieval-Augmented Generation combines:

Retrieval
+
Generation

The retrieval layer finds relevant external information.

The generation layer uses that information to produce the response.

                 RAG
                  โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ–ผ                   โ–ผ
    Retrieval           Generation
        โ”‚                   โ”‚
        โ–ผ                   โ–ผ
Relevant Context          LLM
        โ”‚                   โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ–ผ
               Answer

2. Why RAG Is Needed

LLMs have limitations.

They may not know:

Private Enterprise Data
Recently Updated Policies
Internal Documentation
Customer-Specific Information
Operational Data
Company Procedures

RAG provides an external knowledge layer:

Enterprise Knowledge
        โ†“
      Retrieval
        โ†“
      Context
        โ†“
        LLM

3. LlamaIndex RAG Mental Model

A useful mental model is:

LOAD
 โ†“
TRANSFORM
 โ†“
INDEX
 โ†“
RETRIEVE
 โ†“
RANK / FILTER
 โ†“
BUILD CONTEXT
 โ†“
GENERATE
 โ†“
CITE

Each stage should be independently observable and testable.


4. End-to-End RAG Pipeline

flowchart TB

    A[Enterprise Documents] --> B[Data Connectors]

    B --> C[Documents]

    C --> D[Transformations]

    D --> E[Nodes]

    E --> F[Embeddings]

    F --> G[(Vector Store)]

    H[User Query] --> I[Query Engine]

    I --> J[Retriever]

    J --> G

    G --> K[Candidate Nodes]

    K --> L[Post Processing]

    L --> M[Context Builder]

    M --> N[Response Synthesizer]

    N --> O[LLM]

    O --> P[Grounded Response]

5. Offline vs Online RAG

A production RAG system normally has two paths.

Offline Path

Documents
 โ†“
Parse
 โ†“
Chunk
 โ†“
Metadata
 โ†“
Embedding
 โ†“
Index

Online Path

User Query
 โ†“
Retrieve
 โ†“
Filter / Rank
 โ†“
Context
 โ†“
LLM
 โ†“
Response

6. RAG Architecture

flowchart LR

    A[Enterprise Sources] --> B[Ingestion Pipeline]

    B --> C[(Vector Index)]

    D[User] --> E[Query API]

    E --> F[Retriever]

    F --> C

    F --> G[Context Builder]

    G --> H[LLM]

    H --> I[Response]

Keeping the two paths separate allows ingestion workloads to scale independently from user-facing query workloads.


7. Basic LlamaIndex RAG

A minimal implementation can be:

from llama_index.core import (
    SimpleDirectoryReader,
    VectorStoreIndex
)

# Load documents
documents = SimpleDirectoryReader(
    "data"
).load_data()

# Build index
index = VectorStoreIndex.from_documents(
    documents
)

# Create query engine
query_engine = index.as_query_engine()

# Query
response = query_engine.query(
    "What is the company's security policy?"
)

print(response)

The conceptual flow is:

Documents
 โ†“
VectorStoreIndex
 โ†“
Query Engine
 โ†“
Retriever
 โ†“
LLM
 โ†“
Response

8. What Happens During RAG?

When the user asks:

"What is the company's password policy?"

the system performs approximately:

1. Receive Query
        โ†“
2. Convert Query to Search Representation
        โ†“
3. Search Index
        โ†“
4. Retrieve Relevant Nodes
        โ†“
5. Build Context
        โ†“
6. Construct Prompt
        โ†“
7. Call LLM
        โ†“
8. Generate Response

9. Query Engine

The query engine provides a high-level abstraction for querying indexed data.

query_engine = index.as_query_engine()

response = query_engine.query(
    "Explain the password policy."
)

Conceptually:

Query Engine
 โ”œโ”€โ”€ Query Processing
 โ”œโ”€โ”€ Retriever
 โ”œโ”€โ”€ Context Construction
 โ”œโ”€โ”€ Response Synthesis
 โ””โ”€โ”€ LLM

10. Retriever

The retriever is responsible for finding relevant nodes.

retriever = index.as_retriever(
    similarity_top_k=5
)

nodes = retriever.retrieve(
    "What is the password policy?"
)

The retriever returns candidate information.

It does not necessarily generate the final answer.


11. Retriever vs Query Engine

Retriever

Query
 โ†“
Relevant Nodes

Query Engine

Query
 โ†“
Retriever
 โ†“
Context
 โ†“
LLM
 โ†“
Answer

Therefore:

Retriever
=
Information Retrieval

Query Engine
=
Retrieval + Generation

12. RAG Context

The retrieved nodes become context for the LLM.

Example:

Question:
"What is the password expiry period?"

Retrieved Context:

"Employee passwords must be changed every
90 days unless an approved exception exists."

The LLM then receives:

Question
+
Retrieved Context

and generates the response.


13. Context Construction

A production pipeline should not blindly concatenate every retrieved node.

Instead:

Retrieved Nodes
      โ†“
Filter
      โ†“
Deduplicate
      โ†“
Rank
      โ†“
Select
      โ†“
Context

14. Context Builder

flowchart LR

    A[Retrieved Nodes] --> B[Metadata Filtering]

    B --> C[Deduplication]

    C --> D[Ranking]

    D --> E[Context Selection]

    E --> F[Prompt]

    F --> G[LLM]

15. Top-K Retrieval

Example:

retriever = index.as_retriever(
    similarity_top_k=5
)

This asks the retriever to return approximately the top five candidates according to its retrieval strategy.

The correct value should be determined through evaluation.


16. Top-K Trade-Off

Small K:

Lower Latency
Lower Cost
Less Noise

but:

Potentially Lower Recall

Large K:

Higher Recall

but:

More Noise
More Tokens
Higher Latency
Higher Cost

Therefore:

Top-K
=
Quality / Cost / Latency Trade-Off

17. Metadata Filtering

Metadata can constrain retrieval.

Example:

metadata = {
    "tenant_id": "tenant-001",
    "department": "finance",
    "document_type": "policy"
}

The conceptual query becomes:

User Query
+
Tenant Filter
+
Department Filter
+
Document Type Filter
 โ†“
Retriever

18. Metadata-Aware RAG

flowchart TD

    A[User] --> B[Authentication]

    B --> C[Authorization]

    C --> D[Query API]

    D --> E[Tenant Resolution]

    E --> F[Metadata Filters]

    F --> G[Retriever]

    G --> H[(Vector Store)]

    H --> I[Authorized Nodes]

    I --> J[Context Builder]

    J --> K[LLM]

    K --> L[Response]

19. Security Boundary

A critical enterprise principle:

Retrieved
โ‰ 
Authorized

The application must determine what the user is allowed to access before retrieval context reaches the model.

The model should never be responsible for deciding:

"Is this user allowed to see this document?"

20. Tenant-Aware RAG

For multi-tenant applications:

User
 โ†“
Tenant Identification
 โ†“
Authorization
 โ†“
Tenant Filter
 โ†“
Retriever
 โ†“
Vector Store

Example metadata:

{
    "tenant_id": "tenant-001",
    "document_id": "DOC-1001"
}

21. Multi-Tenant RAG Architecture

flowchart TB

    A[User] --> B[Authentication]

    B --> C[Tenant Resolution]

    C --> D[Authorization]

    D --> E[Query Engine]

    E --> F[Tenant-Aware Retriever]

    F --> G[(Shared Vector Store)]

    G --> H[Authorized Tenant Context]

    H --> I[LLM]

    I --> J[Response]

22. Prompt Construction

A RAG prompt conceptually contains:

System Instructions

+
Retrieved Context

+
User Question

Example:

System:
Answer using only the supplied context.

Context:
Employee passwords must be changed every 90 days.

Question:
How often must employees change passwords?

23. Grounded Generation

A strong RAG prompt should establish the relationship between:

Context

and:

Answer

Conceptually:

Context
 โ†“
Reason over supplied information
 โ†“
Answer

This can reduce unsupported answers, although prompting alone cannot eliminate hallucination.


24. Response Synthesis

LlamaIndex provides response synthesis abstractions for turning retrieved information into a final response.

Conceptually:

Retrieved Nodes
       โ†“
Response Synthesizer
       โ†“
LLM
       โ†“
Final Answer

25. Response Synthesis Architecture

flowchart LR

    A[Query] --> B[Retriever]

    B --> C[Retrieved Nodes]

    C --> D[Response Synthesizer]

    D --> E[Prompt Construction]

    E --> F[LLM]

    F --> G[Response]

26. Why Response Synthesis Matters

Retrieval returns information.

It does not automatically determine:

How the answer should be structured

Response synthesis can coordinate:

Retrieved Context
+
Query
+
Prompt
+
LLM

to create the final response.


27. Response Modes

Depending on the pipeline and LlamaIndex version, response synthesis can use different approaches for combining retrieved information.

Conceptually:

Compact
Refine
Tree-Based
Summarization

The appropriate strategy depends on:

Context Size
Question Complexity
Latency
Cost
Answer Requirements

28. Compact-Style Synthesis

Conceptually:

Node 1
Node 2
Node 3
Node 4
   โ†“
Combine Context
   โ†“
One LLM Call
   โ†“
Answer

Advantages:

Simple
Fast
Lower Number of LLM Calls

Constraint:

Context Window

29. Refine-Style Synthesis

Conceptually:

Query + Node 1
      โ†“
Initial Answer
      โ†“
Refine with Node 2
      โ†“
Refine with Node 3
      โ†“
Final Answer

Architecture:

flowchart LR

    A[Query + Node 1] --> B[LLM]

    B --> C[Initial Answer]

    C --> D[+ Node 2]

    D --> E[LLM]

    E --> F[Refined Answer]

    F --> G[+ Node 3]

    G --> H[LLM]

    H --> I[Final Answer]

This can process information progressively but may require more model calls.


30. Tree-Oriented Synthesis

A hierarchical strategy can summarize information progressively.

Node 1 โ”€โ”
Node 2 โ”€โ”ค
        โ”œโ”€โ”€ Summary A
Node 3 โ”€โ”ค
Node 4 โ”€โ”˜

Node 5 โ”€โ”
Node 6 โ”€โ”ค
        โ”œโ”€โ”€ Summary B
Node 7 โ”€โ”ค
Node 8 โ”€โ”˜

Summary A + Summary B
        โ†“
      LLM
        โ†“
     Answer

This can be useful for large collections of retrieved information.


31. Citation-Aware RAG

Enterprise applications often need:

Answer
+
Source

Example:

The password policy requires a password change
every 90 days.

Source:
security-policy.pdf
Page 14

Source attribution improves:

Trust
Auditability
Verification
User Experience

32. Source Metadata

A node can retain information such as:

metadata = {
    "document_id": "SEC-001",
    "source": "security-policy.pdf",
    "page": 14,
    "section": "Password Policy"
}

This allows the response layer to associate retrieved content with its source.


33. Citation Architecture

flowchart TB

    A[User Query] --> B[Retriever]

    B --> C[Node]

    C --> D[Text]

    C --> E[Source Metadata]

    D --> F[Context]

    E --> G[Citation Metadata]

    F --> H[LLM]

    H --> I[Answer]

    G --> J[Source Attribution]

    I --> K[Final Response]

    J --> K

34. RAG With Citations

Conceptually:

for node in nodes:
    print(
        node.node.text,
        node.node.metadata
    )

The application can then use:

document_id
source
page
section

to construct citations.


35. Query Transformation

A user query may not always be ideal for retrieval.

Example:

"How do we protect employee accounts?"

could be transformed into:

employee account security
password policy
authentication controls
access management

The retrieval pipeline can then search using improved query representations.


36. Query Transformation Pipeline

flowchart LR

    A[User Query] --> B[Query Transformation]

    B --> C[Retrieval Query]

    C --> D[Retriever]

    D --> E[Relevant Nodes]

    E --> F[Context]

    F --> G[LLM]

Advanced query transformation strategies belong to the broader production retrieval layer.


37. Multi-Query RAG

One query may produce multiple retrieval perspectives.

User Query
    โ†“
 โ”Œโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ–ผ  โ–ผ           โ–ผ
Q1  Q2          Q3
 โ”‚   โ”‚           โ”‚
 โ””โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
     โ–ผ
Result Fusion
     โ†“
Context
     โ†“
LLM

This can improve recall for ambiguous or complex questions.


38. Multi-Query Architecture

flowchart TD

    A[User Query] --> B[Query Generator]

    B --> C[Query 1]
    B --> D[Query 2]
    B --> E[Query 3]

    C --> F[Retriever]
    D --> F
    E --> F

    F --> G[Result Fusion]

    G --> H[Deduplication]

    H --> I[Context]

    I --> J[LLM]

39. Hybrid RAG

Hybrid RAG combines:

Semantic Retrieval
+
Keyword Retrieval

Example:

User Query
     โ”‚
 โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
 โ–ผ        โ–ผ
Vector   Keyword
Search   Search
 โ”‚        โ”‚
 โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜
     โ–ผ
Result Fusion
     โ†“
Context
     โ†“
LLM

This is especially useful for:

Technical Terms
Product Codes
Policy Numbers
Legal References
Acronyms

40. Contextual Compression

Retrieved nodes may contain more information than the question requires.

Example:

Retrieved Node
=
2000 tokens

Relevant Content
=
300 tokens

Compression can reduce context before generation.

Retrieved Nodes
 โ†“
Relevant Content Extraction
 โ†“
Compressed Context
 โ†“
LLM

41. Compression Architecture

flowchart LR

    A[Retrieved Nodes] --> B[Contextual Compression]

    B --> C[Relevant Information]

    C --> D[Prompt]

    D --> E[LLM]

    E --> F[Response]

42. Parent-Child Context

Small chunks improve retrieval precision but may lose context.

A production strategy can retrieve:

Child Chunk

and then expand to:

Parent Section

Conceptually:

Document
 โ”œโ”€โ”€ Parent Section
 โ”‚    โ”œโ”€โ”€ Child Chunk 1
 โ”‚    โ”œโ”€โ”€ Child Chunk 2
 โ”‚    โ””โ”€โ”€ Child Chunk 3

Query:

Retrieve Child Chunk 2
        โ†“
Expand Parent Section
        โ†“
LLM Context

43. Parent-Child RAG

flowchart TD

    A[Document] --> B[Parent Section]

    B --> C[Child Chunk 1]
    B --> D[Child Chunk 2]
    B --> E[Child Chunk 3]

    F[Query] --> G[Retriever]

    G --> D

    D --> H[Parent Expansion]

    H --> B

    B --> I[Context]

    I --> J[LLM]

44. RAG With Structured Data

Not every enterprise question should be answered through vector retrieval.

Example:

"What was the total revenue in Q2?"

may require:

SQL

rather than semantic document search.

A production architecture may route:

Unstructured Question โ†’ RAG
Structured Question โ†’ SQL

45. RAG + SQL Architecture

flowchart TD

    A[User Query] --> B[Query Router]

    B -->|Unstructured| C[LlamaIndex Retriever]

    B -->|Structured| D[SQL Query Engine]

    C --> E[(Vector Store)]

    D --> F[(SQL Database)]

    E --> G[Context]

    F --> G

    G --> H[LLM]

    H --> I[Response]

46. RAG + Knowledge Graph

Some questions require relationships.

Example:

"Which applications depend on Service A?"

A graph may be better suited than pure vector retrieval.

Query
 โ†“
Graph Retrieval
 โ†“
Relationships
 โ†“
Context
 โ†“
LLM

47. Multi-Source RAG

Enterprise systems may retrieve from:

Documents
+
Vector Database
+
SQL Database
+
Knowledge Graph
+
APIs

The architecture becomes:

flowchart TB

    A[User Query] --> B[Query Router]

    B --> C[Document RAG]
    B --> D[SQL]
    B --> E[Knowledge Graph]
    B --> F[Enterprise API]

    C --> G[Context]
    D --> G
    E --> G
    F --> G

    G --> H[LLM]

    H --> I[Final Response]

48. RAG as an AI Capability

A production application should avoid embedding all RAG logic directly into controllers.

Instead:

REST Controller
      โ†“
Knowledge Service
      โ†“
RAG Pipeline
      โ†“
LlamaIndex
      โ†“
Retrieval / LLM

This keeps the framework behind an application-level capability.


49. Enterprise RAG Abstraction

Conceptually:

class KnowledgeService:

    def answer(
        self,
        query,
        tenant_id,
        filters=None
    ):
        ...

Internally:

KnowledgeService
      โ†“
LlamaIndex Adapter
      โ†“
Retriever
      โ†“
Context Builder
      โ†“
LLM

50. Ports and Adapters

flowchart LR

    A[Enterprise Application] --> B[KnowledgeService]

    B --> C[Retrieval Port]

    C --> D[LlamaIndex Adapter]

    D --> E[Vector Store]

    B --> F[LLM Port]

    F --> G[LLM Adapter]

    G --> H[Model Provider]

This reduces direct framework coupling.


51. RAG Pipeline Configuration

A production RAG pipeline should make important decisions explicit.

Example:

rag_config = {
    "top_k": 5,
    "similarity_threshold": 0.75,
    "chunk_size": 512,
    "chunk_overlap": 50,
    "enable_citations": True,
    "enable_metadata_filtering": True
}

Configuration should ideally be externally configurable rather than hard-coded throughout the application.


52. RAG Pipeline Lifecycle

REQUEST
   โ†“
AUTHENTICATE
   โ†“
AUTHORIZE
   โ†“
VALIDATE QUERY
   โ†“
RETRIEVE
   โ†“
FILTER
   โ†“
RANK
   โ†“
BUILD CONTEXT
   โ†“
GENERATE
   โ†“
VALIDATE RESPONSE
   โ†“
CITE
   โ†“
RETURN

53. Response Validation

A production RAG system should not blindly return every model response.

Validation can include:

Schema Validation
Citation Validation
Safety Checks
Grounding Checks
Business Rules
PII Checks

Conceptually:

LLM Response
      โ†“
Validation
      โ†“
Valid?
 โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
Yes        No
 โ†“          โ†“
Return     Fallback

54. Grounding Validation

A RAG application can evaluate whether the answer is supported by retrieved context.

Answer
   +
Retrieved Context
   โ†“
Grounding Evaluation
   โ†“
Supported?

This can be implemented using:

Rules
+
Heuristics
+
LLM-based Evaluation
+
Dedicated Evaluation Frameworks

55. No-Answer Behavior

A strong RAG system should be able to say:

"I don't have enough information to answer this."

rather than inventing information.

Architecture:

flowchart TD

    A[Query] --> B[Retriever]

    B --> C{Sufficient Evidence?}

    C -->|Yes| D[LLM]

    C -->|No| E[No-Answer / Clarification]

    D --> F[Grounding Validation]

    F --> G[Response]

56. Empty Retrieval

A query may return no useful results.

Possible causes:

Wrong Query
Wrong Embedding
Wrong Index
Missing Data
Metadata Filter Too Restrictive
Stale Index

A production system should distinguish:

No Data

from:

Retrieval Failure

57. Empty Retrieval Handling

nodes = retriever.retrieve(query)

if not nodes:
    return {
        "status": "NO_EVIDENCE",
        "message": "No relevant information was found."
    }

In production, the actual behavior should be aligned with the application's API and user experience requirements.


58. RAG Observability

Track at least:

Query
Tenant
Retriever
Top-K
Retrieved Node IDs
Similarity Scores
Context Tokens
LLM Latency
LLM Tokens
Response
Citation Metadata
Errors

59. RAG Trace

sequenceDiagram

    participant U as User
    participant A as Application
    participant R as Retriever
    participant V as Vector Store
    participant L as LLM

    U->>A: Query

    A->>R: Retrieve(query, filters)

    R->>V: Search

    V-->>R: Candidate Nodes

    R-->>A: Ranked Nodes

    A->>L: Prompt + Context

    L-->>A: Response

    A-->>U: Grounded Response

60. RAG Metrics

Useful metrics include:

Retrieval

Recall@K
Precision@K
MRR
NDCG
Hit Rate

Generation

Faithfulness
Answer Relevance
Groundedness

System

Latency
Throughput
Error Rate
Token Usage
Cost

61. RAG Evaluation Architecture

flowchart TB

    A[Test Dataset] --> B[RAG Pipeline]

    B --> C[Retrieved Context]

    B --> D[Generated Answer]

    C --> E[Retrieval Evaluation]

    D --> F[Generation Evaluation]

    E --> G[Quality Metrics]

    F --> G

    B --> H[Latency / Cost Metrics]

    H --> G

62. RAG Evaluation Dataset

A useful dataset contains:

Question
Expected Answer
Expected Source
Relevant Node IDs
Tenant
Metadata Filters

Example:

Question:
"What is the password expiry period?"

Expected Answer:
90 days

Expected Source:
security-policy.pdf

Relevant Node:
SEC-001-CHUNK-007

63. Retrieval vs Generation Evaluation

Evaluate these separately.

Retrieval

Did we retrieve the correct information?

Generation

Did the model correctly use that information?

This distinction is critical for debugging.


64. Debugging RAG

If the answer is wrong:

Wrong Answer
     โ†“
Check Retrieval
     โ†“
Did we retrieve correct context?

If:

No

investigate:

Chunking
Embeddings
Index
Filters
Top-K

If:

Yes

investigate:

Prompt
Context Construction
LLM
Response Validation

65. RAG Debugging Flow

flowchart TD

    A[Wrong Answer] --> B{Correct Context Retrieved?}

    B -->|No| C[Debug Retrieval]

    C --> D[Chunking]
    C --> E[Embeddings]
    C --> F[Index]
    C --> G[Filters]
    C --> H[Top-K]

    B -->|Yes| I[Debug Generation]

    I --> J[Prompt]
    I --> K[Context Construction]
    I --> L[LLM]
    I --> M[Validation]

66. RAG Latency

End-to-end latency can be approximated as:

Total Latency
=
Query Processing
+
Embedding
+
Retrieval
+
Post-Processing
+
Prompt Construction
+
LLM
+
Validation

The LLM is not necessarily the only bottleneck.


67. RAG Cost

A simplified cost model:

Total RAG Cost
=
Query Embedding Cost
+
Retrieval Infrastructure
+
Context Processing
+
LLM Input Tokens
+
LLM Output Tokens
+
Observability

Reducing unnecessary context can reduce both:

Latency
Cost

68. RAG Optimization

Potential optimizations:

Metadata Filtering
+
Appropriate Top-K
+
Caching
+
Embedding Optimization
+
Context Compression
+
Smaller Prompts
+
Streaming
+
Efficient Vector Store

Optimization should be validated using measurements.


69. RAG Caching

Repeated queries can potentially use cached retrieval results.

Query
 โ†“
Cache
 โ†“
Hit?
 โ”œโ”€โ”€ Yes โ†’ Cached Result
 โ””โ”€โ”€ No โ†’ Retrieval

But cache keys must consider:

Tenant
Authorization Scope
Index Version
Query
Filters

70. Secure RAG Cache

Bad:

cache[query]

Safer conceptual key:

cache[
    tenant_id,
    authorization_scope,
    index_version,
    query,
    filters
]

This prevents users with different access scopes from accidentally sharing cached retrieval results.


71. Streaming

For long responses, streaming can improve perceived latency.

User Query
 โ†“
Retrieve
 โ†“
LLM
 โ†“
Token Stream
 โ†“
User

However:

Retrieval
+
Security
+
Validation

still need to happen before unsafe context is exposed.


72. Production RAG Architecture

flowchart TB

    A[User] --> B[API Gateway]

    B --> C[Authentication]

    C --> D[Authorization]

    D --> E[RAG Application Service]

    E --> F[Query Processor]

    F --> G[Tenant / Metadata Filters]

    G --> H[Retriever]

    H --> I[(Vector Store)]

    I --> J[Candidate Nodes]

    J --> K[Post Processing]

    K --> L[Context Builder]

    L --> M[Prompt Manager]

    M --> N[LLM]

    N --> O[Response Validator]

    O --> P[Citation Builder]

    P --> Q[Final Response]

    E --> R[Observability]

    H --> R
    N --> R

73. Enterprise RAG Components

A production platform may contain:

API Gateway
Authentication
Authorization
Query Service
Retriever
Vector Store
Metadata Store
LLM Provider
Prompt Management
Evaluation
Observability
Caching
Audit

LlamaIndex can provide important building blocks, but it should remain one component within the broader enterprise architecture.


74. RAG Failure Patterns

Failure 1 โ€” Wrong Context

Query
 โ†“
Wrong Nodes
 โ†“
Wrong Answer

Failure 2 โ€” No Context

Query
 โ†“
No Results
 โ†“
LLM Generates From Internal Knowledge
 โ†“
Potential Hallucination

Failure 3 โ€” Too Much Context

Retrieve 100 Nodes
 โ†“
Huge Prompt
 โ†“
Noise
 โ†“
Higher Cost

Failure 4 โ€” Stale Context

Updated Document
 โ†“
Old Index
 โ†“
Old Answer

Failure 5 โ€” Unauthorized Context

Wrong Tenant Filter
 โ†“
Unauthorized Node
 โ†“
LLM Context
 โ†“
Data Leakage

75. RAG Failure Architecture

flowchart TD

    A[User Query] --> B[RAG Pipeline]

    B --> C{Retrieval Quality}

    C -->|Poor| D[Wrong Context]

    C -->|Empty| E[No Evidence]

    C -->|Too Much| F[Context Noise]

    C -->|Stale| G[Outdated Context]

    C -->|Unauthorized| H[Security Incident]

    C -->|Good| I[LLM]

    I --> J[Response]

76. Production RAG Principles

A production RAG system should:

1. Separate ingestion and query paths
2. Enforce authorization before retrieval
3. Preserve document lineage
4. Use metadata filters
5. Evaluate retrieval independently
6. Control context size
7. Support no-answer behavior
8. Track index freshness
9. Monitor latency and cost
10. Validate generated responses
11. Provide source attribution
12. Version important retrieval configurations

77. LlamaIndex RAG Design Principles

Framework
     โ†“
RAG Capability
     โ†“
Application Architecture

not:

Application
     โ†“
Everything directly coupled to LlamaIndex

Use LlamaIndex where it provides value while keeping business capabilities independently designed.


78. LlamaIndex RAG Abstraction

Conceptually:

class EnterpriseRAGService:

    def answer(
        self,
        query,
        tenant_id,
        filters=None
    ):
        # authorize
        # retrieve
        # build context
        # generate
        # validate
        # cite
        pass

The framework implementation remains behind this application-level capability.


79. RAG Pipeline Stages

A useful production model is:

Stage 1
Authentication

Stage 2
Authorization

Stage 3
Query Processing

Stage 4
Retrieval

Stage 5
Filtering

Stage 6
Ranking

Stage 7
Context Construction

Stage 8
Generation

Stage 9
Validation

Stage 10
Citation

Stage 11
Observability

80. Practical RAG Implementation

A simple LlamaIndex implementation:

from llama_index.core import (
    SimpleDirectoryReader,
    VectorStoreIndex
)

# Ingestion
documents = SimpleDirectoryReader(
    "data"
).load_data()

# Index
index = VectorStoreIndex.from_documents(
    documents
)

# Query engine
query_engine = index.as_query_engine(
    similarity_top_k=5
)

# Query
question = "What is the security incident response process?"

response = query_engine.query(question)

print(response)

81. Retrieval-Only Implementation

For debugging and evaluation, inspect retrieval separately:

retriever = index.as_retriever(
    similarity_top_k=5
)

nodes = retriever.retrieve(
    "What is the security incident response process?"
)

for node in nodes:
    print("Score:", node.score)
    print("Text:", node.node.text)
    print("Metadata:", node.node.metadata)
    print("---")

This is an important production debugging technique.


82. Why Retrieval-Only Testing Matters

If the final answer is wrong:

First inspect:
Retrieved Nodes

If the nodes are wrong:

Fix Retrieval

If the nodes are correct:

Investigate Generation

Therefore:

Retriever

should be independently testable.


83. RAG Testing Strategy

Test at multiple levels.

Unit

Parser
Chunker
Metadata
Retriever
Prompt

Integration

Retriever + Vector Store
RAG + LLM

End-to-End

User Query
 โ†“
RAG
 โ†“
Response

84. RAG Test Architecture

flowchart TB

    A[Test Suite]

    A --> B[Unit Tests]
    A --> C[Integration Tests]
    A --> D[Retrieval Evaluation]
    A --> E[End-to-End Tests]
    A --> F[Security Tests]
    A --> G[Performance Tests]

    B --> H[Quality Gate]
    C --> H
    D --> H
    E --> H
    F --> H
    G --> H

85. Security Testing

Test cases should include:

Tenant A asks about Tenant B data

Expected:

No unauthorized context

Also test:

Unauthorized document
Restricted metadata
Deleted document
Expired document
Confidential document

86. RAG Security Test

User:
Tenant A

Query:
"What is Tenant B's pricing strategy?"

Expected:
No Tenant B context

This should be tested automatically rather than relying only on prompt instructions.


87. RAG Quality Gate

A deployment candidate can require:

Recall@5 >= Target
Faithfulness >= Target
Latency <= Target
Cost <= Target
Security Tests = PASS

Only then:

Deploy

88. Production Deployment Model

flowchart LR

    A[Code Change] --> B[Build]

    B --> C[Unit Tests]

    C --> D[Integration Tests]

    D --> E[RAG Evaluation]

    E --> F[Security Tests]

    F --> G[Performance Tests]

    G --> H[Deploy]

    H --> I[Monitor]

    I --> J{Healthy?}

    J -->|Yes| K[Continue]

    J -->|No| L[Rollback]

89. RAG Configuration Versioning

Important configuration should be versioned:

Embedding Model
Chunk Size
Chunk Overlap
Top-K
Similarity Threshold
Prompt
Retriever
Response Mode
Index Version

Example:

RAG Configuration v7

This makes production experiments reproducible.


90. RAG Experiment Tracking

A useful experiment record:

Experiment ID
Embedding Model
Chunk Size
Top-K
Retriever
Prompt Version
Index Version
Recall@K
Faithfulness
Latency
Cost

This allows engineers to compare changes systematically.


91. Production RAG Checklist

Data

  • [ ] Data connectors
  • [ ] Parsing
  • [ ] Chunking
  • [ ] Metadata
  • [ ] Deduplication
  • [ ] Versioning
  • [ ] Freshness

Retrieval

  • [ ] Index
  • [ ] Retriever
  • [ ] Top-K
  • [ ] Filters
  • [ ] Ranking
  • [ ] Deduplication
  • [ ] Context selection

Generation

  • [ ] Prompt
  • [ ] LLM
  • [ ] Response synthesis
  • [ ] Validation
  • [ ] No-answer behavior
  • [ ] Citations

Security

  • [ ] Authentication
  • [ ] Authorization
  • [ ] Tenant isolation
  • [ ] ACL filtering
  • [ ] Secure caching
  • [ ] Audit logging

Evaluation

  • [ ] Retrieval dataset
  • [ ] Retrieval metrics
  • [ ] Answer quality
  • [ ] Faithfulness
  • [ ] Security tests
  • [ ] Regression tests

Operations

  • [ ] Logging
  • [ ] Metrics
  • [ ] Tracing
  • [ ] Latency
  • [ ] Cost
  • [ ] Freshness
  • [ ] Error monitoring

92. Key Takeaways

  • RAG combines retrieval with generation.
  • LlamaIndex provides abstractions for implementing RAG pipelines.
  • A production RAG system contains ingestion and query paths.
  • The retriever should be independently testable.
  • Query engines combine retrieval and generation.
  • Context construction is a critical stage of RAG.
  • Top-K must be tuned using evaluation.
  • Metadata filtering improves precision and supports enterprise boundaries.
  • Metadata filtering must not be confused with authorization.
  • Tenant isolation is essential for multi-tenant RAG.
  • Response synthesis determines how retrieved information is converted into a response.
  • Citation metadata enables source attribution.
  • Hybrid and multi-query retrieval can improve recall.
  • Parent-child retrieval can balance precision and context.
  • Structured questions may require SQL rather than vector retrieval.
  • Relationship-heavy questions may require graph retrieval.
  • Multi-source RAG can combine documents, databases, graphs, and APIs.
  • No-answer behavior is an important production capability.
  • Retrieval and generation should be evaluated separately.
  • RAG observability should capture retrieval and generation behavior.
  • RAG cost is influenced heavily by context size and LLM usage.
  • Caches must respect tenant and authorization boundaries.
  • Production RAG requires security, evaluation, observability, and lifecycle management.
  • LlamaIndex should be treated as a framework within a broader Enterprise AI architecture.

๐Ÿ“ Quick Revision Notes

Basic RAG

Documents
 โ†“
Nodes
 โ†“
Index
 โ†“
Retriever
 โ†“
Context
 โ†“
LLM
 โ†“
Answer

Production RAG

Authenticate
 โ†“
Authorize
 โ†“
Query
 โ†“
Retrieve
 โ†“
Filter
 โ†“
Rank
 โ†“
Build Context
 โ†“
Generate
 โ†“
Validate
 โ†“
Cite
 โ†“
Observe

RAG Debugging

Wrong Answer
      โ†“
Correct Context?
   /        \
 No          Yes
 โ†“            โ†“
Retrieval   Generation
Problem      Problem

RAG Quality

Retrieval Quality
+
Context Quality
+
Generation Quality
+
Security
+
Freshness
=
Production RAG Quality

RAG Cost

Query Embedding
+
Retrieval
+
Context Tokens
+
Output Tokens
+
Infrastructure
=
RAG Cost

โ“ Interview Questions

Beginner

  1. What is RAG?
  2. Why is RAG needed?
  3. What is the role of LlamaIndex in RAG?
  4. What is a retriever?
  5. What is a query engine?
  6. What is response synthesis?
  7. What is Top-K retrieval?
  8. Why is metadata important in RAG?
  9. What is source attribution?
  10. What is a no-answer response?

Intermediate

  1. Explain the end-to-end LlamaIndex RAG pipeline.
  2. What happens when a user submits a query?
  3. How do you configure Top-K?
  4. How would you implement metadata filtering?
  5. How would you implement citation-aware RAG?
  6. What is the difference between retrieval and generation evaluation?
  7. How would you handle empty retrieval?
  8. How would you reduce RAG latency?
  9. How would you reduce RAG cost?
  10. What is contextual compression?
  11. What is multi-query RAG?
  12. What is hybrid RAG?
  13. What is parent-child retrieval?
  14. How would you handle structured data in a RAG system?

Advanced

  1. Design a production LlamaIndex RAG architecture.
  2. How would you implement multi-tenant RAG?
  3. How would you prevent cross-tenant data leakage?
  4. How would you design a secure retrieval cache?
  5. How would you evaluate retrieval quality independently from generation?
  6. How would you diagnose a wrong RAG answer?
  7. How would you design no-answer behavior?
  8. How would you implement source attribution?
  9. How would you combine vector, keyword, SQL, and graph retrieval?
  10. How would you design multi-stage retrieval?
  11. How would you optimize RAG for high query volume?
  12. How would you design RAG observability?
  13. How would you version RAG configurations?
  14. How would you safely deploy a new RAG configuration?
  15. How would you implement RAG regression testing?
  16. How would you enforce authorization before retrieval?
  17. How would you design RAG for continuously changing enterprise documents?
  18. How would you balance retrieval recall against context cost?
  19. How would you identify whether a RAG failure originates from retrieval or generation?
  20. How would you design a production RAG quality gate?

๐Ÿ› ๏ธ Practical Exercise

Build an Enterprise Knowledge Assistant using LlamaIndex.

Step 1 โ€” Ingest Data

Use:

PDF
Markdown
TXT

Pipeline:

Documents
 โ†“
Nodes
 โ†“
Metadata
 โ†“
Embeddings
 โ†“
Vector Index

Step 2 โ€” Build Retrieval

Implement:

Top-K Retrieval
Metadata Filtering
Tenant Filtering
Similarity Threshold

Step 3 โ€” Build RAG

Implement:

User Query
 โ†“
Retriever
 โ†“
Context
 โ†“
LLM
 โ†“
Response

Step 4 โ€” Add Citations

Return:

Answer

Sources:
- document_id
- document name
- section
- page

Step 5 โ€” Add No-Answer Behavior

If evidence is insufficient:

Do not invent an answer.

Return:
"No sufficient evidence was found."

Step 6 โ€” Evaluate

Create:

50+ Questions

Measure:

Recall@K
Precision@K
Hit Rate
Faithfulness
Answer Relevance
Latency
Cost

๐Ÿข Enterprise Architecture Challenge

Design a RAG platform supporting:

500 Tenants
10 Million Documents
Multiple Data Sources
Continuous Updates
Strict Authorization
High Query Volume

Required:

Vector Retrieval
+
Keyword Retrieval
+
Metadata Filtering
+
Tenant Isolation
+
Citation
+
Evaluation
+
Observability
+
Caching
+
No-Answer Handling

๐Ÿง  Architecture Challenge

Design the following:

                         User
                          โ”‚
                          โ–ผ
                    API Gateway
                          โ”‚
                          โ–ผ
                   Authentication
                          โ”‚
                          โ–ผ
                    Authorization
                          โ”‚
                          โ–ผ
                    Query Service
                          โ”‚
                          โ–ผ
                    Query Router
                          โ”‚
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
             โ–ผ            โ–ผ            โ–ผ
          Vector       Keyword       SQL
          Retrieval    Retrieval    Retrieval
             โ”‚            โ”‚            โ”‚
             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ–ผ
                    Result Fusion
                          โ”‚
                          โ–ผ
                     Filtering
                          โ”‚
                          โ–ผ
                      Ranking
                          โ”‚
                          โ–ผ
                  Context Selection
                          โ”‚
                          โ–ผ
                        LLM
                          โ”‚
                          โ–ผ
                  Response Validation
                          โ”‚
                          โ–ผ
                     Citations
                          โ”‚
                          โ–ผ
                      Response

The system should support:

Security
Scalability
Reliability
Observability
Evaluation
Cost Optimization

๐Ÿš€ Production RAG Exercise

Implement two versions.

Version 1 โ€” Basic RAG

Documents
 โ†“
VectorStoreIndex
 โ†“
Query Engine
 โ†“
LLM

Version 2 โ€” Production RAG

Authentication
 โ†“
Authorization
 โ†“
Tenant Filter
 โ†“
Retriever
 โ†“
Post Processing
 โ†“
Context Builder
 โ†“
LLM
 โ†“
Validation
 โ†“
Citation
 โ†“
Observability

Compare:

Quality
Latency
Cost
Security
Maintainability

๐Ÿ“š References & Further Reading

Recommended areas for further study:

  • LlamaIndex RAG
  • LlamaIndex Query Engines
  • LlamaIndex Retrievers
  • LlamaIndex Response Synthesis
  • LlamaIndex Metadata Filtering
  • LlamaIndex Vector Stores
  • LlamaIndex Citation / Source Attribution
  • LlamaIndex Workflows
  • LlamaIndex Evaluation
  • RAG Evaluation
  • Hybrid Retrieval
  • Multi-Query Retrieval
  • Contextual Compression
  • Parent-Child Retrieval
  • Enterprise RAG Architecture
  • Production RAG Observability
  • Multi-Tenant RAG Security

LlamaIndex evolves rapidly. Before implementing production systems, verify the current APIs, query-engine interfaces, response-synthesis modes, retriever APIs, metadata-filtering syntax, citation capabilities, and vector-store integrations against the official documentation for the version used by your project.


๐Ÿงญ Chapter Navigation

โฌ…๏ธ Previous: 11. LlamaIndex Indexes and Retrieval

๐Ÿ“š Part VIII Index: AI Engineering Frameworks & Tooling

โžก๏ธ Next: 13. LlamaIndex Agents and Tools


Enterprise AI Engineering Handbook

Building Production-Grade Enterprise AI Systems โ€” One Chapter at a Time.