RAG Cost Drivers: Chunk Size, Embeddings, and Reranking
How chunk size, embedding volume, and reranking interact to drive RAG costs, and how to measure cost per answered query.
The four cost centers of a RAG request
A RAG request incurs costs at multiple stages: embedding the query, retrieving relevant chunks from an index, optionally reranking them, and generating the final answer with an LLM. Each stage has its own cost model. Embedding costs depend on the number of tokens embedded, which is influenced by chunk size and the number of chunks. Index storage costs scale with the total number of embeddings and their dimensionality. Retrieval costs are often tied to the number of chunks retrieved (fan-out) and the complexity of the search. Reranking costs are proportional to the number of candidate chunks and their token lengths. Finally, generation costs depend on the size of the context window, which is affected by how many chunks are included and their sizes.
Overlooking any of these centers can lead to unexpected expenses. For example, optimizing only for generation costs by reducing context size might require retrieving more chunks or using larger chunks, which shifts costs to embedding and storage. A holistic view is essential.
Chunk size tradeoffs
Chunk size is a critical parameter with non-obvious cost implications. Smaller chunks (e.g., 128 tokens) increase the total number of chunks for a given document corpus, leading to more embeddings to compute and store. They also tend to increase retrieval fan-out because more chunks are needed to cover the same information, which can raise reranking and generation costs. However, smaller chunks can improve retrieval precision, potentially reducing the number of chunks needed in the context and lowering generation costs. Larger chunks (e.g., 512 tokens) reduce the number of embeddings and storage costs but may decrease precision, requiring more chunks to be retrieved and increasing context size.
The optimal chunk size depends on the document structure, query types, and the relative costs of embedding, storage, retrieval, and generation. There is no one-size-fits-all answer. It's a tradeoff between precision and volume. Smaller chunks are not automatically cheaper; they can increase costs elsewhere.
Reranking break-even
Reranking introduces an additional cost per query: each candidate chunk must be scored, typically using a cross-encoder or LLM-based reranker, which consumes tokens. The benefit is improved precision, allowing you to include fewer chunks in the final context and reduce generation costs. The break-even point depends on the cost of reranking versus the savings from a smaller context. If reranking allows you to cut the number of context chunks in half, the savings in generation tokens might offset the reranking cost, especially for large language models with high per-token prices.
To decide if reranking pays off, measure the average number of chunks needed to achieve a certain answer quality with and without reranking, and calculate the token cost difference. Also consider latency and infrastructure overhead. In some cases, reranking may not be cost-effective if the initial retrieval is already precise or if the reranker is expensive relative to the generator.
Instrumenting cost per answered query
- Define a representative set of queries and expected answers to evaluate quality and cost.
- For each query, log the number of tokens embedded for the query, the number of chunks retrieved, the number of tokens used in reranking (if any), and the number of tokens in the final context and generation.
- Assign unit costs to each token type based on your provider's pricing (embedding, reranking, generation) and storage costs per embedding per month.
- Calculate the total cost per query by summing the costs of each stage.
- Aggregate costs across queries to get an average cost per answered query, and break down by stage to identify the largest cost drivers.
- Experiment with different chunk sizes and reranking settings, re-measuring cost and quality to find the optimal configuration.
Failure modes and pitfalls
- Assuming smaller chunks reduce costs: they can increase embedding volume and retrieval fan-out, raising costs.
- Ignoring storage costs: embeddings for large corpora can accumulate significant storage expenses.
- Overlooking reranking token costs: rerankers can be expensive, especially if applied to many candidates.
- Using a single chunk size for all document types: heterogeneous documents may benefit from adaptive chunking.
- Failing to monitor cost per query over time: costs can drift as data grows or query patterns change.
How to apply this with Admesh
Admesh provides a unified API for multiple model families, making it easier to experiment with different embedding and generation models without managing separate accounts. Use the OpenAI-compatible base URL https://api.admesh.ai/v1 for embeddings and chat completions, and the Anthropic-compatible endpoint https://api.admesh.ai/v1/messages for Claude models. With one API key and one balance, you can compare costs across providers.
To optimize RAG costs, start by instrumenting your pipeline as described above. Use Admesh's console at https://console.admesh.ai to monitor usage and costs per model. Check the model catalog at https://admesh.ai/models and pricing at https://admesh.ai/pricing to select cost-effective models for embedding, reranking, and generation. For implementation details, refer to the docs at https://docs.admesh.ai.
Run A/B tests with different chunk sizes and reranking strategies, measuring cost per answered query for each configuration. Admesh's unified billing simplifies cost tracking across models, allowing you to focus on finding the optimal balance between quality and cost.
Частые вопросы
Are smaller chunks always cheaper for RAG?
When does reranking pay for itself?
How can I measure cost per answered query?
What tools can help track RAG costs?
Источники
Вызывайте эти модели через один шлюз
Admesh предоставляет единый API, совместимый с OpenAI: один Base URL и один ключ для Claude, GPT, DeepSeek и других моделей. Новым аккаунтам доступен пробный кредит без срока действия.
