08. RAG Performance Optimization¶
Category: Production RAG Engineering
Module: Part V β Advanced Retrieval-Augmented Generation
Difficulty: Advanced
π Overview¶
A RAG system can be functionally correct and still fail in production because it is:
- Too slow
- Too expensive
- Difficult to scale
- Inefficient with context
- Over-fetching documents
- Performing unnecessary model calls
- Saturating vector databases
- Generating excessive tokens
- Performing redundant retrieval
- Using expensive models for simple requests
Production RAG performance optimization is therefore not a single optimization technique.
It is a systematic engineering discipline covering the complete pipeline:
User Query
β
Query Processing
β
Query Rewriting
β
Embedding
β
Retrieval
β
Filtering
β
Reranking
β
Context Selection
β
Prompt Assembly
β
LLM Generation
β
Validation
β
Citation
β
Response
The goal is to optimize the complete system across:
Latency
Throughput
Accuracy
Context Efficiency
Token Usage
Cost
Scalability
Reliability
Resource Utilization
The fastest RAG system is not necessarily the one with the fastest individual component. It is the system that minimizes unnecessary work while preserving answer quality.
π― Learning Objectives¶
After completing this chapter, you will be able to:
- Understand RAG performance bottlenecks
- Decompose end-to-end RAG latency
- Optimize retrieval latency
- Optimize embedding generation
- Optimize vector search
- Optimize hybrid retrieval
- Optimize reranking
- Optimize context selection
- Reduce unnecessary context
- Optimize prompt construction
- Reduce LLM latency
- Optimize token usage
- Implement caching
- Implement parallel retrieval
- Implement asynchronous processing
- Optimize batching
- Optimize model selection
- Implement query routing
- Optimize top-K
- Optimize reranking candidates
- Optimize context windows
- Optimize vector indexes
- Optimize database connections
- Improve throughput
- Control concurrency
- Optimize resource utilization
- Design performance SLOs
- Perform latency profiling
- Perform capacity planning
- Build production performance dashboards
- Balance latency, quality, and cost
π§ 1. What Is RAG Performance Optimization?¶
RAG performance optimization means improving the system's:
while maintaining acceptable:
A useful objective is:
π§ 2. Performance Is a Multi-Dimensional Problem¶
Do not define performance as latency alone.
RAG PERFORMANCE
β
βββββββββββββββββΌβββββββββββββββββ
βΌ βΌ βΌ
Latency Throughput Cost
β β β
βΌ βΌ βΌ
p50/p95/p99 req/sec $/request
β
βΌ
Scalability
β
βΌ
Quality
π§ 3. End-to-End Latency¶
A simplified model:
T_total =
T_query
+ T_embedding
+ T_retrieval
+ T_reranking
+ T_context
+ T_prompt
+ T_generation
+ T_validation
+ T_citation
The first optimization step is therefore:
Measure before optimizing.
π§ 4. Latency Waterfall¶
Query Processing βββ 20 ms
Embedding βββββ 35 ms
Retrieval βββββββ 70 ms
Reranking βββββββββββ 120 ms
Context Selection ββ 15 ms
Prompt Assembly ββ 10 ms
LLM Generation βββββββββββββββββββββ 1420 ms
Validation βββ 25 ms
Citation ββ 12 ms
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
Total 1727 ms
The largest component should usually receive the most attention.
π§ 5. Latency Percentiles¶
Average latency is not enough.
Monitor:
Example:
A good average can hide poor tail latency.
π§ 6. Why p95 and p99 Matter¶
Suppose:
The average may still appear reasonable.
But that one slow request represents a serious tail-latency problem.
Production systems therefore optimize:
for predictable user experience.
π§ 7. Performance Optimization Framework¶
Use this loop:
Measure
β
Profile
β
Identify Bottleneck
β
Form Hypothesis
β
Optimize
β
Benchmark
β
Evaluate Quality
β
Deploy
β
Monitor
Never optimize blindly.
π§ 8. RAG Performance Bottlenecks¶
Common bottlenecks include:
Slow Embeddings
Slow Vector Search
Large Top-K
Expensive Reranking
Large Context
Large Prompt
Slow LLM
Repeated LLM Calls
Sequential Retrieval
Database Saturation
Network Latency
Excessive Serialization
Poor Caching
High Concurrency
π§ 9. Retrieval Pipeline¶
flowchart LR
A["Query"] --> B["Embedding"]
B --> C["Vector Search"]
A --> D["Keyword Search"]
C --> E["Candidate Merge"]
D --> E
E --> F["Filtering"]
F --> G["Reranking"]
G --> H["Context Selection"]
H --> I["LLM"]
Every stage can become a bottleneck.
π§ 10. The Optimization Principle¶
A powerful production rule:
Do Less Work
β
Do Necessary Work in Parallel
β
Use the Cheapest Suitable Component
β
Cache Reusable Results
β
Measure Quality
π§ 11. Optimize the Critical Path¶
The critical path is the sequence of operations that determines response time.
Example:
If independent operations exist:
do not unnecessarily execute them sequentially.
π§ 12. Sequential vs Parallel Retrieval¶
Sequential¶
Latency:
Parallel¶
βββ Dense Search βββ
Query ββββββββ€ βββ Merge
βββ Sparse Search ββ
Latency becomes approximately:
π§ 13. Parallel Retrieval¶
from concurrent.futures import ThreadPoolExecutor
def retrieve(query):
with ThreadPoolExecutor(max_workers=2) as executor:
dense_future = executor.submit(
dense_retriever.retrieve,
query
)
sparse_future = executor.submit(
sparse_retriever.retrieve,
query
)
dense_results = dense_future.result()
sparse_results = sparse_future.result()
return merge_results(
dense_results,
sparse_results
)
Use concurrency carefully according to the client, database, and service behavior.
π§ 14. Async Retrieval¶
For I/O-heavy systems:
import asyncio
async def retrieve(query):
dense_task = asyncio.create_task(
dense_retriever.retrieve(query)
)
sparse_task = asyncio.create_task(
sparse_retriever.retrieve(query)
)
dense, sparse = await asyncio.gather(
dense_task,
sparse_task
)
return merge_results(dense, sparse)
π§ 15. Parallelism Trade-Off¶
Parallelism can reduce latency but increase:
Therefore:
Concurrency must be bounded.
π§ 16. Concurrency Control¶
Use:
Example:
import asyncio
semaphore = asyncio.Semaphore(20)
async def safe_retrieve(query):
async with semaphore:
return await retriever.retrieve(query)
π§ 17. Top-K Optimization¶
A larger K is not always better.
Increasing K can improve recall but increases:
π§ 18. Retrieval Top-K Trade-Off¶
Retrieval Quality
β²
β βββββββ
β /
β /
β /
β /
ββββββββββββββββββββΊ K
The improvement often diminishes after a certain point.
π§ 19. Candidate K vs Final K¶
Separate:
from:
Example:
This is often more effective than sending all 50 documents to the LLM.
π§ 20. Adaptive Top-K¶
Instead of always using:
adapt K based on query characteristics.
π§ 21. Score-Based Retrieval Cutoff¶
Instead of selecting only by fixed K:
Example:
Thresholds must be calibrated for the retrieval system.
π§ 22. Dynamic Retrieval¶
A more advanced pipeline:
Query
β
Initial Retrieval
β
Are results sufficient?
β
βββ Yes β Continue
β
βββ No β Expand Retrieval
This avoids expensive retrieval for easy queries.
π§ 23. Early Exit¶
Example:
Query
β
Fast Retrieval
β
Confidence High?
βββ Yes β Generate
βββ No β Rerank / Expand
This is a powerful optimization.
π§ 24. Query Classification¶
Before expensive processing:
Query Classifier
β
βββ FAQ
βββ Simple Search
βββ Complex RAG
βββ SQL
βββ Graph
βββ Multimodal
Simple requests can bypass unnecessary stages.
π§ 25. Router-Based Optimization¶
flowchart TD
A["User Query"] --> B["Query Router"]
B --> C["Simple Retrieval"]
B --> D["Hybrid Retrieval"]
B --> E["Graph RAG"]
B --> F["SQL RAG"]
B --> G["Agentic RAG"]
C --> H["Response"]
D --> H
E --> H
F --> H
G --> H
The objective:
Use the simplest pipeline that can reliably answer the query.
π§ 26. Query Rewriting Cost¶
Query rewriting improves retrieval but adds latency.
If rewriting costs:
for every request, it may become a major bottleneck.
π§ 27. Conditional Query Rewriting¶
This avoids unnecessary LLM calls.
π§ 28. Multi-Query Optimization¶
Multi-query retrieval:
can improve recall but increases:
Use it selectively.
π§ 29. Multi-Query Parallelization¶
βββ Query A ββ Search βββ
βββ Query B ββ Search βββ€
Original Query βββΌββ Query C ββ Search βββΌββ Merge
βββ Query D ββ Search βββ
Parallelize independent searches.
π§ 30. Embedding Optimization¶
Embedding latency can be reduced using:
π§ 31. Embedding Batching¶
Instead of:
batch:
This is particularly important during ingestion.
π§ 32. Query Embedding Cache¶
Queries can sometimes repeat.
π§ 33. Embedding Cache Example¶
def get_embedding(query):
key = hash_query(query)
cached = cache.get(key)
if cached:
return cached
embedding = embedding_model.embed(query)
cache.set(key, embedding)
return embedding
Use appropriate invalidation/versioning.
π§ 34. Embedding Model Selection¶
Larger models may provide better embeddings but can increase:
Evaluate:
together.
π§ 35. Vector Search Optimization¶
Vector search performance depends on:
π§ 36. ANN Search¶
Approximate Nearest Neighbor search trades exactness for speed.
Exact Search
β
High Recall
High Cost
ANN Search
β
Lower Search Cost
Very Fast
Potential Recall Trade-Off
π§ 37. Index Selection¶
Common structures include:
Selection depends on:
π§ 38. Flat Search¶
Complexity grows with dataset size.
Good for:
π§ 39. HNSW¶
HNSW creates a graph-based search structure.
Search navigates the graph rather than scanning every vector.
π§ 40. HNSW Search Parameters¶
Common parameters include:
Increasing search effort can improve recall but increase latency.
π§ 41. efSearch Trade-Off¶
Tune against a benchmark rather than choosing arbitrary values.
π§ 42. Vector Dimension¶
Higher dimensions can increase:
But reducing dimensions can affect retrieval quality.
Therefore:
π§ 43. Quantization¶
Quantization reduces representation size.
Potential benefits:
Potential trade-off:
Benchmark before production adoption.
π§ 44. Vector Database Optimization¶
Optimize:
π§ 45. Connection Pooling¶
Bad:
Better:
Connection Pool
βββ Connection 1
βββ Connection 2
βββ Connection 3
βββ Connection N
Reuse connections.
π§ 46. Retrieval Payload Optimization¶
Avoid returning unnecessary data.
Bad:
if only:
is needed.
π§ 47. Hybrid Search Optimization¶
Hybrid retrieval:
can improve quality but adds work.
Optimize using:
π§ 48. Hybrid Retrieval Pipeline¶
Query
β
βββββββββββ΄ββββββββββ
βΌ βΌ
Dense Search Sparse Search
β β
βββββββββββ¬ββββββββββ
βΌ
Merge
β
Rerank
β
Top-N Context
π§ 49. Reranking Cost¶
Reranking is often more expensive than initial retrieval.
Example:
If you rerank:
the cost can become significant.
π§ 50. Candidate Reduction¶
Instead of:
use:
This reduces reranking work.
π§ 51. Two-Stage Retrieval¶
This is a common production architecture.
π§ 52. Multi-Stage Retrieval¶
flowchart LR
A["Query"] --> B["Cheap Retrieval"]
B --> C["Candidate Set"]
C --> D["Filtering"]
D --> E["Reranking"]
E --> F["Context Compression"]
F --> G["Final Context"]
G --> H["LLM"]
Each stage reduces the amount of data passed to the next expensive stage.
π§ 53. Reranker Batching¶
When reranking multiple candidates:
batch them when supported.
This can improve accelerator utilization and reduce per-request overhead.
π§ 54. Reranker Selection¶
Possible options:
Use the least expensive mechanism that meets the quality target.
π§ 55. Context Optimization¶
One of the most important RAG optimizations is:
Do not send irrelevant context to the LLM.
Large context can cause:
π§ 56. Context Compression¶
Example:
π§ 57. Context Selection¶
Use:
to select final context.
π§ 58. Context Token Budget¶
Define:
Example:
Selection should respect the budget.
π§ 59. Token Budgeting¶
A useful conceptual budget:
Model Context Window
β
βββ System Prompt
βββ User Query
βββ Retrieved Context
βββ Conversation Memory
βββ Output Budget
If context grows excessively:
π§ 60. Context Ordering¶
Ordering can affect generation quality.
Possible strategy:
or an empirically tested ordering strategy.
Do not assume one ordering works universally.
π§ 61. Duplicate Context Removal¶
Retrieval systems may return:
Deduplicate before generation.
def deduplicate(chunks):
seen = set()
result = []
for chunk in chunks:
if chunk.id not in seen:
seen.add(chunk.id)
result.append(chunk)
return result
π§ 62. Parent-Child Retrieval Optimization¶
Parent-child retrieval can provide:
Optimization:
rather than sending entire documents.
π§ 63. MMR Optimization¶
MMR can reduce redundant context.
Instead of:
you may select:
This can improve context efficiency.
π§ 64. Context Diversity¶
Example:
D1 β Payment architecture
D2 β Payment architecture
D3 β Payment architecture
D4 β Database configuration
D5 β Error handling
A diversity-aware selection can provide broader evidence.
π§ 65. Prompt Optimization¶
Prompt construction has two goals:
Avoid:
π§ 66. Prompt Template Optimization¶
Bad:
Better:
π§ 67. Prompt Caching¶
If supported by the model provider:
Potential benefits:
π§ 68. Static vs Dynamic Prompt Content¶
Separate:
Static
βββ System Instructions
βββ Response Schema
βββ Policy
Dynamic
βββ Query
βββ Context
βββ Conversation
This makes caching and prompt management easier.
π§ 69. LLM Latency Optimization¶
LLM latency can be influenced by:
π§ 70. Model Routing¶
Use different models for different workloads:
The objective is:
π§ 71. Model Cascade¶
This can reduce average cost and latency.
π§ 72. Streaming¶
Without streaming:
With streaming:
Streaming improves perceived latency even when total generation time remains similar.
π§ 73. Time to First Token¶
Track:
separately from:
Example:
A system may feel responsive despite a longer total generation time.
π§ 74. Output Token Optimization¶
Large answers increase:
Use:
where appropriate.
π§ 75. Structured Output¶
If the application requires a fixed response:
Structured output can reduce unnecessary generation and downstream parsing work.
π§ 76. Validation Optimization¶
Validation itself can add latency.
Avoid:
for every low-risk query unless justified.
Possible strategies:
π§ 77. Selective Validation¶
Response
β
Risk Classifier
β
βββ Low Risk β Lightweight Validation
β
βββ High Risk β Deep Validation
π§ 78. Citation Optimization¶
Citation generation can be optimized by maintaining source IDs throughout the pipeline.
Instead of reconstructing citations:
carry:
through the pipeline.
π§ 79. End-to-End Source Tracking¶
The source identity should remain attached to the context object.
π§ 80. Caching¶
Caching is one of the most effective RAG optimizations.
Possible cache layers:
π§ 81. Cache Architecture¶
flowchart TD
A["User Query"] --> B["Cache"]
B -->|Hit| C["Cached Result"]
B -->|Miss| D["RAG Pipeline"]
D --> E["Store Result"]
E --> C
π§ 82. Retrieval Cache¶
Cache:
Example key:
π§ 83. Cache Invalidation¶
The biggest cache problem:
Invalidate when:
π§ 84. Cache Versioning¶
Use:
This makes invalidation more deterministic.
π§ 85. Semantic Cache¶
A semantic cache attempts to reuse results for semantically similar queries.
These may be semantically similar.
But semantic caching must consider:
π§ 86. Semantic Cache Risk¶
Two queries may be similar but require different answers.
Therefore:
Use conservative thresholds and evaluate carefully.
π§ 87. Cache by Tenant¶
Never allow:
to reuse unauthorized data.
Cache keys should include appropriate isolation dimensions.
π§ 88. Batch Processing¶
Batching can improve:
Example:
The optimal batch size depends on infrastructure and model behavior.
π§ 89. Ingestion Performance¶
Production ingestion:
Optimize using:
π§ 90. Incremental Indexing¶
Avoid rebuilding the complete index when only a few documents changed.
instead of:
π§ 91. Incremental Embedding¶
Track document versions:
Only regenerate embeddings when relevant content changes.
π§ 92. Change Detection¶
Use:
Example:
If the hash is unchanged:
π§ 93. Index Build Optimization¶
For large ingestion jobs:
Avoid one-document-at-a-time indexing.
π§ 94. Bulk Indexing¶
Instead of:
use:
This reduces network and transaction overhead.
π§ 95. Database Optimization¶
Monitor:
π§ 96. Vector DB Saturation¶
Symptoms:
Possible solutions:
π§ 97. Horizontal Scaling¶
Load Balancer
β
ββββββββββββΌβββββββββββ
βΌ βΌ βΌ
RAG-1 RAG-2 RAG-3
β β β
ββββββββββββΌβββββββββββ
βΌ
Vector DB
Stateless RAG services scale more easily.
π§ 98. Autoscaling¶
Scale based on:
For AI systems also consider:
π§ 99. Backpressure¶
When downstream services cannot keep up:
Without backpressure:
π§ 100. Load Shedding¶
Under extreme load:
while protecting:
π§ 101. Rate Limiting¶
Apply limits at:
Example:
π§ 102. Resource Isolation¶
Use separate resource pools for:
This prevents batch workloads from degrading interactive traffic.
π§ 103. Priority Queues¶
Priority 1
Production User Query
Priority 2
Internal Query
Priority 3
Evaluation
Priority 4
Batch Processing
π§ 104. Network Optimization¶
RAG often crosses:
Network latency can accumulate.
Reduce it using:
π§ 105. Region Selection¶
If the application runs in:
but the LLM endpoint is far away:
latency increases.
Use an appropriate region/provider architecture while respecting:
π§ 106. Serialization Optimization¶
Avoid transferring unnecessary:
between services.
Prefer compact payloads.
π§ 107. Compression¶
Compression can reduce:
but adds:
Benchmark the trade-off.
π§ 108. Retrieval Result Payload¶
Prefer:
instead of transferring the entire source document when it is not needed.
π§ 109. Observability Overhead¶
Instrumentation itself consumes:
Optimize telemetry using:
π§ 110. Performance vs Quality¶
The key trade-off:
Quality
β²
β
β β
β β
β β
β β
βββββββββββββββββββββββββββββββΊ
Latency
The goal is not:
but:
π§ 111. Quality-Latency-Cost Triangle¶
Optimizing one dimension can affect the others.
π§ 112. Example Trade-Off¶
Option A:
Option B:
The correct choice depends on the product SLO and risk profile.
π§ 113. Performance Budget¶
Define a latency budget:
Example:
Query Processing 50 ms
Embedding 100 ms
Retrieval 200 ms
Reranking 250 ms
Context 50 ms
LLM 1200 ms
Validation 100 ms
Citation 50 ms
--------------------------------
Total 2000 ms
π§ 114. Budget Violation¶
If:
then other stages cannot consume unlimited latency.
The budget forces architectural discipline.
π§ 115. Throughput¶
Throughput is often measured as:
For LLM systems also monitor:
π§ 116. Retrieval Throughput¶
Example:
The LLM becomes the bottleneck.
π§ 117. Bottleneck Identification¶
flowchart LR
A["Query"] --> B["Embedding"]
B --> C["Retrieval"]
C --> D["Reranking"]
D --> E["LLM"]
E --> F["Response"]
D -. "Potential Bottleneck" .-> G["Profile"]
E -. "Potential Bottleneck" .-> G
Always profile the actual workload.
π§ 118. Queueing Effects¶
As utilization approaches capacity:
A component operating near saturation can cause dramatic tail latency.
π§ 119. Capacity Planning¶
Estimate:
Then size:
π§ 120. Load Testing¶
Test:
and observe:
π§ 121. Stress Testing¶
Push beyond expected capacity.
Find:
π§ 122. Performance Regression Testing¶
Every major change should be benchmarked.
Example:
π§ 123. Benchmark Table¶
| Configuration | p95 Latency | Recall@10 | Tokens | Cost |
|---|---|---|---|---|
| Dense | 620 ms | 88% | 4,800 | Low |
| Hybrid | 710 ms | 93% | 5,000 | Medium |
| Hybrid + Reranker | 920 ms | 96% | 4,300 | Higher |
| Hybrid + Reranker + Compression | 980 ms | 96% | 3,100 | Medium |
Illustrative values only.
π§ 124. A/B Performance Testing¶
Compare:
against:
using:
π§ 125. Canary Deployment¶
Monitor:
Increase traffic only if healthy.
π§ 126. Performance Optimization Experiment¶
Example:
Hypothesis:
Reducing reranker candidates
from 100 β 40
will reduce latency
without significant recall loss.
Experiment:
Measure:
π§ 127. Performance Optimization Notebook¶
experiments = [
{
"name": "reranker_candidates_100",
"candidates": 100
},
{
"name": "reranker_candidates_40",
"candidates": 40
}
]
Run the same evaluation set against both.
π§ 128. Optimization Scorecard¶
| Dimension | Baseline | Optimized | Change |
|---|---|---|---|
| p95 Latency | 2.1s | 1.4s | -33% |
| Recall@10 | 94% | 93% | -1 pp |
| Faithfulness | 95% | 95% | 0 |
| Tokens | 5,400 | 3,600 | -33% |
| Cost | $0.025 | $0.017 | -32% |
The objective is not necessarily to maximize every metric independently.
π§ 129. Common Optimization Mistakes¶
Mistake 1¶
without measuring context quality.
Mistake 2¶
for every query.
Mistake 3¶
without considering latency.
Mistake 4¶
for every request.
Mistake 5¶
without candidate reduction.
Mistake 6¶
without invalidation.
Mistake 7¶
without measuring quality.
π§ 130. Over-Optimization¶
A system can become:
but:
Example:
Latency may be excellent while answer quality collapses.
π§ 131. Under-Optimization¶
The opposite:
Top-K:
100
Reranker:
100
Context:
20,000 tokens
LLM:
Largest Model
Validation:
2 additional LLM calls
Quality may improve slightly while:
explode.
π§ 132. Optimization Priority¶
A practical order:
1. Measure
2. Remove unnecessary work
3. Parallelize independent work
4. Reduce candidate volume
5. Optimize context
6. Cache reusable work
7. Optimize model selection
8. Optimize infrastructure
9. Fine-tune low-level components
π§ 133. Remove Unnecessary Work¶
Ask:
Do we need query rewriting?
Do we need multi-query?
Do we need reranking?
Do we need a second validation model?
Do we need 20 documents?
Do we need 8,000 context tokens?
Do we need the largest model?
The cheapest optimization is often:
Not performing the operation at all.
π§ 134. Performance Architecture¶
flowchart TD
A["User Query"] --> B["Query Router"]
B --> C{"Simple?"}
C -->|Yes| D["Fast Retrieval"]
C -->|No| E["Advanced Retrieval"]
E --> F["Parallel Dense + Sparse"]
F --> G["Candidate Merge"]
G --> H["Metadata Filtering"]
H --> I["Reranking"]
I --> J["Context Compression"]
D --> K["Context Selection"]
J --> K
K --> L["Prompt Assembly"]
L --> M["Model Router"]
M --> N["Small Model"]
M --> O["Large Model"]
N --> P["Validation"]
O --> P
P --> Q["Citation"]
Q --> R["Response"]
π§ 135. Production Performance Architecture¶
The key architectural principles are:
Fast Path
+
Adaptive Path
+
Parallel Retrieval
+
Candidate Reduction
+
Context Budget
+
Caching
+
Model Routing
+
Bounded Concurrency
+
Observability
π§ͺ 136. Practical Project¶
Build a:
Production RAG Performance Optimization Lab
Start with:
then progressively optimize it.
π§ͺ 137. Baseline Architecture¶
Measure:
π§ͺ 138. Optimization Stage 1¶
Add:
Measure:
π§ͺ 139. Optimization Stage 2¶
Add:
Measure:
π§ͺ 140. Optimization Stage 3¶
Add:
Measure:
π§ͺ 141. Optimization Stage 4¶
Add:
Measure:
π§ͺ 142. Optimization Stage 5¶
Add:
Measure:
π§ͺ 143. Optimization Stage 6¶
Add:
Architecture:
Query
β
Initial Retrieval
β
Confidence
β
βββ High β Generate
β
βββ Low β Rerank / Expand
π§ͺ 144. Optimization Experiment Matrix¶
| Experiment | Latency | Recall | Tokens | Cost | Quality |
|---|---|---|---|---|---|
| Baseline | β | β | β | β | β |
| Cache | β | β | β | β | β |
| Parallel Retrieval | β | β | β | β | β |
| Reranking | β | β | β | β | β |
| Compression | β | β | β | β | β |
| Model Routing | β | β | β | β | β |
| Adaptive Retrieval | β | β | β | β | β |
Populate using actual benchmark results.
π§ͺ 145. Performance Test Dataset¶
Create representative query categories:
Simple FAQ
Technical Query
Multi-hop Query
Ambiguous Query
Long Query
Short Query
SQL Query
Graph Query
No-Answer Query
High-Context Query
Do not benchmark using only easy questions.
π§ͺ 146. Performance Test Harness¶
def benchmark(rag, queries):
results = []
for query in queries:
result = rag.answer(query)
results.append({
"query": query,
"latency_ms": result.latency_ms,
"input_tokens": result.input_tokens,
"output_tokens": result.output_tokens,
"cost": result.cost
})
return results
π§ͺ 147. Performance Metrics¶
Calculate:
p50 latency
p95 latency
p99 latency
Average retrieval latency
Average reranking latency
Average generation latency
Average context tokens
Average input tokens
Average output tokens
Cost/request
Requests/second
π§ͺ 148. Quality Metrics¶
Do not optimize without measuring:
π§ͺ 149. Final Benchmark¶
The optimized system should answer:
Did latency improve?
Did throughput improve?
Did token usage decrease?
Did cost decrease?
Did retrieval quality remain acceptable?
Did answer quality remain acceptable?
Did citation quality remain acceptable?
Did infrastructure utilization improve?
π§ 150. Production Performance Checklist¶
β Measure end-to-end latency
β Measure p50
β Measure p95
β Measure p99
β Profile every RAG stage
β Identify critical path
β Remove unnecessary operations
β Parallelize independent retrieval
β Use bounded concurrency
β Optimize embedding
β Batch embeddings
β Cache embeddings
β Optimize vector indexes
β Tune ANN parameters
β Optimize vector DB connections
β Reduce retrieval payloads
β Tune Top-K
β Separate candidate K from final K
β Use adaptive retrieval
β Use score thresholds where appropriate
β Reduce reranker candidates
β Batch reranking
β Optimize hybrid retrieval
β Deduplicate context
β Compress context
β Set context budgets
β Optimize context ordering
β Remove irrelevant chunks
β Track context tokens
β Optimize prompts
β Version prompts
β Use prompt caching where appropriate
β Reduce repeated instructions
β Optimize LLM selection
β Use model routing
β Use model cascades where appropriate
β Stream responses
β Track TTFT
β Limit output tokens
β Cache retrieval results
β Cache reusable computations
β Version caches
β Implement invalidation
β Preserve tenant isolation
β Optimize ingestion
β Batch indexing
β Incrementally update indexes
β Avoid unnecessary re-embedding
β Use content hashes
β Control database connections
β Control concurrency
β Implement backpressure
β Implement rate limits
β Implement load shedding
β Separate workloads
β Configure autoscaling
β Load test
β Stress test
β Capacity test
β Regression test
β Canary performance changes
β Monitor latency
β Monitor throughput
β Monitor cost
β Monitor token usage
β Monitor retrieval quality
β Monitor answer quality
β Compare optimization experiments
β Preserve quality SLOs
β Document performance budgets
β Monitor production regressions
π§ 151. Performance Optimization Mental Model¶
RAG PERFORMANCE
β
ββββββββββββββββββββΌβββββββββββββββββββ
βΌ βΌ βΌ
LATENCY THROUGHPUT COST
β β β
βΌ βΌ βΌ
Critical Path Concurrency Tokens
Parallelism Batching Models
Caching Scaling Caching
β β β
ββββββββββββββββββββΌβββββββββββββββββββ
βΌ
QUALITY
β
βββββββββββββββΌββββββββββββββ
βΌ βΌ βΌ
Retrieval Generation Citation
Quality Quality Quality
β
βΌ
RELIABILITY
β
βΌ
PRODUCTION SLOs
π§ 152. Final Mental Model¶
The complete optimization loop is:
PRODUCTION RAG
β
βΌ
MEASURE
β
βΌ
PROFILE
β
βΌ
FIND BOTTLENECK
β
βΌ
REMOVE UNNECESSARY WORK
β
βΌ
PARALLELIZE WORK
β
βΌ
REDUCE DATA FLOW
β
βΌ
CACHE
β
βΌ
OPTIMIZE COMPONENTS
β
βΌ
OPTIMIZE MODEL ROUTING
β
βΌ
BENCHMARK QUALITY
β
βΌ
LOAD TEST
β
βΌ
DEPLOY
β
βΌ
OBSERVE
β
βββββββββββββββββΊ
The fundamental production principle is:
Do the minimum amount of computation necessary to produce the required quality within the required latency and cost budget.
π 153. Key Takeaways¶
- RAG performance is broader than latency.
- Optimize latency, throughput, cost, scalability, and resource utilization together.
- Always measure before optimizing.
- Use distributed tracing to identify the real bottleneck.
- Monitor p50, p95, and p99 latency.
- Optimize the critical path.
- Parallelize independent retrieval operations.
- Bound concurrency to protect downstream services.
- Do not blindly increase Top-K.
- Separate retrieval candidates from final context.
- Use adaptive retrieval when appropriate.
- Use early exits when confidence is sufficient.
- Query rewriting should be conditional when possible.
- Multi-query retrieval should be used selectively.
- Batch embedding operations.
- Cache repeated query embeddings.
- Optimize vector indexes according to recall and latency requirements.
- ANN indexes trade some exactness for performance.
- HNSW search parameters require workload-specific tuning.
- Quantization can reduce memory and latency but requires quality benchmarking.
- Reduce retrieval payload sizes.
- Use connection pooling.
- Parallelize dense and sparse retrieval.
- Reduce reranking candidates before expensive reranking.
- Batch reranking when supported.
- Context optimization is one of the most important RAG performance techniques.
- Remove duplicate and irrelevant context.
- Use context budgets.
- Context compression can reduce token usage and latency.
- Optimize prompt size.
- Track prompt versions.
- Use prompt caching where appropriate.
- Track LLM time to first token separately from total generation time.
- Use model routing when different queries have different complexity.
- Streaming improves perceived responsiveness.
- Limit unnecessary output tokens.
- Use selective validation for appropriate workloads.
- Preserve source identity throughout the pipeline for efficient citation.
- Caching can significantly reduce repeated work.
- Cache invalidation and versioning are essential.
- Semantic caching requires careful handling of freshness and authorization.
- Batch ingestion and indexing.
- Use incremental indexing instead of rebuilding everything.
- Avoid unnecessary re-embedding.
- Use content hashes for change detection.
- Control vector database connections and concurrency.
- Use backpressure during overload.
- Separate interactive, batch, ingestion, and evaluation workloads.
- Use autoscaling based on actual workload characteristics.
- Network locality can materially affect RAG latency.
- Observability itself has a performance and cost footprint.
- Performance optimization must preserve retrieval and answer quality.
- Quality, latency, and cost form a continuous engineering trade-off.
- Benchmark every significant optimization.
- Use canary deployment for high-impact performance changes.
- Performance regression testing should become part of the production lifecycle.
- The best optimization is often eliminating unnecessary work.
π§ 154. Chapter Navigation¶
Part V β Advanced Retrieval-Augmented Generation¶
Previous:
07. RAG Observability
Next:
09. RAG Cost Optimization
Section:
06 β Production RAG Engineering
Production RAG Engineering Path¶
01 Prompt Assembly
β
02 Context Selection & Context Engineering
β
03 Response Validation
β
04 Citation & Source Attribution
β
05 Enterprise Response
β
06 RAG Evaluation & Benchmarking
β
07 RAG Observability
β
08 RAG Performance Optimization
β
09 RAG Cost Optimization
β
10 Production Retrieval Architecture
β
11 Building Production RAG Systems
Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems β One Chapter at a Time.