How to Cut AI API Costs Without Losing Quality
A practical playbook for reducing LLM spend — caching, prompt trimming, model routing, batching, and smarter architecture — with a measurement loop to prove you didn't hurt quality.
Where the Money Actually Goes
Most AI bills come from inference, not training. And within inference, three things dominate: the size of the model, the size of the context, and the number of calls. Agentic and RAG systems multiply all three — a single user request can trigger a dozen model calls, each carrying retrieved documents and conversation history.
That gives you a simple cost equation:
cost ≈ calls × tokens per call × price per token
Every optimisation below attacks one of those three factors. Start by measuring which one dominates in your workload.
Step 0: Instrument Before You Optimise
You cannot cut what you cannot see. Log per request:
- Model used, input tokens, output tokens, cached tokens.
- Number of model and tool calls per user action.
- Latency and success/failure.
- A user or request ID so you can trace a bill line back to a feature.
Group costs by feature, not just by model. Teams routinely discover that one background job — a nightly re-summarisation, a verbose agent loop — accounts for most of the spend.
1. Cache Everything You Can
Caching is usually the highest-return, lowest-risk change.
- Exact-match cache: hash the prompt and parameters; serve stored responses for repeated queries. FAQ-style workloads often see 20–40% hit rates.
- Semantic cache: embed the query and return a cached answer for sufficiently similar questions. Cheaper models can generate the embedding; set the similarity threshold conservatively and test for wrong-answer regressions.
- Provider prompt caching: many APIs discount repeated prefixes (system prompts, tool definitions, long documents). Structure prompts so the stable part comes first, and reuse the same prefix across calls.
- Application-level memoisation: don’t re-summarise a document that hasn’t changed; store derived artifacts keyed by content hash.
Even a modest cache hit rate compounds: cached responses are near-free and faster.
2. Shrink the Prompt
Long prompts cost money on every single call. Common waste:
- Full conversation history where only the last few turns matter. Summarise or drop older turns.
- Retrieving ten documents when three suffice. Tune the retrieval cutoff; measure quality at k=2, 3, 5, 10.
- Duplicated instructions and examples across system and user messages.
- Boilerplate tool schemas for tools not relevant to the request. Load tools per intent instead of all at once.
A useful discipline: for each component of the prompt, ask “what breaks if this is removed?” Delete what doesn’t.
3. Route to the Right Model
Not every call needs the frontier model. A routing layer assigns requests to models by difficulty:
- Rules first: short classification, extraction with a fixed schema, and template-based tasks can go to small models outright.
- Cascade: try a cheap model; if a validator, confidence signal, or rubric score fails, escalate to a stronger one. For code, run the unit tests — they are free verification.
- Tiered features: use strong models where the user is waiting and quality is visible; use cheap models for background enrichment and internal tooling.
Frontier models can cost 10–30× more per token than small ones. If half your traffic is easy, routing is often the single biggest saving — provided you measure quality on the cheap path.
4. Batch and Defer
- Batch APIs offer substantial discounts for non-urgent work: nightly re-indexing, bulk classification, backfills.
- Debounce and coalesce: if three events trigger the same analysis, run it once.
- Stream and stop early: streamed responses can be cut off when enough information has been produced, rather than generating to a fixed max.
- Async pipelines: move heavy jobs off the request path so you can use cheaper, slower options.
5. Control Output Tokens
Output tokens are typically 2–4× the price of input tokens.
- Cap
max_tokensper call type instead of setting one generous global limit. - Ask for the minimum format: structured JSON instead of prose when a downstream system consumes it.
- For reasoning models, budget thinking deliberately — disabling or limiting hidden reasoning for easy tasks is a legitimate lever.
- Stop generating when the answer is complete: stop sequences for templates, and validators that terminate as soon as the schema is satisfied.
6. Engineer the Architecture
Sometimes the fix is one level up:
- Precompute what can be precomputed at publish time instead of query time.
- Use non-LLM tools where they beat models: regex, keyword search, deterministic rules, and classical ML are free at the margin.
- Reduce agent iterations: constrain loops with step budgets, better tools, and clearer stopping conditions. Most runaway agent bills come from loops that never converge.
- Right-size retrieval: a well-tuned BM25 or hybrid search can outperform an expensive embedding pipeline at a fraction of the cost.
Step 7: Prove You Didn’t Break Quality
Every saving must be checked against a quality bar. Keep a small evaluation set (see our guide on evaluating AI models) and run it before and after each change:
- Task success rate and schema validity.
- Refusal and error rates.
- User-facing metrics: thumbs-down rate, escalation rate, edit distance on accepted outputs.
- Cost per successful task — the number that actually matters.
A change that halves spend and drops success by 15% is usually a bad trade. A change that halves spend with no measurable quality loss is free money.
A Realistic Order of Operations
- Instrument cost per feature.
- Add caching (exact, then semantic).
- Trim prompts and context; tune retrieval k.
- Route easy traffic to small models with a validator.
- Move background work to batch APIs.
- Cap and shape outputs.
- Re-measure quality on the eval set.
The Bottom Line
AI cost work is mostly ordinary engineering discipline: measure, remove waste, and verify. The teams with the lowest bills are rarely using exotic tricks — they know exactly which feature spends the money, they cache what repeats, they route what’s easy, and they never let an agent loop run without a budget.
The sharpest AI news, curated daily.
One email each morning. Summarized, sourced, and skimmable in under three minutes.