Cost-Effective RAG at Scale
Implementing Semantic Caching and Hybrid Search to Reduce API Costs by 60% Without Sacrificing Retrieval Accuracy
Retrieval-Augmented Generation is no longer an experimental pattern. It is the dominant architecture for production LLM systems that require grounded, up-to-date, or domain-specific responses. But as RAG workloads mature from prototypes into systems handling millions of queries per day, a new problem emerges: cost. Specifically, the cost of embedding generation, vector search, and LLM inference adds up in ways that catch most engineering teams off guard at scale.
At Psivex Labs, we have run RAG infrastructure across several enterprise deployments. What we have learned is that the path to cost efficiency is not about compromising on quality — it is about being precise about what work actually needs to be done for a given query. This article documents two specific techniques — semantic caching and hybrid search — that, when combined, have consistently delivered 55–65% reductions in API spend without measurable degradation in retrieval accuracy.
The Cost Anatomy of a RAG Pipeline
Before optimizing, it helps to be precise about where money is actually going. A standard RAG pipeline has three cost-generating steps:
Embedding generation — every query must be embedded before retrieval. If you are using a hosted embedding model, each call costs tokens. At high query volumes, this is non-trivial.
Vector search — depending on your vector database, you pay per query, per stored vector, or both. At tens of millions of vectors and thousands of queries per second, the costs compound.
LLM inference — the final generation step is almost always the largest single cost. A RAG response typically consumes 2,000–8,000 tokens when you include the retrieved context window. This is 5–10x more expensive per query than a simple chat completion.
The insight that drives both optimizations in this article is that a large fraction of production queries are semantically redundant. Users ask variations of the same questions. Support systems receive the same intents phrased differently. Knowledge base lookups cluster around a small set of high-frequency concepts. When you measure this empirically, the numbers are striking: in enterprise deployments we have analyzed, 40–60% of queries are semantically equivalent to a query answered within the last 24 hours.
That redundancy is waste. The goal is to eliminate it precisely and safely.
Technique 1: Semantic Caching
Traditional caching is exact-match: the same string maps to the same cached response. This works well for deterministic systems but fails almost completely for natural language queries. "What is the refund policy?" and "How do I get a refund?" are different strings that map to identical retrieval results and nearly identical responses. An exact-match cache will miss both unless the user types the question character-for-character.
Semantic caching solves this by caching on meaning rather than text. The implementation has four components.
Architecture
When a query arrives, you embed it. Before executing the full RAG pipeline, you search the semantic cache — a separate, low-latency vector store — for embeddings that are similar above a threshold. If a match is found, you return the cached response. If not, you run the full pipeline, store the result in the cache, and return it.
Query → Embed → Cache Lookup (vector similarity)
↓ hit ↓ miss
Return cached response Full RAG Pipeline
↓
Store in cache
↓
Return response
Threshold Calibration
The threshold is the most consequential parameter. Set it too high (e.g., cosine similarity > 0.98) and you get almost no cache hits — you have only avoided the exact duplicate case. Set it too low (e.g., > 0.80) and you start returning stale or semantically mismatched responses, which is worse than no cache at all.
In our deployments, the sweet spot consistently falls between 0.91 and 0.94 cosine similarity, depending on domain specificity. Technical documentation domains (where precise terminology matters) use 0.93–0.94. General knowledge domains tolerate 0.91–0.92.
The correct way to calibrate this is to run offline evaluation: take a sample of production queries, compute pairwise similarities for known-equivalent and known-different questions, and find the threshold that maximizes true positives while keeping false positives below 1%.
Cache TTL Strategy
Not all cached entries should expire at the same rate. A response grounded in your company's pricing page should expire when that page changes — ideally via a document change webhook rather than a fixed TTL. A response about a stable technical concept (how does TCP handshake work) can be cached for weeks.
What to Cache — and What Not To
Semantic caching works for queries where the correct answer is stable and context-independent. It breaks for queries that are inherently personal ("What is my account balance?"), time-sensitive ("What is happening right now?"), or session-dependent ("Based on what we just discussed, summarize the key points"). These should always bypass the cache entirely. Add a classifier at query intake to route these categories directly to the full pipeline.
Technique 2: Hybrid Search
Pure vector search — embedding the query and finding nearest neighbours in embedding space — is elegant and generalizes well. But it has a well-documented failure mode: it struggles with queries containing specific identifiers that are semantically opaque. Model version numbers, product SKUs, proper nouns, technical acronyms, and error codes all perform poorly in pure vector search because their embeddings do not capture their specificity.
If a user asks "What changed in firmware version 3.14.2-rc7?", a vector search will return documents semantically related to firmware changelogs — which might be pages about version 3.14.1, 3.14.0, or even unrelated firmware entirely. The specific version string is what matters, and vector similarity cannot distinguish it.
Hybrid search combines vector retrieval with BM25 sparse retrieval, resolving this failure mode.
BM25 and Dense Retrieval: Complementary Strengths
BM25 (Best Match 25) is a classical term-frequency/inverse-document-frequency ranking algorithm. It excels at exact and near-exact keyword matching. It fails at semantic generalization — it cannot understand that "automobile" and "car" are the same concept.
Dense vector retrieval does the opposite: it excels at semantic generalization but fails at precise keyword discrimination.
Combining them yields a retriever that handles both broad semantic queries and specific keyword-anchored queries accurately.
Reciprocal Rank Fusion
The practical challenge of hybrid search is combining scores from two different ranking systems. BM25 scores are not directly comparable to cosine similarities. Reciprocal Rank Fusion (RRF) sidesteps this problem by combining ranks rather than scores:
RRF_score(doc) = Σ 1 / (k + rank_in_system_i)
Where `k` is a smoothing constant (typically 60) and the sum is taken across both the BM25 ranking and the vector search ranking. This is both simple to implement and remarkably robust — it outperforms learned fusion weights in most benchmarks unless you invest heavily in training data for the fusion model.
Accuracy Impact
Across five production deployments where we introduced hybrid search to replace pure vector retrieval, Mean Reciprocal Rank at top-5 improved by an average of 14 percentage points. The improvement is most pronounced for:
- Queries with product identifiers or version numbers (+31% MRR)
- Queries with rare domain-specific acronyms (+22% MRR)
- Short queries with one or two precise keywords (+18% MRR)
For general conversational queries, the improvement is modest (3–5%) but never negative — hybrid search does not hurt retrieval quality even when the BM25 component contributes minimally.
Combining the Two: What the Numbers Look Like
Semantic caching and hybrid search are independent optimizations that compound when deployed together. Here is a representative profile from a production enterprise knowledge base deployment (legal domain, ~2M documents, ~50K queries/day):
Net monthly saving: ~$3,600 on this single deployment.
The cost reduction and quality improvement are not in tension — they come from eliminating redundant work and improving the precision of the work that remains.
Operational Considerations
Cache Poisoning Risk
If a query is answered incorrectly and cached, that incorrect answer will be served to all semantically similar future queries until the entry expires or is invalidated. This is the primary risk of semantic caching.
Mitigation: implement a feedback loop. If users rate a response as incorrect, immediately invalidate that cache entry. Run periodic offline evaluations against ground truth to detect degraded cached entries proactively.
Embedding Model Consistency
Your semantic cache is only valid for embeddings generated by the same model at the same version. When you upgrade your embedding model, all cached entries are invalid — their similarity scores are no longer meaningful relative to newly generated query embeddings. Plan for full cache flush on embedding model upgrades and avoid embedding model changes mid-deployment unless you have a migration strategy.
Conclusion
The cost of running RAG at scale is not inevitable — it is mostly a function of how much redundant work your system does. Semantic caching eliminates the redundancy in queries. Hybrid search eliminates the redundancy in retrieval — fetching fewer but more precisely relevant documents, which in turn reduces the context window fed to the LLM.
Together, these techniques have consistently delivered 55–65% cost reductions across production deployments at Psivex Labs, with either neutral or positive impact on retrieval accuracy. The implementation investment is moderate — a week of engineering time for a team already running a RAG system — and the payoff at scale is significant.
The infrastructure patterns described here are production-tested and applicable across the major vector database stacks. For teams scaling past 10K daily queries, both optimizations should be considered standard practice rather than optional enhancements.