hybrid rag retrieval
Implement and benchmark hybrid RAG retrieval combining keyword search, vector embeddings, and rank fusion.
$9
Works with the AI Agents you already use
hybrid rag retrieval
Example session with this skill installed
My RAG app keeps missing specific product IDs like 'SKU-992-X' even though they are in the docs. Can you add BM25 and measure the improvement?
- Read your context and instructions
- Compiled the hybrid rag retrieval
I have analyzed the current retrieval pipeline and implemented a hybrid search strategy. I added a BM25 index alongside your existing vector store and fused the results using Reciprocal Rank Fusion (RRF). I also added a fallback mechanism so the system remains functional if the embedding service is unreachable.
Retrieval Benchmarks (N=30)
| Stage | Hit@3 | MRR | p50 Latency | Status |
|---|---|---|---|---|
| Baseline (Vector Only) | 0.62 | 0.48 | 115ms | Current |
| BM25 Only | 0.74 | 0.65 | 12ms | Tested |
| Hybrid (RRF) | 0.89 | 0.78 | 132ms | Selected |
Implementation Details
import numpy as np
from rank_bm25 import BM25Okapi
class HybridRetriever:
def __init__(self, vector_store, corpus):
self.vector_store = vector_store
self.tokenized_corpus = [doc.text.lower().split() for doc in corpus]
self.bm25 = BM25Okapi(self.tokenized_corpus)
self.corpus = corpus
def search(self, query, k=5):
# 1. Get Keyword Candidates
tokenized_query = query.lower().split()
bm25_scores = self.bm25.get_scores(tokenized_query)
bm25_indices = np.argsort(bm25_scores)[::-1][:24]
# 2. Get Vector Candidates
try:
vector_results = self.vector_store.similarity_search(query, k=24)
except Exception:
# Graceful fallback to BM25
return self.corpus[bm25_indices[:k]]
# 3. Reciprocal Rank Fusion (RRF)
# Combine lists using: score = sum(1 / (60 + rank))
# ... implementation of RRF fusion logic ...
return fused_results[:k]
Next steps
- Verify Tokenization: Ensure your tokenizer is not stripping the hyphens from IDs like
SKU-992-X. - Persistence: The BM25 index is currently in-memory; I can migrate this to a Postgres GIN index for persistence.
- Reranking: If you need higher precision, I can add a
cross-encoder/ms-marco-MiniLM-L-6-v2rerank stage.
Connects securely to your tools. The creator never sees your data.
About this skill
Standard vector search often fails on exact identifiers like invoice numbers, tickers, and specific percentages. This skill implements hybrid retrieval by combining keyword search (BM25) with dense embeddings and rank fusion to ensure both semantic meaning and exact matches are captured.
What it does
- Hybrid fusion combines BM25 and dense vector results using Reciprocal Rank Fusion (RRF) or weighted min-max.
- Performance benchmarking measures hit@k and MRR before and after every architectural change to prove utility.
- Cross-encoder reranking optimizes the final candidate list for higher precision at the cost of controlled latency.
- Graceful degradation ensures the system falls back to keyword search if embedding models fail to load.
- Production hardening fixes common RAG issues like duplicate chunks, stale indices, and lazy model loading.
How it works
- Baseline measurement uses a local evaluation set to record the current system's hit@3, MRR, and p50 latency.
- Incremental implementation adds BM25, RRF fusion, and optional reranking stages one by one.
- Benchmarking validates each stage against the baseline to ensure gains are not flattened by latency costs.
- Integration adapts the reference implementation to the existing stack while adding unit tests for edge cases.
Frameworks & tools
Works with Python and JavaScript stacks using Postgres (pgvector), Elasticsearch, OpenSearch, Qdrant, MongoDB, or Weaviate. Supported libraries include rank_bm25, fastembed, sentence-transformers, and LangChain.
Why this beats prompting it yourself
Generic prompts often result in inefficient retrieval chains that rebuild indices on every query or fail silently when models OOM. This skill enforces a rigorous measurement-first approach and handles production pitfalls like tenant-leaking and tokenizer issues that manual code often misses.
Use cases
- Financial search where exact tickers and numeric margin figures must be retrieved accurately.
- Technical support requiring precise matches for error codes, log IDs, and specific CLI commands.
- Legacy systems needing to upgrade basic SQL search with semantic capabilities without losing keyword precision.
- Cost optimization by measuring if expensive cross-encoders actually improve metrics for a specific corpus.
Known limitations
Hybrid search requires more memory for keyword indices and embedding models. Retrieval gains may be negligible for corpora smaller than 50 chunks.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for LLM Ops.
Minimal impact on corpora under 50 chunks. Requires additional memory for local models or API budget for remote embeddings. Cross-encoders add measurable latency to the query pipeline.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 14 days ago