AI features are cheap to prototype and can be expensive to run. The gap between the two surprises teams, usually in the second month.
The costs are controllable, and the techniques are not complicated. They just have to be applied deliberately, because the default implementation — send everything to the best model — is also the most expensive one.
Understand what drives cost
Billing is by tokens, input and output, with input usually cheaper. That means:
Context length is the main driver. The prompt is charged on every call. A system prompt of 2,000 tokens sent on every one of 50,000 monthly requests is 100 million input tokens before any user content.
Output length matters less but is priced higher. Asking for a summary rather than a full rewrite cuts the expensive half.
Model choice is the multiplier. The gap between the smallest and largest models in a family is typically an order of magnitude or more per token.
Everything below follows from those three facts.
Cache aggressively
The largest saving in most workflows, and the most often skipped.
Exact-match caching. The same question with the same context produces the same answer. Hash the input, store the output, serve from cache. In support bots and classification workflows, repetition is high and this alone can remove a large share of calls.
Semantic caching. Cache on meaning rather than exact string, so "what are your opening hours" and "when are you open" hit the same entry. More complex, more effective in customer-facing use.
Prompt caching. Most providers now offer caching of a stable prompt prefix at reduced cost. If you have a long system prompt or a fixed document, structure the request so the stable part comes first and can be cached.
Set a sensible expiry. Cached answers to questions about prices or availability go stale.
Route by difficulty
Most steps in most workflows do not need the most capable model.
A realistic routing scheme:
- Classification, extraction, routing, simple formatting → smallest model
- Drafting, summarising, standard Q&A → mid-tier model
- Complex reasoning, long documents, multi-step planning → frontier model
Implementing this requires knowing which step is which, which requires thinking about your workflow — the actual work here is analysis rather than engineering.
A useful pattern is escalation: try the small model, check the output against a validity condition, and escalate to a larger one only if it fails. For tasks where the small model succeeds most of the time, this captures most of the saving while keeping quality.
Trim the context
The highest-leverage single change in retrieval-based systems.
Retrieve, do not stuff. Sending an entire document when three paragraphs answer the question multiplies cost on every call. Proper retrieval with re-ranking sends only what is needed.
Tune the number of retrieved chunks. Many systems default to retrieving ten passages when four would do. Measure quality against the count and use the smallest that holds up.
Trim conversation history. A long chat does not need every previous turn resent. Summarise older turns, or keep a rolling window.
Shorten the system prompt. It is charged on every call. Prompts accumulate instructions over time; review and prune periodically.
Cap and quota
Protection against the bug rather than the normal case.
Max tokens per request, on both input and output. An unbounded request can consume an enormous amount in one call.
Per-user quotas, so a single account cannot consume the budget — whether through enthusiasm or abuse.
Per-workflow rate limits, so a loop cannot run away.
A hard monthly ceiling that stops rather than merely alerts. Alerting fires while the spend continues; a ceiling is the actual control.
Every one of these exists because somebody's agent entered a loop at 2am.
Batch where latency does not matter
Providers typically offer substantially reduced pricing for batch processing with a delayed response.
For anything not interactive — overnight document processing, bulk classification, content generation, enrichment of a lead list — this is a significant saving for no quality cost. The only requirement is that you can wait.
Review your workflows for anything that could run on a schedule rather than on demand.
Measure cost per task
Not cost per month. Cost per completed task is the number that lets you decide anything.
Instrument the workflow to record tokens and cost per run, tagged by workflow and step. Then you can see:
- Which workflow consumes most of the budget
- Which step inside it is the expensive one
- Whether a change improved or worsened the economics
- Whether the task is worth automating at all, compared to its human cost
Without this, cost reduction is guesswork. With it, the expensive step is usually obvious and frequently easy to fix.
Monitor and alert
Daily spend tracking, broken down by workflow.
Anomaly alerting — spend significantly above the recent average should notify someone the same day, not at month end.
Token-per-request distribution. A rising average means context is creeping, usually because a prompt grew or retrieval started returning more.
When optimisation is not worth it
A caveat that matters.
If your AI spend is a small line item relative to the value it produces, engineering effort spent reducing it is effort not spent on the product. A workflow costing a few dollars a day that saves hours of staff time does not need optimising.
Apply this work when spend is material, when it is growing faster than usage, or when the unit economics of a feature do not work. Otherwise, set the caps and quotas as protection and move on.
A sensible default setup
For any production AI feature:
- Caps on input and output tokens per request
- Per-user quotas and a hard monthly ceiling
- Exact-match caching on repeated requests
- Model routing, with the cheapest model that passes your evaluation
- Retrieval rather than context stuffing
- Cost per task instrumented and visible
- Daily spend alerting
That is a day of work and it prevents the two bad outcomes: a surprise invoice, and a feature whose economics quietly do not work.
If you have an AI feature in production and want its costs analysed, get in touch — the first pass usually finds something obvious.
