Mark DeLeap King

AI and cost

Putting a ceiling on AI spend without switching features off

AI spend behaves like cloud spend did a decade ago. It grows until someone asks why the bill doubled. The controls that hold it down without removing features are a single gateway, a cap per product, separate lanes for interactive and background traffic, model routing, fallbacks and caching. At CloudVerg one gateway with a ceiling per product cut AI spend by more than 70 percent with no feature switched off.

Published · 8 min read

Why a ceiling comes before switching things off

The usual response to a surprising AI bill is to turn something off. A summary feature goes away, a nightly job is disabled, a free tier gets stricter. It works, and it costs you the features that made the product worth using.

There's a better order. Most of the spend in a young AI product is waste nobody could see: a large model answering questions a small one handles fine, the same prompt asked a thousand times, a retry loop that never stopped, a background job running at three times the rate anyone intended. Find that first. Then put a hard ceiling over whatever remains, so the next mistake has a bounded cost.

At CloudVerg, every product calls models through one gateway, with a cap per product and a ceiling for the whole fleet. Putting that in place cut AI spend by more than 70 percent, and no feature was switched off. Once each call was tied to a product, the waste was easy to see.

One gateway in front of every model

The gateway is a small service that sits between every product and the model providers. It holds the provider keys, so the products don't. It writes one line per call and enforces the caps. Everything else here hangs off it.

On day one the gateway needs to do five things.

  • Authenticate each product with its own key, so spend is attributed to a specific product.
  • Record the product, the lane, the model, the token counts in and out, the latency and the result of every call.
  • Refuse calls that would go over a cap, with a clear error the calling product can show to a person.
  • Apply routing rules, so changing a model is a configuration change and doesn't mean releasing every product.
  • Keep provider keys out of product code and out of logs.

The usual objection is that it's one more thing to run. It's a small thing, and the alternative is several products each with their own key, their own retry logic and their own idea of a reasonable cost. If more than one product or team calls models, the gateway pays for itself the first time you need to know who spent what.

Caps: per product, per fleet, set before launch

Give each product a daily cap, and give the whole fleet a ceiling under the sum of the product caps. Two levels are needed because the failure modes differ. One runaway product should hit its own cap and stop. Several products each running just under their caps should still be stopped by the fleet ceiling.

Set the number before the feature launches, from a rough estimate of use and a margin you can defend. Set afterward, it's a reaction to a bill, and it'll be wrong in one of two directions. Too high and it protects nothing. Too low and it cuts off users on a good day.

Setting a cap takes five steps.

  1. Estimate calls per day from realistic use, and leave the best case out of it.
  2. Multiply by the typical tokens per call for the model you chose, using numbers from the first week in a test environment.
  3. Add headroom for a successful day and name who gets told when the cap is reached.
  4. Decide what the product does when it's refused (queue, degrade or show a clear message), then build that path and test it.
  5. Review the cap monthly against what was actually spent.

The refusal matters as much as the number. An over-budget call should come back as a clear refusal that the product can turn into a sentence a person understands. A silent failure is worse than a clear "try again later".

Lanes: interactive first, background pauses first

Not all model traffic is equal. A person waiting for an answer in a chat window is in one situation, and a job rewriting a thousand pages overnight is in another. If both draw from the same budget at the same priority, the batch job will starve the person or the person will starve the batch job, depending on the hour.

Give each call a lane. Two are enough to start, interactive and background. Tag the lane on the request and let the gateway treat them differently.

  • Interactive traffic gets a reserved share of the budget and the fastest path.
  • Background traffic runs from what remains, and it pauses first when the provider returns a rate limit or the cap is close.
  • Background callers have to treat a pause as normal. On a 429 they wait, back off and resume later, without marking the job failed.

That last point is easy to miss. If background code treats a pause as an error, it retries harder, and you've turned a budget problem into a traffic problem. Every background engine I run sends a lane header and is written to stop and resume when told to wait.

The cheapest model that keeps the quality

This is where most of the savings are, and it needs a measurement you trust. Take a task, say classifying support requests or drafting a short description. Run it on the model you use now and on the next one down. Compare the outputs on a fixed set of examples, scored by the same rubric you use in review. If the smaller model passes, move the task to it.

Keep the routing in the gateway so the decision lives in one place. A rough rule of thumb follows.

  • Classification, extraction, routing and short rewrites go to the smallest model that passes your examples.
  • Drafting and summarizing from a clear spec go to a mid-size model.
  • Reasoning over messy input, or anything where a wrong answer is costly, goes to the strongest model, and only there.

Keep a fallback model per task. Providers have outages and rate limits, and a product that goes dark when one vendor does hasn't removed its dependency. A second provider or a second model class, tried automatically on failure, turns an incident into a brief change in tone or speed.

Re-run the comparison when a new model arrives. Prices and quality both move, and the best choice six months ago may be neither the cheapest nor the best today.

Caching, and other free savings

A lot of model traffic repeats. The same page gets summarized for every visitor, the same system prompt goes out with every call, and fifty people ask the same question in the same week. Three layers help.

  • Provider prompt caching covers the long, stable part of a prompt. Put what never changes at the start and what varies at the end.
  • A response cache in the gateway handles deterministic requests, keyed on the model, the prompt and the settings.
  • Product-level storage handles answers that will be shown many times. Generate once and store.

Smaller wins add up too. Cap the output length. Trim retrieved context to what the answer needs, and send a summary when the whole conversation isn't necessary. Put a limit on retries and loop depth in any agent that calls tools, because an agent that can't find its answer will keep trying at your expense.

Alerts and the monthly review

A cap protects you from the worst case. Alerts tell you about slow drift before the cap does, and a few are enough.

  • Spend rate for a product against its normal rate for that hour, as well as the daily total. A job that doubles its speed shows up within the hour, well before midnight.
  • A product reaching a set share of its daily cap.
  • A jump in refusals or fallbacks, which points to a bug or to growth.
  • A change in the average tokens per call for a feature, which usually means a prompt grew.

Once a month, put the gateway's own data in front of the people who run the products. Which feature costs the most per use? Which cap was hit, and was it right to hit it? Which task can move to a cheaper model this month? The conversation takes thirty minutes and keeps cost part of how the team thinks about quality.

I hold my own work to one rule: every model gets a firm spending limit before it goes live. People who use a product are better served by a feature that stays on than by one that was great for a week and then got switched off. Before the next AI feature launches, put the gateway and the cap in place. Adding them after the first surprise bill is much harder.

TopicsAI agents and AI adoption