How to control the cost of AI API calls in your app
Where AI API spending comes from, how to measure it per feature and per user, and the limits, caching, model choices and batch jobs that keep the bill in check.
Leer en español →
You control AI API costs by first measuring which features and which users generate the tokens, then putting limits at three levels: a hard spend cap at the provider, a quota per user or per plan in your own app, and a maximum size for every request. After that, the biggest savings usually come from sending less text per call, using a cheaper model where quality allows, and using the providers' prompt caching and batch discounts. Done together, these make the bill predictable instead of a monthly surprise.
This guide explains how AI APIs bill, how to see where the money goes, and the concrete changes that reduce it, roughly in the order you should make them.
Why is my AI bill higher than expected?
AI providers such as OpenAI and Anthropic charge per token, a unit of text a little shorter than a word on average. You pay for input tokens (everything you send: instructions, conversation history, documents, tool definitions) and output tokens (what the model writes), and output tokens usually cost more per token than input. Prices differ a lot between models, so check the provider's current pricing page for the models you use.
In apps built quickly with AI tools, the bill tends to grow for a few predictable reasons:
- Conversation history is resent every turn. In a chat feature, message 30 includes messages 1 to 29. Cost per message keeps rising the longer a conversation runs.
- Whole documents go into every call when only a section is relevant.
- The most capable model is used for everything, including tasks like classifying a message or extracting a date.
- No output limit, so the model writes long answers nobody asked for.
- Retries without limits. A failing request retried in a loop multiplies the cost of an error.
- Open endpoints. If the API route that calls the model has no authentication or per-user limit, anyone who finds it can run up your bill. If your key is in the front-end code, read what to do when an API key is exposed in the frontend first.
How do I see where the money goes?
The provider dashboards show spend by day, model and API key or project. That's a start, but it doesn't tell you which feature or which customer is responsible.
Every response from these APIs includes a usage section with the input and output token counts. Log those numbers in your own database for each call, along with:
- the feature name (
summarise_report,chat,suggest_reply), - the user or account ID,
- the model,
- whether the call succeeded.
With a week of that data you can answer the questions that matter: which feature costs the most, what the heaviest 1% of users spend, and what a typical paying customer costs you per month compared with what they pay.
Using a separate API key or project per environment (development, staging, production) also helps, so testing doesn't hide inside production numbers.
What limits should I set?
At the provider
- OpenAI lets you set monthly spend alerts and hard spend limits for the organization and for each project. Once a project's hard limit is reached, requests return a 429 error with the code
project_spend_limit_exceededuntil the limit is raised or the month resets. Alerts alone don't stop spending. - Anthropic lets you set a monthly spend limit for the organization in the Claude Console billing settings, and lower limits per workspace (not on the default workspace).
A hard cap protects you from disaster, but it stops the feature for everyone when it's hit. Your app needs to handle that error gracefully with a clear message, not a blank screen.
In your app
Per-user limits are what keep one account from consuming everyone's budget. Typical options:
- A number of AI actions per day or month per plan, stored and checked on the server.
- A maximum document length or number of pages per request.
- A cooldown between expensive actions.
Our guide to rate limiting AI features shows how to implement these. If you sell plans, tie the quotas to them, as described in feature gating by plan.
On each request
Set a maximum output length on every call, and set a timeout. Neither should be left to the default.
How do I make each call cheaper?
- Send less context. Summarise old conversation turns instead of resending them in full, keep only the last few messages verbatim, and retrieve the relevant parts of a document rather than the whole thing.
- Pick the smallest model that does the job. Test the cheaper model on 20 or 30 real examples. Use the larger one only where the difference shows. Many apps route simple steps (classify, extract, rewrite) to a small model and keep the larger one for the main answer.
- Structure prompts for caching. Both OpenAI and Anthropic offer prompt caching, where a repeated prefix of a prompt is billed at a fraction of the normal input price on later calls. OpenAI applies it automatically for supported models once the prompt is long enough; Anthropic requires you to mark what to cache with
cache_control. In both cases, put the stable parts (instructions, tool definitions, reference material) first and the changing parts (the user's question, timestamps) last. Writing to the cache can cost slightly more than normal input, so it pays off when the same prefix is reused within the cache lifetime. - Batch what isn't urgent. OpenAI's Batch API charges 50% less than normal calls in exchange for results within 24 hours; Anthropic also offers batch processing at a discount. Nightly summaries, bulk tagging and report generation are good candidates.
- Cache full answers yourself. If many users ask the same question about the same data, store the answer and reuse it until the data changes.
- Ask for shorter output. Instruct the model to answer in a set format and length; it's often faster as well as cheaper.
How do I know the changes worked?
Compare cost per feature and cost per active user from your logs for the week before and after each change, not just the total bill (which also moves with traffic). Check quality on the same set of real examples you used to choose the model. A saving that makes answers noticeably worse usually costs more in churn than it saves.
Quick answers
What's the fastest way to stop a runaway bill? Set a hard spend limit at the provider today, then add per-user limits in your app.
Does prompt caching change the answers? No. It only changes how repeated input is billed and how quickly it's processed.
Should I let users bring their own API key? It moves the cost to them, but you then handle their secret keys, which adds security work. For most consumer apps, quotas tied to plans are simpler.
How do I price an AI feature? Use your logged cost per user per month, add a margin, and build the limits into the plan, so heavy use either costs more or stops at a ceiling.
Want a second look at your usage?
If your AI bill is growing faster than your user count, our AI development team can review how your app calls the models and put limits, logging and caching in place, and maintenance and support keeps an eye on it afterwards. You can also contact us with your current setup. OpenAI documents its controls in the spend limits guide.


