WEBSITES · APPS · AI AGENTSSEO · AEO · GEO · GOOGLE BUSINESS PROFILESOCIAL MEDIA · GOOGLE & META ADSWHATSAPP AUTO-REPLY IN UNDER 60 SECONDSCLINICS · SCHOOLS · JEWELLERS · REAL ESTATE · 40 TRADESJAWAB, OUR WHATSAPP CRMFREE AUDIT, ALWAYSDELHI · PAN-INDIAWEBSITES · APPS · AI AGENTSSEO · AEO · GEO · GOOGLE BUSINESS PROFILESOCIAL MEDIA · GOOGLE & META ADSWHATSAPP AUTO-REPLY IN UNDER 60 SECONDSCLINICS · SCHOOLS · JEWELLERS · REAL ESTATE · 40 TRADESJAWAB, OUR WHATSAPP CRMFREE AUDIT, ALWAYSDELHI · PAN-INDIA
Guide · AI engineering

Keeping RAG costs under control in production

Without a ceiling, every cost incident is an emergency. The fix is not hoping people are careful — it is wiring the limit into the request path.

In short

On a typical retrieval-augmented request, the generation call is almost the entire bill, and output tokens cost several times more than input tokens. Cost incidents come from three patterns: over-long contexts, runaway agent loops, and tail-of-distribution requests. The controls that actually move the number are a hard per-request token cap, a per-user daily cap, routing easy requests to a smaller model, prompt caching, an embedding cache, a retrieval ceiling, streaming with early termination, and a daily spend alarm.

Dominant cost
The generation call
Output vs input
Several times pricier
Incident causes
Long context, loops, tails
Controls
Eight, all in the request path

Where the money actually goes

A retrieval request pays for three things: embedding the query, which is cheap; the retrieval round trip, usually negligible; and the generation call, which is almost everything. Within generation, the bill is roughly tokens in times the input rate plus tokens out times the output rate — and output tokens are materially more expensive than input tokens on every major provider.

Teams spend most of their attention on prompt length, which is the cheaper half. Cost incidents come from three specific patterns: a context stuffed with too many chunks, an agent calling tools in a circle, and a single user arriving with a 200KB document. A ceiling has to defend against all three, and only one of them is about prompt size.

The eight controls

  1. 01

    A hard per-request token cap

    Set a maximum output length on every generation call — for cost, not only for safety. Sensible defaults are a few hundred tokens for chat, far fewer for re-ranking and classification. Add stop sequences where the model tends to trail.

  2. 02

    A per-user, per-day cap

    Track spend per user and reject once they hit the cap, with a clean message rather than an error. On multi-tenant products this single control prevents most bad weeks.

  3. 03

    A smaller-model fallback

    Route trivial requests to a cheaper model — a fast classifier or even a regex on the query is enough to decide. Reserve the flagship model for genuinely hard requests. Getting most traffic onto the cheap path is the largest single saving available.

  4. 04

    Prompt caching

    If your system prompt or knowledge-base prefix runs past roughly a thousand tokens, use the provider's prompt caching. The first call costs slightly more; every subsequent cache hit is dramatically cheaper on the input side. For an assistant with long instructions this is free money.

  5. 05

    An embedding cache

    Do not embed the same query twice. Hash the normalised query and cache the embedding with a short TTL. On B2B tools where people re-search the same things, this halves embedding spend.

  6. 06

    A retrieval ceiling

    Cap top-k, cap chunk size, and re-rank before stuffing chunks into the prompt. Retrieve wide, re-rank down, send few. More context is not reliably better — past a point the model just gets confused.

  7. 07

    Streaming with early termination

    Stream, and abort when the user navigates away or the parser already has what it needs. A surprising share of a bill is completions nobody ever read.

  8. 08

    A daily spend dashboard and alarm

    Chart usage daily by feature, model and tenant, and alarm at 1.5× the rolling seven-day average. The alarm catches what the ceilings cannot — such as a new feature quietly running an eval loop in production.

Diagnosing a spike

Work top down. Is one user or tenant the cause? Is one feature the cause? Is one model the cause? It is almost always a single feature on a single model that tripled in volume, not a uniform rise across the system.

That matters because it makes the fix local. Teams that respond to a spike with a global prompt-shaving exercise usually degrade quality everywhere and fix nothing.

Two things we do not do

We do not self-host open models to save money. The operational overhead — GPUs, scaling, evals, drift, security patching — has eaten every projected saving. Data sensitivity and latency are good reasons to self-host. Unit cost is not.

We do not obsess over shaving prompt tokens. A clear prompt costing a few hundred extra tokens per call is worth more than a cryptic one that misfires once a day and generates a support ticket.

Questions people ask

What is the biggest lever on RAG cost?

Routing easy requests to a smaller model. Most traffic in a typical system does not need the flagship model, and moving that share onto a cheaper one usually cuts the bill more than every other control combined.

Does prompt caching actually save money?

Yes, when the cached prefix is long and reused — a large system prompt or a fixed knowledge-base preamble. The first call costs a little more and every cache hit after it is far cheaper on the input side.

Should we self-host a model to cut costs?

Usually not. GPU, scaling, evaluation, drift and patching overheads tend to exceed the saving. Self-host for data sensitivity or latency, not for unit price.

How do we find what caused a cost spike?

Top down: one tenant, then one feature, then one model. Spikes are almost always a single feature on a single model rising in volume rather than a uniform increase.

Read next

Book the free audit

90 minutes. We map where your enquiries die. Yours either way.

Book it →

Ask on WhatsApp

Fastest reply. +91 89203 04337, Mon–Sat, 10am–7pm IST.

Chat now →

Prefer email?

Send your situation. We reply within one working day.