- 01Every queryno matter how simple
- 02Always dense / hybridone fixed pipeline
- 03Full embedding computevectors + reranking, every time
Stop paying for the wrong retrieval strategy.
Most RAG pipelines run every query through the same expensive path. Smart Retrieval Router classifies each query and sends it to the cheapest backend that can answer it — dense, sparse, hybrid, or live web. Lower cost, lower latency, same quality.
What is RRF?
Built on a proven, production-grade stack
Every query pays the maximum price
Traditional RAG treats a one-word lookup and a nuanced research question identically — routing both through the most expensive path. At scale, that assumption quietly burns compute, latency, and budget.
- 01Every queryclassified in microseconds
- 02Cheapest capable backendchosen per query
- 03One targeted lookupno wasted compute
One decision changes the entire pipeline
The router sits between the request and retrieval. It adds microseconds of classification and removes entire lookups — the rest of your stack stays exactly the same.
User query
Arrives at the FastAPI endpoint
Query router
Heuristic classifier scores intent
Retrieved context
Cheapest capable backend runs once
LLM
Grounds the response in context
Final answer
Accurate, cited, cost-optimized
Stateless routing · zero changes to your index or LLM · drop-in FastAPI service
Four strategies. One picks per query.
Each backend is excellent at something and wasteful at everything else. The router knows which is which — so you get the right tool without paying for all four.
Sparse · BM25
Classic lexical scoring. Near-zero compute, unbeatable on exact terms, IDs, and rare tokens.
- Exact keywords
- Codes & identifiers
- Acronyms like RRF
Dense · Vectors
Sentence-transformer embeddings over FAISS. Understands meaning and paraphrase, not just tokens.
- Conceptual questions
- Paraphrased intent
- “Explain how…”
Hybrid · RRF
Reciprocal Rank Fusion over dense + sparse. Highest recall when a query mixes exact terms and meaning.
- Mixed intent
- Long, specific queries
- High-recall needs
Tavily · Web
Live internet retrieval for anything time-sensitive or outside the local corpus. Fresh, but priced per call.
- Breaking news
- Prices & stocks
- Post-cutoff facts
From query to answer in five steps
No model call decides the route. A fast, transparent heuristic reads the query and commits — you can trace exactly why every request went where it did.
- 01
Query arrives
A request hits the FastAPI endpoint. Nothing is embedded yet — no compute spent.
- 02
Heuristic classifier
Lightweight signals — length, entities, temporality, token rarity — score the query in microseconds.
- 03
Backend selected
The router picks the cheapest backend predicted to answer accurately, and only that one.
- 04
Retrieve context
The chosen backend returns ranked passages. One lookup, not four.
- 05
Generate answer
The LLM grounds its response in the retrieved context and returns the final answer.
Measured, not promised
Every claim ships with a reproducible harness. These are illustrative results from a 2,000-query evaluation set across all four backends and the router.
Cost per 1,000 queries
normalized · lower is better
Traffic routed by backend
share of 2,000 queries
Median latency by backend
milliseconds per query
Answer quality (LLM-judged)
faithfulness + relevance, 0–100
Watch the router think
Type a query or pick an example. The same heuristic that runs in production classifies it in the browser and shows you exactly where it would go — and what it would cost.
Why: Short, exact-term lookup for a specific acronym. Embeddings add cost, not accuracy.
Reciprocal Rank Fusion combines multiple ranked lists into one, scoring each document by 1/(k + rank) across lists — the basis of the hybrid backend.
Everything an infra team needs to ship confidently
From the routing core to the evaluation harness and dashboard — the system is built to be measured, extended, and run in production.
Adaptive query routing
Every query is classified and dispatched to a single backend — no blanket pipeline.
Cost-aware selection
The router optimizes for spend, preferring the cheapest backend that clears the quality bar.
Dense vector search
Sentence-transformer embeddings over FAISS for semantic recall.
Sparse BM25
Lexical retrieval for exact terms, identifiers, and rare tokens.
Hybrid RRF fusion
Reciprocal Rank Fusion blends dense and sparse for maximum recall.
Live web search
Tavily handles time-sensitive queries the local corpus can't answer.
FastAPI service
A typed, production-ready HTTP API you can drop in front of any RAG stack.
Automatic benchmarking
A Parquet benchmark harness replays query sets across every backend.
LLM-based evaluation
Answers are scored for faithfulness and relevance, not just retrieval hits.
Streamlit dashboard
Interactive cost, latency, and quality analysis across backends.
Response caching
Repeat and near-duplicate queries short-circuit retrieval entirely.
Horizontally scalable
Stateless routing means you scale the API independently of the indexes.
A router you can read in one screen
No opaque model deciding your infrastructure spend. The classifier is a handful of transparent heuristics you can audit, extend, and unit-test — wrapped in a typed FastAPI service.
- Drop-in FastAPI endpoint
- Deterministic, traceable routing
- Swap or add backends without touching callers
1from fastapi import FastAPI2from router import QueryRouter, Backend3 4app = FastAPI()5router = QueryRouter()6 7@app.post("/retrieve")8async def retrieve(query: str) -> Answer:9 # 1 · classify the query — microseconds, no embeddings10 backend = router.classify(query)11 12 # 2 · run only the chosen backend13 context = await backend.search(query, k=8)14 15 # 3 · ground the LLM in the retrieved context16 return await llm.answer(query, context)17 18 19def classify(self, q: str) -> Backend:20 if self.is_time_sensitive(q):21 return Backend.TAVILY # live web search22 if self.is_keyword_like(q):23 return Backend.BM25 # cheapest path24 if self.is_mixed_intent(q):25 return Backend.HYBRID # RRF fusion26 return Backend.DENSE # semantic defaultThe bottom line
Same answers. A fraction of the bill.
100% Python · open source · reproducible benchmark harness included
Ready to optimize your RAG pipeline?
Star the repo, run the benchmark on your own query set, or reach out to talk through routing for production RAG.