LLMOpsThe LLMOps lifecycle · MLflow 3 · FastAPI · Docker · AWS/Azure
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
41 small wins to finish your pathNext lesson →

Controlling the cost of API calls

Controlling API cost is keeping the bill from an LLM app down by spending fewer tokens and making fewer calls: a cache for repeats, a cap on output length, a smaller model where it suffices, and a rate limit on the whole app.

Last updated: 15 Oct, 2026 · LLMOps

The Serving an API and scaling it lesson grew the endpoint to serve more traffic, and more traffic is more cost. The Detailed LLMOps Project Lifecycle video singles this out as one important thing, how do we optimize the API call cost, because LLM model API calls will be happening on every request. A paid model charges per token in and per token out, and a self-hosted one bills by the GPU hour, so both reward the same habits. This lesson is the set of levers that lower the bill.

The levers, and what each one saves

Cost on a paid model is tokens multiplied by calls, so every lever either cuts the tokens a call spends or cuts how many calls happen. They stack: a cache removes calls, a smaller model lowers the price of the ones that remain, and a token cap bounds the worst case of each.

LeverWhat it doesEffect on cost
Cache repeatsReturn a stored answer for an identical promptRemoves the call and its tokens entirely
Cap max_tokensLimit the length of the model's outputBounds the priciest half of each call
Trim the promptSend less context and fewer examplesFewer input tokens on every call
A smaller modelRoute easy requests to a cheaper modelLower price per token for most traffic
Rate limitCap calls per user or per minuteStops a loop or abuse running up a bill

Prompt size is a cost you pay every call

The input tokens are easy to forget because they are not the answer, but a long system prompt and a pile of retrieved context are sent on every single request and charged every time. Retrieving four tight chunks instead of twenty, and keeping the system prompt short, lowers the floor cost of every call the app ever makes.

Output is the dearer half

On most paid models an output token costs more than an input token, and output is the part that runs unbounded if you let it. A max_tokens cap is the single cheapest guard: it puts a ceiling on the most expensive part of each call and stops a model that decides to write an essay from billing like one.

One model call drawn as input tokens, the prompt and context you send, followed by output tokens, what the model writes back and the dearer half, with a note that this is what you are billed for. Three levers point up at the part they act on: caching repeats, in green, removes the call and its tokens entirely; trimming the prompt means fewer input tokens on every call; and capping max_tokens bounds the priciest half of each call. A panel underneath adds two levers that act on the whole app rather than one call: a smaller model lowers the price per token for most traffic, and a rate limit caps calls per user so a loop cannot run up a bill.

Where cost control matters

  • A high-traffic app, where a cache on common questions removes a large share of calls before they cost anything.
  • A retrieval app, where trimming how much context is sent cuts input tokens on every request at once.
  • A mixed workload, where routing simple requests to a smaller model keeps the dear model for the hard ones.
Watch out. A cheaper model or a shorter prompt saves money only while the answers stay good enough, and the way to know is the evaluation set from the tracking part, not a hunch. Cut a lever, then re-run the evaluation before shipping it, so a cost saving does not quietly become a quality drop no one measured.
Try it yourself
  • Add a dict cache keyed on the prompt to an app you run, and count the calls it removes over an hour of repeats.
  • Set a max_tokens cap and watch the cost of a long answer stop growing at the ceiling.
  • Measure the input tokens of your system prompt plus retrieved context, and work out what that floor costs per thousand calls.

You understood something today that you didn't yesterday.