15 November 2026 · Practical AI builds
How do I set a spending limit on an AI API so the bill cannot run away?
By the end of this you will have an AI feature with several limits on what it can spend: one set with the provider, one in your own code, and an alert that tells you when spending is climbing. It suits anyone about to put a chatbot, form helper or other AI tool on a public page who wants to know the worst case before it happens.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
What you need
- An account with an AI API provider, and permission to change its billing settings.
- A separate API key for this one feature, so you can watch it and switch it off without touching anything else.
- Access to the code or settings of the app that calls the API. A small script on a server is fine.
- The provider's price page, open in a tab. Prices are per token (a token is roughly a short word or part of a word), and they differ by model.
- An email address or phone app for alerts.
Step by step
- Check how the provider bills you. Some providers use prepaid credit: you top up first and calls stop when it runs out. Others bill you afterwards for what you used. Prepaid credit with automatic top-up turned off is the simplest hard cap there is.
- Set the provider's own limit. Look in the billing or usage settings for a monthly spend limit, a budget, or a limit per project or workspace. Read the wording carefully. Some are hard limits that block calls, and some only send an email while the calls carry on.
- Give the feature its own project and key. If the provider lets you set limits per project or workspace, put this feature in its own one with a low limit. A mistake in one tool then cannot use up the budget for everything else.
- Cap the length of every reply. Most APIs let you set a maximum number of output tokens for each call. Set it to what the feature really needs, so one odd request cannot produce a huge, costly answer.
- Keep the input short. Cut long pasted text and old chat history before sending it. You pay for what goes in as well as what comes out.
- Add a daily cap in your own code. Each API reply reports how many tokens it used. Work out the cost, add it to a running total for the day, and refuse new calls once the total reaches your limit. This works whatever the provider offers.
- Limit each visitor. Allow only a set number of requests per visitor per hour, by IP address or signed-in account. This stops one person or one bot using the whole day's budget.
- Set an alert below the cap. Send yourself a message when spending passes, say, half the daily limit. You then hear about trouble while there is still budget left.
- Show a polite message when the cap is hit. Something like "This tool is resting for today, please try again tomorrow" is better than an error page.
Here is a small example of the daily cap in Python. The prices are made-up example figures, so replace them with the ones on your provider's price page.
import json, datetime
DAILY_LIMIT = 2.00 # most this feature may spend in a day
PRICE_IN = 3.00 # example price per million input tokens
PRICE_OUT = 15.00 # example price per million output tokens
LEDGER = "/var/lib/appname/spend.json"
def load():
try:
with open(LEDGER) as f:
return json.load(f)
except FileNotFoundError:
return {}
def allowed():
today = str(datetime.date.today())
return load().get(today, 0.0) < DAILY_LIMIT
def record(input_tokens, output_tokens):
today = str(datetime.date.today())
data = load()
cost = (input_tokens * PRICE_IN + output_tokens * PRICE_OUT) / 1_000_000
data[today] = data.get(today, 0.0) + cost
with open(LEDGER, "w") as f:
json.dump(data, f)Call allowed() before each API call and record() afterwards with the token counts from the reply. If allowed() returns false, show the resting message and do not call the API.
What good looks like
Test it by setting a tiny daily limit, then use the feature until it stops. You should see the resting message, no further calls in the provider's usage page, and an alert at the halfway point. Put the real limit back, and check the provider's usage figures against your own running total for the first few days. They should be close. If they are not, your prices or token counts are wrong.
Watch out for
Provider limits are not always instant. Some usage figures update with a delay, so a sudden burst can go past a limit before it takes effect. That is why the cap in your own code matters. Also keep the API key on the server, never in the web page, because anyone who copies a key from a page can spend your money outside all of these checks. If you need a refresher on keeping a small app safe on a server, see what you need to run a small AI app on a VPS.
Questions people ask
Is the provider's spend limit enough on its own?
It is a good safety net, but check whether it blocks calls or only warns you. A cap in your own code as well means you decide exactly when the feature stops.
What if a real customer hits the cap?
They see your resting message and the tool works again the next day. Use the first weeks of usage figures to set a limit that ordinary use stays well under.
A runaway AI bill is avoided with layers: prepaid credit or a provider limit, short replies, a daily cap and a per-visitor limit in your own code, and an alert that reaches you early. Test each one with a small limit before you rely on it.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.