SabaKore

Home / Articles / AI guides

Preventing AI API Costs from Growing Beyond What You Expected

The typical causes of ballooning costs and the safeguards to put on the server side. It also covers how to estimate once before you start, and a setup that lets you find out sooner than the bill does.

Ren, a junior infrastructure engineer

Ren

My AI API bill is way higher than I thought...!
Procura, a cloud AI robot who explains

Procura

That happens a lot. Pay-as-you-go keeps charging as long as things run. And if you put it on a server that runs 24 hours a day, charges keep accumulating while you sleep.

Typical causes of ballooning costs

Most cost spikes come from the number of calls, not the price per call. A button a person presses has a natural cap on count, but when a program calls the API, there's no brake. If you build it to retry automatically on failure, calls increase most when things aren't going well. If you count "how many times it could be called in the worst case" at design time, you won't be surprised later.

  • Sending the entire conversation history every time (the cost per call grows as the exchange continues)
  • Automatically retrying on errors, and that repeating
  • Calling the API endlessly in an infinite loop (the costliest kind of accident)
  • Passing long text whole (often passing only the necessary part is enough)
  • Leaving test code running in production

Safeguards to put on the server side

Procura, a cloud AI robot who explains

Procura

The API provider also has limit settings, but that's the last line of defense. Build your own safeguards before it.
  • Set a cap on calls per day and stop when it's exceeded
  • Always limit the number of retries (unlimited retries invite accidents)
  • Summarize the old part of the conversation history, or cut it off at a certain amount
  • Just returning the same answer to the same question (caching) can cut a lot
  • Log usage so you can view it day by day
Ren, a junior infrastructure engineer

Ren

What do you do when you hit the cap? Does it give an error?
Procura, a cloud AI robot who explains

Procura

That depends on the use. The options include stopping and saying "We've stopped accepting requests for today", switching to a cheaper model, or asking people to wait. What matters is not to keep charging silently.
Wilson, the security guard

Wilson

...Let me add something. If you expose an AI feature without authentication, I'll use it all up for you. The bill is addressed to you, though. Setting a cap is also preparation against me.
Procura, a cloud AI robot who explains

Procura

Right. If you put AI on a feature you make public, think of authentication and rate limiting as a pair.

Estimate before you use it

Procura, a cloud AI robot who explains

Procura

Before you start building, measure once how much one request costs. Multiply it by the expected number of requests and you get a rough monthly figure. If you skip this, you find out the amount only after you've gone public.
  • Measure the actual cost of one request
  • Multiply by the expected number of requests per day
  • Multiply that by 30 to get the approximate monthly amount
  • If the amount is unacceptable, review the design before going public

Use different models for different jobs

Procura, a cloud AI robot who explains

Procura

Not every process needs the most capable model. Simple processing like classification or judgment is often fine with a lightweight model. If you build it to send only hard cases to a higher-tier model, the total cost changes quite a lot.

Make it something you can notice

Finding out for the first time from the bill at the end of the month is the costliest way. Many providers let you set usage notifications, so set those first. If you set it to stop when a limit is reached, you can prevent costs from growing while you sleep.

  • In the provider's control panel, set a usage limit and notifications
  • Also make daily call counts visible in your own logs
  • Add a mechanism that lets you notice a sudden increase
  • Separate the key for testing from the key for production (so you can tell which one is being used)
Procura, a cloud AI robot who explains

Procura

Noticing from the bill is the slowest. If you can view things day by day, the scale of an accident stops at one day's worth.
Ren, a junior infrastructure engineer

Ren

Measure first, think about whether a lighter model is enough, and look day by day. Authentication and rate limits go as a pair.

This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.