Controlling LLM Costs: Caching, Model Routing and Token Budgets
AI features have a running cost that grows with every user. The practical levers — prompt caching, routing easy requests to smaller models, trimming context — that cut bills without cutting quality.

A feature that costs a few dollars a day in testing can cost thousands a month once real users arrive. Unlike most software, AI features have a variable cost per request, and it grows with usage. Managing that cost is part of building the feature, not an afterthought.
Know your cost per useful outcome
Track cost per call, but optimise cost per successful result: per answered question, per processed document, per completed task. A cheap model that needs retries or produces results users reject can cost more per useful outcome.
cost_per_outcome = (input_tokens * in_price + output_tokens * out_price) * calls / successesLog tokens for every request, tagged by feature, so you can see which features drive the bill.
Trim the context
Input tokens are usually the largest share of cost. Common waste:
- Sending the entire conversation history on every turn instead of a summary plus recent messages
- Retrieving 20 document chunks when 5 would do
- Long system prompts full of instructions that rarely apply
- Including full JSON records when only a few fields matter
Cutting context often improves quality too, because the model has less irrelevant material to get distracted by.
Use prompt caching
Many providers discount repeated prompt prefixes. Put stable content — system instructions, tool definitions, reference documents — at the start of the prompt, and variable content at the end. Requests that share the same prefix then reuse the cached portion at a lower price and with lower latency.
Route requests by difficulty
Not every request needs the most capable model.
| Request type | Model tier | |---|---| | Classification, extraction, short rewrites | Small, fast model | | Standard Q&A with retrieval | Mid-tier model | | Complex reasoning, long multi-step tasks | Top-tier model |
A simple router can be rule-based (by feature or input length) or use a small model to classify difficulty first. Validate routing with your eval set: confirm the cheaper tier passes for the requests you send it.
Cache responses
Identical or near-identical questions come up constantly — especially in support and documentation search. Cache answers keyed on a normalised version of the question plus the relevant document versions, with an expiry. Even a modest hit rate meaningfully reduces cost.
Cap output length
Set a sensible maximum output token limit per feature, and ask for concise answers in the prompt. Output tokens are typically priced higher than input tokens, and long answers are often worse answers.
Set budgets and alerts
- A daily spend alert per feature
- Per-user rate limits to stop runaway scripts or abuse
- A hard monthly cap that degrades gracefully — for example, switching to a smaller model — rather than failing outright
A single bug, like a retry loop or an agent that keeps calling tools, can burn a month's budget in hours. Limits turn that into a small incident instead of a large invoice.
Batch what isn't urgent
Background jobs — summarising yesterday's tickets, tagging old articles — can often use batch processing, which some providers offer at a significant discount in exchange for slower turnaround.
Review monthly
Look at cost per feature, cost per outcome, and the biggest individual requests. Prices and models change frequently; a routing decision that made sense six months ago may be outdated.
AI costs are controllable. Measure them per feature, send less context, use the smallest model that passes your evals, and put limits in place before you need them.
Paj Digital Labs
The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.
Related reading

Streaming AI Responses: Partial Text Is Not a Completed Result
Streaming improves perceived speed but adds cancellation and partial-output states. Keep tool actions, structured results and saved answers behind a verified completion boundary.

Human Review Queues for AI: Confidence Is Not a Permission Slip
A model’s self-reported confidence does not establish accuracy. Route uncertain and consequential cases using tested rules, show reviewers the evidence and record their decisions without rubber-stamping.

AI Evaluation Sets: Keep the Test Data Out of the Tuning Loop
An evaluation stops being independent when every prompt revision is shaped around its answers. Separate development examples from held-out cases and measure meaningful failure categories.