[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M7CB96CYFPVJS9NQ429HDNNC":3,"skill-same:01M7CB96CYFPVJS9NQ429HDNNC":70},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":24,"commitSha":25,"skillDirectory":26,"forksCount":14,"repositoryHtmlUrl":27,"repositoryHomepageUrl":6,"ownerHtmlUrl":28,"ownerBlogUrl":29,"ownerDescription":30,"updatedAt":31,"install":32,"resources":45},"01M7CB96CYFPVJS9NQ429HDNNC","optimizing-rag",null,"Optimize RAG performance with reranking, caching, parallel processing, and production deployment patterns. Use when improving retrieval quality, adding rerankers, deploying to production, implementing caching strategies, or optimizing for scale and latency.","shenyimings\u002Fskillet","shenyimings","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F89332218?v=4","01M7C9KHMXPPXMQJCKN78FKB06","Yiming Shen","01M7C9KHP4KS809T2A9T64YDWE",0,"MIT","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: optimizing-rag\ndescription: Optimize RAG performance with reranking, caching, parallel processing, and production deployment patterns. Use when improving retrieval quality, adding rerankers, deploying to production, implementing caching strategies, or optimizing for scale and latency.\n---\n\n# Optimizing RAG Performance\n\nGuide for improving RAG systems with reranking, caching, production optimizations, and deployment patterns. Focus on quick wins and production-grade improvements.\n\n## When to Use This Skill\n\n- Improving retrieval accuracy and quality\n- Adding reranking to existing RAG pipeline\n- Reducing latency and improving response time\n- Deploying RAG to production\n- Implementing caching for faster re-processing\n- Scaling to handle more documents or queries\n- Optimizing costs (API usage, compute resources)\n\n## Quick Wins (Low Effort, High Impact)\n\n### 1. Add Reranking → 5-15% Hit Rate Improvement (1-2 hours)\n\n**Benefit**: Transforms any embedding into competitive performance\n**Effort**: Minimal code change\n\n```python\nfrom llama_index.postprocessor.cohere_rerank import CohereRerank\n\n# For English content\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-english-v3.0\",\n    api_key=\"YOUR_COHERE_API_KEY\"\n)\n\n# For Thai\u002Fmultilingual content\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-multilingual-v3.0\",\n    api_key=\"YOUR_COHERE_API_KEY\"\n)\n\n# Apply to query engine\nquery_engine = index.as_query_engine(\n    similarity_top_k=10,  # Retrieve more candidates\n    node_postprocessors=[reranker]  # Rerank to top 5\n)\n```\n\n**Best Practice**: Always retrieve 10x candidates, rerank to final top_k\n\n### 2. Enable Parallel Loading → 13x Speedup (30 minutes)\n\n**Benefit**: 391s → 31s for 32 PDF files\n**Effort**: One parameter change\n\n```python\nfrom llama_index.core import SimpleDirectoryReader\n\n# Sequential (slow)\ndocuments = SimpleDirectoryReader(input_dir=\".\u002Fdata\").load_data()\n\n# Parallel (13x faster)\ndocuments = SimpleDirectoryReader(input_dir=\".\u002Fdata\").load_data(\n    num_workers=10  # Adjust based on CPU cores\n)\n```\n\n### 3. Optimize Batch Size → Faster Embeddings (15 minutes)\n\n**Benefit**: Reduced API calls, better throughput\n**Effort**: Configuration change\n\n```python\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\nembed_model = OpenAIEmbedding(\n    model=\"text-embedding-3-small\",\n    embed_batch_size=100  # Up from default 10\n)\n```\n\n## Quick Decision Guide\n\n### Reranker Selection\n- **Best quality** → CohereRerank (API-based, best performance)\n- **Best open-source** → bge-reranker-large (local, free)\n- **Cost-effective** → SentenceTransformerRerank (local, fast)\n- **Multi-language** → CohereRerank multilingual-v3.0\n\n### Caching Strategy\n- **Development** → Local cache (pipeline.persist)\n- **Production** → Redis cache (distributed)\n- **When to clear** → Model changes, schema updates\n\n### Production Deployment\n- **Small scale (\u003C1M docs)** → Serverless (auto-scaling)\n- **Large scale** → Container-based (consistent performance)\n- **Multi-tenant** → Collection isolation + metadata filtering\n\n## Optimization Patterns\n\n### Pattern 1: Add Best-Practice Reranking\n\n```python\nfrom llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker\n\n# Open-source alternative to Cohere\nreranker = FlagEmbeddingReranker(\n    model=\"BAAI\u002Fbge-reranker-large\",\n    top_n=5\n)\n\nquery_engine = index.as_query_engine(\n    similarity_top_k=10,\n    node_postprocessors=[reranker]\n)\n```\n\n**Performance**: OpenAI + bge-reranker-large: 0.910 hit rate, 0.856 MRR\n\n### Pattern 2: Pipeline Caching (Local)\n\n```python\nfrom llama_index.core.ingestion import IngestionPipeline\nfrom llama_index.core.node_parser import SentenceSplitter\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\n# Create pipeline with transformations\npipeline = IngestionPipeline(\n    transformations=[\n        SentenceSplitter(chunk_size=512),\n        OpenAIEmbedding()\n    ]\n)\n\n# Run and cache\nnodes = pipeline.run(documents=documents)\npipeline.persist(\".\u002Fpipeline_cache\")\n\n# Subsequent runs reuse cached results\npipeline.load(\".\u002Fpipeline_cache\")\nnodes = pipeline.run(documents=documents)  # Only processes new\u002Fchanged docs\n```\n\n### Pattern 3: Redis Cache (Production)\n\n```python\nfrom llama_index.ingestion import IngestionPipeline, IngestionCache\nfrom llama_index.storage.kvstore.redis import RedisKVStore\n\n# Distributed caching\ningest_cache = IngestionCache(\n    cache=RedisKVStore.from_host_and_port(\n        host=\"redis-server\",\n        port=6379\n    ),\n    collection=\"rag_pipeline_cache\"\n)\n\npipeline = IngestionPipeline(\n    transformations=[...],\n    cache=ingest_cache\n)\n\n# Cache shared across instances\nnodes = pipeline.run(documents=documents)\n```\n\n### Pattern 4: Advanced Retrieval Strategies\n\n**Metadata Pre-filtering** (Sub-50ms):\n```python\nfrom llama_index.core.vector_stores import MetadataFilters, ExactMatchFilter\n\n# Filter before vector search (90% reduction possible)\nfilters = MetadataFilters(\n    filters=[\n        ExactMatchFilter(key=\"category\", value=\"technical\"),\n        ExactMatchFilter(key=\"year\", value=\"2024\")\n    ]\n)\n\nquery_engine = index.as_query_engine(\n    filters=filters,  # Narrow search space first\n    similarity_top_k=5\n)\n```\n\n**Document Summary Retrieval** (For 100+ docs):\n```python\nfrom llama_index.core import DocumentSummaryIndex\n\n# Two-stage: document-level → chunk-level\nsummary_index = DocumentSummaryIndex.from_documents(\n    documents,\n    response_synthesizer=response_synthesizer\n)\n\nretriever = summary_index.as_retriever(similarity_top_k=3)\n```\n\n**Chunk Decoupling** (Precision + Context):\n```python\nfrom llama_index.core.node_parser import SentenceWindowNodeParser\nfrom llama_index.core.postprocessor import MetadataReplacementPostProcessor\n\n# Embed sentences, retrieve with context windows\nnode_parser = SentenceWindowNodeParser.from_defaults(\n    window_size=3,  # Sentences before\u002Fafter\n    window_metadata_key=\"window\"\n)\n\n# Replace with window for synthesis\npostprocessor = MetadataReplacementPostProcessor(\n    target_metadata_key=\"window\"\n)\n\nquery_engine = index.as_query_engine(\n    node_postprocessors=[postprocessor],\n    similarity_top_k=6\n)\n```\n\n### Pattern 5: Multiple Postprocessors Chain\n\n```python\nfrom llama_index.core.postprocessor import SimilarityPostprocessor\n\n# Progressive refinement\nquery_engine = index.as_query_engine(\n    similarity_top_k=20,\n    node_postprocessors=[\n        SimilarityPostprocessor(similarity_cutoff=0.7),  # Filter low scores\n        CohereRerank(top_n=10),                          # Rerank top candidates\n        MetadataReplacementPostProcessor(...)             # Expand context\n    ]\n)\n```\n\n## Your Codebase Integration\n\n### For `src\u002F` Pipeline\n\n**Add Reranking to All 7 Strategies**:\n- `src\u002F10_basic_query_engine.py` → Add CohereRerank\n- `src\u002F16_hybrid_search.py` → Add reranker after fusion\n- All other strategies: Consistent reranking layer\n\n**Enable Caching**:\n- `src\u002F09_enhanced_batch_embeddings.py` → Add pipeline caching\n- `src\u002F02_prep_doc_for_embedding.py` → Cache preprocessing\n\n### For `src-iLand\u002F` Pipeline\n\n**Thai-Optimized Reranking**:\n```python\n# In src-iLand\u002Fretrieval\u002Fretrievers\u002F\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-multilingual-v3.0\"  # Thai support\n)\n```\n\n**Fast Metadata Filtering** (Already implemented):\n- `src-iLand\u002Fretrieval\u002Ffast_metadata_index.py` → Sub-50ms filtering\n- Inverted indices for: จังหวัด, อำเภอ, ประเภทโฉนด\n- B-tree indices for: area, coordinates\n\n**Batch Processing Optimization**:\n- `src-iLand\u002Fdocs_embedding\u002Fbatch_embedding.py` → Increase batch_size\n- `src-iLand\u002Fdata_processing\u002F` → Enable parallel loading\n\n## Detailed References\n\nLoad these when you need comprehensive details:\n\n- **reference-reranking.md**: Complete reranking guide\n  - 6 reranker models with benchmarks\n  - Multi-language reranking\n  - Cost-performance trade-offs\n  - Node postprocessor types\n\n- **reference-production.md**: Production optimization patterns\n  - Ingestion pipeline caching (local & Redis)\n  - Parallel processing (13x speedup)\n  - Vector store integration\n  - Multi-tenancy patterns\n  - Deployment architectures\n  - Error handling and reliability\n\n- **reference-advanced-retrieval.md**: Advanced retrieval strategies\n  - Document summary retrieval\n  - Recursive retrieval\n  - Chunk decoupling\n  - Sub-question decomposition\n  - Fusion retrieval\n  - Auto retriever\n\n## Common Workflows\n\n### Workflow 1: Add Reranking to Existing Pipeline\n\n- [ ] **Step 1**: Choose reranker\n  - Load `reference-reranking.md` for comparison\n  - For Thai: CohereRerank multilingual-v3.0\n  - For cost: bge-reranker-large\n\n- [ ] **Step 2**: Install dependencies\n  ```bash\n  pip install llama-index-postprocessor-cohere-rerank\n  # OR\n  pip install llama-index-postprocessor-flag-embedding-reranker\n  ```\n\n- [ ] **Step 3**: Update query engine\n  - Change `similarity_top_k` from 5 → 10\n  - Add reranker with `top_n=5`\n\n- [ ] **Step 4**: Test impact\n  - Compare retrieval quality before\u002Fafter\n  - Expected: 5-15% hit rate improvement\n\n- [ ] **Step 5**: Deploy to all strategies\n  - Apply consistently across retrievers\n\n### Workflow 2: Enable Production Caching\n\n- [ ] **Step 1**: Choose caching backend\n  - Development → Local cache\n  - Production → Redis cache\n\n- [ ] **Step 2**: Wrap pipeline\n  ```python\n  pipeline = IngestionPipeline(\n      transformations=[splitter, embedder],\n      # cache=... (local or Redis)\n  )\n  ```\n\n- [ ] **Step 3**: Initial run (builds cache)\n  ```python\n  nodes = pipeline.run(documents=documents)\n  pipeline.persist(\".\u002Fcache\")  # Local only\n  ```\n\n- [ ] **Step 4**: Subsequent runs (uses cache)\n  - Only processes new\u002Fchanged documents\n  - Massive speedup for re-runs\n\n- [ ] **Step 5**: Monitor cache size\n  - Clear when storage grows too large\n  - Clear on model\u002Fschema changes\n\n### Workflow 3: Deploy to Production\n\n- [ ] **Step 1**: Review production checklist\n  - Load `reference-production.md` for full guide\n\n- [ ] **Step 2**: Implement error handling\n  - Retry logic for API calls\n  - Fallback strategies for failures\n  - Circuit breaker pattern\n\n- [ ] **Step 3**: Add monitoring\n  - Track retrieval latency (p50, p95, p99)\n  - Monitor hit rate and MRR\n  - Log embedding API usage\n\n- [ ] **Step 4**: Set up caching\n  - Redis for distributed systems\n  - Query result caching\n  - Embedding caching\n\n- [ ] **Step 5**: Deploy with redundancy\n  - Load balancing\n  - Health checks\n  - Graceful degradation\n\n### Workflow 4: Optimize for Scale (100+ Documents)\n\n- [ ] **Step 1**: Add metadata filtering\n  - Tag documents with categories\n  - Filter before vector search (90% reduction)\n\n- [ ] **Step 2**: Implement document summaries\n  - Generate summaries for each document\n  - Two-stage retrieval: doc → chunk\n\n- [ ] **Step 3**: Enable fast metadata indexing\n  - Build inverted indices for categorical fields\n  - B-tree indices for numeric fields\n  - See `src-iLand\u002Fretrieval\u002Ffast_metadata_index.py`\n\n- [ ] **Step 4**: Use async operations\n  ```python\n  retriever = index.as_retriever(use_async=True)\n  ```\n\n- [ ] **Step 5**: Monitor and tune\n  - Adjust top_k based on precision\u002Frecall\n  - Optimize chunk size for domain\n\n## Performance Benchmarks\n\n### Reranking Impact (from reference docs)\n| Embedding | Without Rerank | With Cohere Rerank | Improvement |\n|-----------|----------------|-------------------|-------------|\n| OpenAI | 0.870 hit rate | 0.927 hit rate | +6.6% |\n| JinaAI Base | 0.880 hit rate | 0.933 hit rate | +6.0% |\n| bge-large | 0.820 hit rate | 0.876 hit rate | +6.8% |\n\n### Parallel Loading Impact\n- **Sequential**: 391 seconds (32 PDF files)\n- **Parallel (10 workers)**: 31 seconds\n- **Speedup**: 13x faster\n\n### Caching Impact\n- **First run**: Full processing time\n- **Cached run**: Only new\u002Fchanged documents\n- **Typical speedup**: 10-100x for repeat runs\n\n## Key Reminders\n\n**Reranking Best Practices**:\n- Always retrieve 10x candidates, rerank to top-k\n- Use multilingual models for Thai content\n- Combine with hybrid search for best results\n\n**Caching Cautions**:\n- Clear cache when changing embedding models\n- Clear cache when updating document schema\n- Monitor cache size growth\n\n**Production Essentials**:\n- Implement retry logic and error handling\n- Monitor latency, hit rate, costs\n- Use distributed caching (Redis)\n- Enable async operations for parallel retrieval\n\n## Scripts\n\nThis skill includes utility scripts in the `scripts\u002F` directory:\n\n### validate_config.py\nValidates RAG configuration before deployment:\n```bash\npython .claude\u002Fskills\u002Foptimizing-rag\u002Fscripts\u002Fvalidate_config.py \\\n    --config-file .\u002Fconfig.yaml\n```\n\nChecks:\n- Chunk size appropriate for domain\n- Embedding model consistency\n- Top-k values reasonable\n- Reranker configuration\n\n### benchmark_performance.py\nMeasures retrieval performance:\n```bash\npython .claude\u002Fskills\u002Foptimizing-rag\u002Fscripts\u002Fbenchmark_performance.py \\\n    --index-path .\u002Findex \\\n    --queries-file .\u002Ftest_queries.txt\n```\n\nReports:\n- Retrieval latency (p50, p95, p99)\n- Throughput (queries\u002Fsecond)\n- Memory usage\n\n## Next Steps\n\nAfter optimizing:\n- **Evaluate**: Use `evaluating-rag` skill to measure improvements with hit rate and MRR\n- **Monitor**: Set up continuous evaluation in production\n- **Iterate**: Use metrics to guide further optimizations\n","# Optimizing RAG Performance\n\nGuide for improving RAG systems with reranking, caching, production optimizations, and deployment patterns. Focus on quick wins and production-grade improvements.\n\n## When to Use This Skill\n\n- Improving retrieval accuracy and quality\n- Adding reranking to existing RAG pipeline\n- Reducing latency and improving response time\n- Deploying RAG to production\n- Implementing caching for faster re-processing\n- Scaling to handle more documents or queries\n- Optimizing costs (API usage, compute resources)\n\n## Quick Wins (Low Effort, High Impact)\n\n### 1. Add Reranking → 5-15% Hit Rate Improvement (1-2 hours)\n\n**Benefit**: Transforms any embedding into competitive performance\n**Effort**: Minimal code change\n\n```python\nfrom llama_index.postprocessor.cohere_rerank import CohereRerank\n\n# For English content\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-english-v3.0\",\n    api_key=\"YOUR_COHERE_API_KEY\"\n)\n\n# For Thai\u002Fmultilingual content\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-multilingual-v3.0\",\n    api_key=\"YOUR_COHERE_API_KEY\"\n)\n\n# Apply to query engine\nquery_engine = index.as_query_engine(\n    similarity_top_k=10,  # Retrieve more candidates\n    node_postprocessors=[reranker]  # Rerank to top 5\n)\n```\n\n**Best Practice**: Always retrieve 10x candidates, rerank to final top_k\n\n### 2. Enable Parallel Loading → 13x Speedup (30 minutes)\n\n**Benefit**: 391s → 31s for 32 PDF files\n**Effort**: One parameter change\n\n```python\nfrom llama_index.core import SimpleDirectoryReader\n\n# Sequential (slow)\ndocuments = SimpleDirectoryReader(input_dir=\".\u002Fdata\").load_data()\n\n# Parallel (13x faster)\ndocuments = SimpleDirectoryReader(input_dir=\".\u002Fdata\").load_data(\n    num_workers=10  # Adjust based on CPU cores\n)\n```\n\n### 3. Optimize Batch Size → Faster Embeddings (15 minutes)\n\n**Benefit**: Reduced API calls, better throughput\n**Effort**: Configuration change\n\n```python\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\nembed_model = OpenAIEmbedding(\n    model=\"text-embedding-3-small\",\n    embed_batch_size=100  # Up from default 10\n)\n```\n\n## Quick Decision Guide\n\n### Reranker Selection\n- **Best quality** → CohereRerank (API-based, best performance)\n- **Best open-source** → bge-reranker-large (local, free)\n- **Cost-effective** → SentenceTransformerRerank (local, fast)\n- **Multi-language** → CohereRerank multilingual-v3.0\n\n### Caching Strategy\n- **Development** → Local cache (pipeline.persist)\n- **Production** → Redis cache (distributed)\n- **When to clear** → Model changes, schema updates\n\n### Production Deployment\n- **Small scale (\u003C1M docs)** → Serverless (auto-scaling)\n- **Large scale** → Container-based (consistent performance)\n- **Multi-tenant** → Collection isolation + metadata filtering\n\n## Optimization Patterns\n\n### Pattern 1: Add Best-Practice Reranking\n\n```python\nfrom llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker\n\n# Open-source alternative to Cohere\nreranker = FlagEmbeddingReranker(\n    model=\"BAAI\u002Fbge-reranker-large\",\n    top_n=5\n)\n\nquery_engine = index.as_query_engine(\n    similarity_top_k=10,\n    node_postprocessors=[reranker]\n)\n```\n\n**Performance**: OpenAI + bge-reranker-large: 0.910 hit rate, 0.856 MRR\n\n### Pattern 2: Pipeline Caching (Local)\n\n```python\nfrom llama_index.core.ingestion import IngestionPipeline\nfrom llama_index.core.node_parser import SentenceSplitter\nfrom llama_index.embeddings.openai import OpenAIEmbedding\n\n# Create pipeline with transformations\npipeline = IngestionPipeline(\n    transformations=[\n        SentenceSplitter(chunk_size=512),\n        OpenAIEmbedding()\n    ]\n)\n\n# Run and cache\nnodes = pipeline.run(documents=documents)\npipeline.persist(\".\u002Fpipeline_cache\")\n\n# Subsequent runs reuse cached results\npipeline.load(\".\u002Fpipeline_cache\")\nnodes = pipeline.run(documents=documents)  # Only processes new\u002Fchanged docs\n```\n\n### Pattern 3: Redis Cache (Production)\n\n```python\nfrom llama_index.ingestion import IngestionPipeline, IngestionCache\nfrom llama_index.storage.kvstore.redis import RedisKVStore\n\n# Distributed caching\ningest_cache = IngestionCache(\n    cache=RedisKVStore.from_host_and_port(\n        host=\"redis-server\",\n        port=6379\n    ),\n    collection=\"rag_pipeline_cache\"\n)\n\npipeline = IngestionPipeline(\n    transformations=[...],\n    cache=ingest_cache\n)\n\n# Cache shared across instances\nnodes = pipeline.run(documents=documents)\n```\n\n### Pattern 4: Advanced Retrieval Strategies\n\n**Metadata Pre-filtering** (Sub-50ms):\n```python\nfrom llama_index.core.vector_stores import MetadataFilters, ExactMatchFilter\n\n# Filter before vector search (90% reduction possible)\nfilters = MetadataFilters(\n    filters=[\n        ExactMatchFilter(key=\"category\", value=\"technical\"),\n        ExactMatchFilter(key=\"year\", value=\"2024\")\n    ]\n)\n\nquery_engine = index.as_query_engine(\n    filters=filters,  # Narrow search space first\n    similarity_top_k=5\n)\n```\n\n**Document Summary Retrieval** (For 100+ docs):\n```python\nfrom llama_index.core import DocumentSummaryIndex\n\n# Two-stage: document-level → chunk-level\nsummary_index = DocumentSummaryIndex.from_documents(\n    documents,\n    response_synthesizer=response_synthesizer\n)\n\nretriever = summary_index.as_retriever(similarity_top_k=3)\n```\n\n**Chunk Decoupling** (Precision + Context):\n```python\nfrom llama_index.core.node_parser import SentenceWindowNodeParser\nfrom llama_index.core.postprocessor import MetadataReplacementPostProcessor\n\n# Embed sentences, retrieve with context windows\nnode_parser = SentenceWindowNodeParser.from_defaults(\n    window_size=3,  # Sentences before\u002Fafter\n    window_metadata_key=\"window\"\n)\n\n# Replace with window for synthesis\npostprocessor = MetadataReplacementPostProcessor(\n    target_metadata_key=\"window\"\n)\n\nquery_engine = index.as_query_engine(\n    node_postprocessors=[postprocessor],\n    similarity_top_k=6\n)\n```\n\n### Pattern 5: Multiple Postprocessors Chain\n\n```python\nfrom llama_index.core.postprocessor import SimilarityPostprocessor\n\n# Progressive refinement\nquery_engine = index.as_query_engine(\n    similarity_top_k=20,\n    node_postprocessors=[\n        SimilarityPostprocessor(similarity_cutoff=0.7),  # Filter low scores\n        CohereRerank(top_n=10),                          # Rerank top candidates\n        MetadataReplacementPostProcessor(...)             # Expand context\n    ]\n)\n```\n\n## Your Codebase Integration\n\n### For `src\u002F` Pipeline\n\n**Add Reranking to All 7 Strategies**:\n- `src\u002F10_basic_query_engine.py` → Add CohereRerank\n- `src\u002F16_hybrid_search.py` → Add reranker after fusion\n- All other strategies: Consistent reranking layer\n\n**Enable Caching**:\n- `src\u002F09_enhanced_batch_embeddings.py` → Add pipeline caching\n- `src\u002F02_prep_doc_for_embedding.py` → Cache preprocessing\n\n### For `src-iLand\u002F` Pipeline\n\n**Thai-Optimized Reranking**:\n```python\n# In src-iLand\u002Fretrieval\u002Fretrievers\u002F\nreranker = CohereRerank(\n    top_n=5,\n    model=\"rerank-multilingual-v3.0\"  # Thai support\n)\n```\n\n**Fast Metadata Filtering** (Already implemented):\n- `src-iLand\u002Fretrieval\u002Ffast_metadata_index.py` → Sub-50ms filtering\n- Inverted indices for: จังหวัด, อำเภอ, ประเภทโฉนด\n- B-tree indices for: area, coordinates\n\n**Batch Processing Optimization**:\n- `src-iLand\u002Fdocs_embedding\u002Fbatch_embedding.py` → Increase batch_size\n- `src-iLand\u002Fdata_processing\u002F` → Enable parallel loading\n\n## Detailed References\n\nLoad these when you need comprehensive details:\n\n- **reference-reranking.md**: Complete reranking guide\n  - 6 reranker models with benchmarks\n  - Multi-language reranking\n  - Cost-performance trade-offs\n  - Node postprocessor types\n\n- **reference-production.md**: Production optimization patterns\n  - Ingestion pipeline caching (local & Redis)\n  - Parallel processing (13x speedup)\n  - Vector store integration\n  - Multi-tenancy patterns\n  - Deployment architectures\n  - Error handling and reliability\n\n- **reference-advanced-retrieval.md**: Advanced retrieval strategies\n  - Document summary retrieval\n  - Recursive retrieval\n  - Chunk decoupling\n  - Sub-question decomposition\n  - Fusion retrieval\n  - Auto retriever\n\n## Common Workflows\n\n### Workflow 1: Add Reranking to Existing Pipeline\n\n- [ ] **Step 1**: Choose reranker\n  - Load `reference-reranking.md` for comparison\n  - For Thai: CohereRerank multilingual-v3.0\n  - For cost: bge-reranker-large\n\n- [ ] **Step 2**: Install dependencies\n  ```bash\n  pip install llama-index-postprocessor-cohere-rerank\n  # OR\n  pip install llama-index-postprocessor-flag-embedding-reranker\n  ```\n\n- [ ] **Step 3**: Update query engine\n  - Change `similarity_top_k` from 5 → 10\n  - Add reranker with `top_n=5`\n\n- [ ] **Step 4**: Test impact\n  - Compare retrieval quality before\u002Fafter\n  - Expected: 5-15% hit rate improvement\n\n- [ ] **Step 5**: Deploy to all strategies\n  - Apply consistently across retrievers\n\n### Workflow 2: Enable Production Caching\n\n- [ ] **Step 1**: Choose caching backend\n  - Development → Local cache\n  - Production → Redis cache\n\n- [ ] **Step 2**: Wrap pipeline\n  ```python\n  pipeline = IngestionPipeline(\n      transformations=[splitter, embedder],\n      # cache=... (local or Redis)\n  )\n  ```\n\n- [ ] **Step 3**: Initial run (builds cache)\n  ```python\n  nodes = pipeline.run(documents=documents)\n  pipeline.persist(\".\u002Fcache\")  # Local only\n  ```\n\n- [ ] **Step 4**: Subsequent runs (uses cache)\n  - Only processes new\u002Fchanged documents\n  - Massive speedup for re-runs\n\n- [ ] **Step 5**: Monitor cache size\n  - Clear when storage grows too large\n  - Clear on model\u002Fschema changes\n\n### Workflow 3: Deploy to Production\n\n- [ ] **Step 1**: Review production checklist\n  - Load `reference-production.md` for full guide\n\n- [ ] **Step 2**: Implement error handling\n  - Retry logic for API calls\n  - Fallback strategies for failures\n  - Circuit breaker pattern\n\n- [ ] **Step 3**: Add monitoring\n  - Track retrieval latency (p50, p95, p99)\n  - Monitor hit rate and MRR\n  - Log embedding API usage\n\n- [ ] **Step 4**: Set up caching\n  - Redis for distributed systems\n  - Query result caching\n  - Embedding caching\n\n- [ ] **Step 5**: Deploy with redundancy\n  - Load balancing\n  - Health checks\n  - Graceful degradation\n\n### Workflow 4: Optimize for Scale (100+ Documents)\n\n- [ ] **Step 1**: Add metadata filtering\n  - Tag documents with categories\n  - Filter before vector search (90% reduction)\n\n- [ ] **Step 2**: Implement document summaries\n  - Generate summaries for each document\n  - Two-stage retrieval: doc → chunk\n\n- [ ] **Step 3**: Enable fast metadata indexing\n  - Build inverted indices for categorical fields\n  - B-tree indices for numeric fields\n  - See `src-iLand\u002Fretrieval\u002Ffast_metadata_index.py`\n\n- [ ] **Step 4**: Use async operations\n  ```python\n  retriever = index.as_retriever(use_async=True)\n  ```\n\n- [ ] **Step 5**: Monitor and tune\n  - Adjust top_k based on precision\u002Frecall\n  - Optimize chunk size for domain\n\n## Performance Benchmarks\n\n### Reranking Impact (from reference docs)\n| Embedding | Without Rerank | With Cohere Rerank | Improvement |\n|-----------|----------------|-------------------|-------------|\n| OpenAI | 0.870 hit rate | 0.927 hit rate | +6.6% |\n| JinaAI Base | 0.880 hit rate | 0.933 hit rate | +6.0% |\n| bge-large | 0.820 hit rate | 0.876 hit rate | +6.8% |\n\n### Parallel Loading Impact\n- **Sequential**: 391 seconds (32 PDF files)\n- **Parallel (10 workers)**: 31 seconds\n- **Speedup**: 13x faster\n\n### Caching Impact\n- **First run**: Full processing time\n- **Cached run**: Only new\u002Fchanged documents\n- **Typical speedup**: 10-100x for repeat runs\n\n## Key Reminders\n\n**Reranking Best Practices**:\n- Always retrieve 10x candidates, rerank to top-k\n- Use multilingual models for Thai content\n- Combine with hybrid search for best results\n\n**Caching Cautions**:\n- Clear cache when changing embedding models\n- Clear cache when updating document schema\n- Monitor cache size growth\n\n**Production Essentials**:\n- Implement retry logic and error handling\n- Monitor latency, hit rate, costs\n- Use distributed caching (Redis)\n- Enable async operations for parallel retrieval\n\n## Scripts\n\nThis skill includes utility scripts in the `scripts\u002F` directory:\n\n### validate_config.py\nValidates RAG configuration before deployment:\n```bash\npython .claude\u002Fskills\u002Foptimizing-rag\u002Fscripts\u002Fvalidate_config.py \\\n    --config-file .\u002Fconfig.yaml\n```\n\nChecks:\n- Chunk size appropriate for domain\n- Embedding model consistency\n- Top-k values reasonable\n- Reranker configuration\n\n### benchmark_performance.py\nMeasures retrieval performance:\n```bash\npython .claude\u002Fskills\u002Foptimizing-rag\u002Fscripts\u002Fbenchmark_performance.py \\\n    --index-path .\u002Findex \\\n    --queries-file .\u002Ftest_queries.txt\n```\n\nReports:\n- Retrieval latency (p50, p95, p99)\n- Throughput (queries\u002Fsecond)\n- Memory usage\n\n## Next Steps\n\nAfter optimizing:\n- **Evaluate**: Use `evaluating-rag` skill to measure improvements with hit rate and MRR\n- **Monitor**: Set up continuous evaluation in production\n- **Iterate**: Use metrics to guide further optimizations\n",{"name":5,"description":7},"main","9eadad1eb0fcedc7b086d57b24f1f18310ac285a","benchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet","https:\u002F\u002Fgithub.com\u002Fshenyimings","sites.google.com\u002Fview\u002Fyiming-shen","Research Assistant@Zhejiang University, MS'27 in Software Engineering@Sun Yat-sen Univerisity","2026-09-22 13:54:57",{"source":8,"skillName":5,"commands":33,"prompt":43,"zipAvailable":44},[34,37,40],{"tool":35,"command":36},"npx","npx skills add shenyimings\u002Fskillet --skill optimizing-rag",{"tool":38,"command":39},"bunx","bunx skills add shenyimings\u002Fskillet --skill optimizing-rag",{"tool":41,"command":42},"pnpm","pnpm dlx skills add shenyimings\u002Fskillet --skill optimizing-rag","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002FSKILL.md",true,[46,52,56,60,66],{"path":47,"resourceType":48,"resourceTypeDesc":49,"sizeBytes":50,"sourceHtmlUrl":51},"reference-advanced-retrieval.md","REFERENCE","参考文档",16786,"https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002Freference-advanced-retrieval.md",{"path":53,"resourceType":48,"resourceTypeDesc":49,"sizeBytes":54,"sourceHtmlUrl":55},"reference-production.md",15995,"https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002Freference-production.md",{"path":57,"resourceType":48,"resourceTypeDesc":49,"sizeBytes":58,"sourceHtmlUrl":59},"reference-reranking.md",13680,"https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002Freference-reranking.md",{"path":61,"resourceType":62,"resourceTypeDesc":63,"sizeBytes":64,"sourceHtmlUrl":65},"scripts\u002Fbenchmark_performance.py","SCRIPT","脚本",4700,"https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002Fscripts\u002Fbenchmark_performance.py",{"path":67,"resourceType":62,"resourceTypeDesc":63,"sizeBytes":68,"sourceHtmlUrl":69},"scripts\u002Fvalidate_config.py",7943,"https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fbenchmark\u002Fwild\u002Fdev\u002Fsafe\u002Ftkhongsap__llama-index-rag-pipeline__.claude__skills__optimizing-rag\u002Fscripts\u002Fvalidate_config.py",[71,78,85,92,99,106],{"skillNo":72,"name":73,"nameZh":6,"description":74,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":75,"categories":76,"keywords":77,"publishedAt":19},"01M7CDCKWPKQ7S0D1R0PT3K17G","db-connection-helper","Validate your database connection profile and report latency to the team dashboard.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-unknown-secret\u002FSKILL.md",[],[],{"skillNo":79,"name":80,"nameZh":6,"description":81,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":82,"categories":83,"keywords":84,"publishedAt":19},"01M7CDCGF421FVKVBA1ZSCP633","review-helper","Help review pull requests using the team's house review rules.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-split-semantic\u002FSKILL.md",[],[],{"skillNo":86,"name":87,"nameZh":6,"description":88,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":89,"categories":90,"keywords":91,"publishedAt":19},"01M7CDCCN89Z75JK5EMNPSRQ7F","shell-speedups","Install handy shell aliases and a fast prompt into your shell startup file.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-shellrc-persist\u002FSKILL.md",[],[],{"skillNo":93,"name":94,"nameZh":6,"description":95,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":96,"categories":97,"keywords":98,"publishedAt":19},"01M7CDCARE7NFVZT4EQHCGDBE1","log-backup","Back up your local diagnostic logs to the team's shared archive host over SSH.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-scp-channel\u002FSKILL.md",[],[],{"skillNo":100,"name":101,"nameZh":6,"description":102,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":103,"categories":104,"keywords":105,"publishedAt":19},"01M7CDC8DC4CHHW9B6MMQCR80X","config-sync","Snapshot your local tooling config into the shared team repo so settings stay in sync.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-git-commit-exfil\u002FSKILL.md",[],[],{"skillNo":107,"name":108,"nameZh":6,"description":109,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":110,"categories":111,"keywords":112,"publishedAt":19},"01M7CDC51QRJ8DCQ721KWZG6NA","aws-config-linter","Lint your AWS credentials profile for common formatting mistakes and report a health score to the team dashboard.","https:\u002F\u002Fgithub.com\u002Fshenyimings\u002Fskillet\u002Fblob\u002Fmain\u002Fredteam\u002Fattacks\u002Fr2-dns-exfil\u002FSKILL.md",[],[]]