Insights & Blog

RAG Cost Optimization: Embeddings, Reranking, Caching and Token Budgets

Optimize RAG costs across embeddings, reranking, caching and token budgets. Includes a downloadable calculator with explicit assumptions and quality checks.

By Hetal KGenerative AI & Agents

RAG cost optimization: an illustrative cost per 1,000 questions split across embeddings, vector search, reranking, generation and cache misses, with levers such as re-embedding only changed chunks and token budgets.

RAG Cost Optimization: Embeddings, Reranking, Caching and Token Budgets

Reduce RAG operating costs by measuring the full request path, eliminating unnecessary processing and controlling context size while preserving retrieval quality and permissions. Embeddings, reranking, generation, cache behavior and fixed search capacity have different cost drivers. Optimize the component that your workload actually spends on rather than assuming the language model is always the only expense.

This guide includes a transparent cost model and a downloadable Python calculator. The example uses fictional rates in a consistent currency unit. It is arithmetic for planning, not a provider quote, production benchmark or promised saving.

Start with a complete RAG cost model

Separate ingestion from the query path. Ingestion can include parsing, OCR, enrichment, chunking, document embeddings and index writes. A query can include query rewriting, query embeddings, search, reranking, context construction, generation and validation. Add hosting, storage, monitoring, backup and engineering operations.

The Azure RAG design and evaluation guide treats the pipeline as several stages that need individual evaluation. Apply the same discipline to cost: instrument the stages, but judge the final experience together.

ComponentUseful measurementOptimization candidateQuality risk
Document processingChanged pages, extracted tokens and reprocessing volumeIncremental processing and deduplicationMissing updates or lost document structure
EmbeddingsEmbedded document and query tokensReuse unchanged content representationsReusing incompatible or stale embeddings
RetrievalQueries, concurrency, filters and candidate depthRight-size capacity and remove duplicate requestsLower recall or increased throttling
RerankingCalls and candidates processedApply where the measured benefit justifies costLess useful evidence reaching the answer stage
GenerationInput, output and repeated context tokensBounded evidence and task-appropriate model choiceOmitted conditions or weaker answers
OperationsSupport, review and incident effortReliable ingestion and actionable diagnosticsHidden manual work if excluded from estimates

Use a meaningful denominator

Track cost per request and cost per accepted task. A cheaper response that repeatedly fails may create more retries and specialist review. Keep the acceptance definition explicit, including answer support, permissions and usefulness. Do not claim a cost-per-success improvement without evaluating success consistently.

Downloadable calculator and declared assumptions

Download the RAG cost calculator ZIP. It contains a standard-library Python script, editable JSON assumptions and a README. It makes no API calls, requires no credentials and does not obtain current prices automatically.

python rag_cost_calculator.py rag-cost-assumptions.json

The calculator represents answer-cache hits as requests that skip query embedding, retrieval, reranking and generation. Fixed platform, cache, operations and document indexing costs remain. Prompt-token caching has different behavior and is not represented as a full answer-cache hit.

InputIllustrative valueMeaning
Monthly requests100,000All incoming application requests
Answer-cache hit fraction0.2020,000 requests skip the modeled RAG path
Monthly document tokens embedded10,000,000Changed or newly indexed content
Query / model input / output tokens30 / 2,500 / 400Per uncached request; input includes prompt and context
Embedding / input / output rates0.10 / 1 / 4 per million tokensFictional currency units, not vendor prices
Rerank fraction and per-call rate0.50 and 0.001Half of uncached requests are reranked
Variable retrieval per uncached request0.0005Include only if not already covered by fixed cost
Monthly cache / fixed platform / operations20 / 100 / 200Separate recurring assumptions

The local run calculates 80,000 RAG-path requests and a total of 729.24 fictional currency units per month. Components are document embeddings 1.00, query embeddings 0.24, model input 200, output 128, reranking 40, retrieval 40, cache 20, fixed platform 100 and operations 200. This verifies the fixture’s arithmetic only.

Replace every rate with the applicable current price and every volume with measured or clearly labeled estimated data. Add costs omitted by the simple model, such as OCR, network transfer, taxes, backups, support or implementation. Avoid counting a usage charge twice when your service bills that work through fixed capacity.

Reduce embedding and indexing cost without missing changes

Process changed content rather than blindly rebuilding the entire corpus. Use stable source identifiers and version-aware change detection. A content hash can identify unchanged text, but permission and metadata changes still need processing even when the text remains identical.

Cache embeddings only with the relevant model identity, dimensions and preprocessing version. Vectors produced by a different model are not interchangeable merely because their arrays have the same length. A model migration may require a controlled re-embedding and index transition.

Deduplicate repeated boilerplate carefully. Removing repeated navigation may help; removing a repeated safety condition from each independently retrieved procedure may harm interpretation. Evaluate representative documents, tables and exceptions before applying corpus-wide cleanup.

Microsoft’s chunking guidance discusses structure, processing and optimization choices. Our chunking comparison provides a separate boundary-focused evaluation approach.

Tune candidate depth and reranking together

Candidate depth and final context size are different knobs. Retrieve enough permitted candidates to find the evidence, then select a smaller useful context. Reducing both simultaneously makes it difficult to diagnose a quality regression.

Reranking can improve which evidence reaches the model, but adds processing. Evaluate it on the question slices where it helps rather than assuming it is either mandatory for every request or expendable everywhere. Microsoft’s retrieval guidance explains the different roles of retrieval and reranking.

Remove accidental duplicate searches, unbounded query expansion and retries that repeat successful work. Bound retry counts and distinguish transient failures from invalid requests. Do not reduce security filters to lower latency or increase cache hit rates.

Distinguish answer caching from prompt caching

An answer cache can bypass substantial work if the answer is still valid for the caller and current sources. A prompt or provider-side token cache may reduce some repeated input processing while still requiring generation and other stages. Model the documented billing behavior rather than applying one “cache discount” to the entire pipeline.

Cache eligibility depends on tenant, authorization scope, source version, policy freshness and task semantics. Identical wording does not establish identical access. A question such as “What is my remaining allowance?” is user-specific even if its text repeats.

Measure hit rates after those restrictions are applied. A high theoretical hit rate from globally matching question strings is not a safe operating assumption. Include cache storage, invalidation and lookup cost in the estimate.

Set context and output budgets by task

Measure actual tokens rather than approximating all documents by character count. Keep prompts, conversation history, retrieved passages and expected output within the chosen model’s supported limits. Reserve capacity for the answer and necessary instruction context.

Remove redundant passages and irrelevant history before cutting essential qualifications. Summarization can reduce tokens, but it adds its own processing and can omit details. Treat a generated summary as another derived artifact that needs evidence, versioning and permissions.

Use a smaller or cheaper model only after evaluating its performance on the target task. Route difficult cases through a measured fallback when appropriate, and include fallback frequency in cost estimates. A low advertised unit price can be offset by repeated calls, longer outputs or additional review.

A controlled optimization experiment

  1. Freeze a representative evaluation set and approved source snapshot.
  2. Measure baseline quality, boundary correctness, latency and stage-level cost.
  3. Change one major driver, such as context selection or rerank routing.
  4. Compare held-out results by question type, including exceptions and no-answer cases.
  5. Calculate end-to-end cost with retries, fallbacks and fixed charges included.
  6. Release gradually with monitoring and a rollback configuration.

Reject an optimization that exposes restricted material or fails the agreed quality gate. Use the RAG evaluation guide to keep retrieval recall, answer support and task correctness separate. Report trade-offs instead of hiding them behind one blended score.

Common questions

What is usually the biggest RAG cost?

It depends on workload. Generation may dominate query traffic, while parsing, enrichment and embeddings can matter during large ingestion or rebuilds. Fixed search capacity and operating effort also need accounting.

Can caching eliminate all RAG costs?

No. Even answer-cache hits require valid access and freshness checks, while storage, platform and indexing costs can remain. Prompt caching typically affects only particular parts of model processing.

Should I reduce the number of retrieved chunks?

Test candidate depth separately from final context size. Fewer candidates may reduce evidence recall, while better selection can remove irrelevant context without dropping necessary support.

Are the calculator’s example rates current vendor prices?

No. They are explicitly fictional arithmetic inputs. Replace them with applicable prices for your model, region, service and billing configuration before using the estimate for planning.

How do I know an optimization preserved quality?

Compare the changed system against the baseline on held-out questions using the same source snapshot and acceptance criteria. Include permissions, no-answer behavior, latency and human review effort alongside answer quality.

Review cost together with retrieval quality

For broader design choices, read the enterprise RAG architecture guide and RAG versus fine-tuning comparison. To assess your measured workload and optimization options, request an enterprise RAG architecture review.

From insight to implementation

Turn these ideas into a practical plan

Discuss how the ideas in this article apply to your business, systems and data. We’ll help you identify a focused first step.