How to keep LLM costs under control in production

Seven levers that cut the cost of AI features without hurting quality, plus the limits every SaaS should set before launch.

Samed Škulj

Samed Škulj
· 3 min read

AI features have a cost profile most SaaS teams are not used to: every click costs money, and a single heavy user or a bug in a retry loop can produce a bill nobody planned for. The good news is that most features can run at a fraction of their first-version cost without users noticing.

Measure before you optimise

Log, for every call: feature name, workspace, model, input tokens, output tokens, latency, and whether the output was used. Then answer:

  • What does one use of the feature cost on average, and at the 95th percentile?
  • Which feature, and which customers, drive most of the spend?
  • How much of the input is the same on every call?

The answers decide which of the levers below matter for you.

1. Use the smallest model that passes your evals

Teams often start on the largest model and never revisit. Run your eval suite against a smaller, cheaper model. For classification, extraction and short drafts, smaller models often score within a few points of large ones at a fraction of the price.

You can also route: a small model handles most requests, and only hard ones (long input, low confidence, a failed validation) go to the large model.

2. Send less context

Input tokens are usually the bigger part of the bill. Common waste:

  • Sending a whole document when only two sections matter.
  • Sending the full conversation history on every turn.
  • Long system prompts full of instructions that no longer change behaviour.

Retrieve only the relevant chunks, summarise older history, and prune prompts with your evals as a safety net.

3. Cache what repeats

Two kinds of caching help:

  • Prompt caching from the provider: if every call starts with the same long system prompt or the same document, the repeated prefix is billed at a much lower rate. Put stable content first and variable content last to get the most from it.
  • Response caching in your app: if the same input produces the same output (a summary of a document that hasn't changed), store it and skip the call.

4. Cap output length

Ask for exactly the format you need and set a maximum output length. A JSON object with five fields is cheaper and easier to validate than three paragraphs.

5. Batch work that isn't interactive

Nightly tagging, backfills and bulk summaries don't need an instant answer. Batch APIs are cheaper and avoid rate limits during the day.

6. Don't call the model when you don't need to

Simple rules catch a lot: skip empty inputs, skip records that haven't changed, use a regex for the cases a regex handles. Only call the model for what needs it.

7. Stop runaway loops

Agents and retry logic are the usual source of surprise bills. Set a hard limit on steps per task, retries per call and tokens per request.

Limits to set before launch

Every AI feature should ship with:

Limit Why
Per-user daily cap Stops one heavy user or script from dominating spend
Per-workspace monthly budget Aligns cost with what the plan pays
Max tokens per request Stops one huge input from costing a fortune
Alert on daily spend You hear about a bug before the invoice does

Tie the caps to your pricing. If AI usage is part of a paid plan, the plan's price should cover the heaviest realistic user with margin.

The order to work in

  1. Logging and limits (protects you now).
  2. Smaller model plus evals (usually the biggest saving).
  3. Less context and caching.
  4. Batching and skipping calls.

Cost limits and logging are part of every AI feature build. If you already have a feature in production and the bill is growing faster than usage, a performance and code audit covers the AI calls too.

Written by Samed Škulj

Senior Full-Stack Engineer since 2021. I have shipped with teams at Deel, HTEC Group (for Zoox), Ministry of Programming, and now help SaaS teams add AI features and get AI-built apps ready for production. LinkedIn