Skip to main content

Trends, tech news, and tool comparisons.

How to cap AI API costs in a SaaS before the bill surprises you

· 7 min read
Dealpuffin
Proof points and live coupons for early adopters

To cap AI API costs in a SaaS, set a hard spend limit at the provider, cap output tokens on every request, cache the repeated part of your prompts, move non-urgent work to batch jobs, and enforce a usage limit per customer in your own code. The provider limit stops a disaster, and the per-customer limit stops one user from eating your margin.

This guide walks through those five layers in order, using the OpenAI and Anthropic documentation read on 13 November 2026. Model names and prices change often, so check your provider's pricing page before you set numbers.

Layer 1: a hard limit at the provider​

Start with the control that works even if your code has a bug. OpenAI's rate limits guide separates two things you can set at the organization or project level. A spend alert sends a notification and API traffic continues. A hard spend limit makes affected API requests return a 429 error. OpenAI describes the hard limit as the way to enforce a monthly organization or project cap, and it is separate from the approved monthly usage limit OpenAI sets for each organization.

Anthropic takes a similar approach with tiers. Each usage tier has a monthly spend limit, and once you reach it you wait for the next month or qualify for the next tier. Anthropic also lets you set lower limits for individual Workspaces, which is useful if you want production and experiments on separate budgets.

Practical setup:

  1. Create one project or workspace per environment, so a test script cannot spend the production budget.
  2. Set a hard limit slightly above your expected monthly spend.
  3. Set an alert at roughly half of that, so you hear about growth before you hit the wall.
  4. Make your app handle a 429 gracefully, with a clear message and no retry loop.

Sources: OpenAI rate limits and Anthropic rate limits.

Layer 2: cap output on every request​

Output tokens usually cost more than input tokens, and a model that rambles is the cheapest thing to fix. Set a maximum output length on each call, chosen per feature. A classifier needs 20 tokens. A summary needs a few hundred.

There is one subtlety worth knowing. OpenAI notes that your rate limit is calculated as the maximum of your max_tokens setting and an estimate based on the request, so setting it close to the real response size also avoids wasting rate-limit headroom. Anthropic says its max_tokens parameter does not factor into its output-token-per-minute limits, so the rate-limit reason does not apply there, but the cost reason still does.

Also cap the input. Truncate or summarize long histories before sending them, and put a maximum on user-supplied text. An unbounded paste box is an unbounded bill.

Layer 3: cache the repeated prefix​

Many SaaS prompts start with the same system instructions, tool definitions or reference text. Both providers discount cached input.

  • Anthropic's prompt caching documentation lists cache hits and refreshes at a fraction of the base input price. Its rate limits page says that for most models cached input tokens do not count toward your input-token rate limit and are billed at 10% of the base input price, with some models priced lower still. Cache writes cost more than base input, so caching pays off when the same prefix is reused.
  • OpenAI says cached input tokens are discounted up to 95%, and that the minimum cacheable prompt length is 1,024 tokens for its newest model family. It also notes that cached tokens still count toward tokens-per-minute limits.

The design rule is simple: put the stable content first and the changing content (the user's message) last. A prefix only matches if it is identical, so a timestamp at the top of your system prompt will break the cache on every call.

Sources: Anthropic prompt caching and OpenAI prompt caching.

Layer 4: batch what does not need to be instant​

Nightly summaries, bulk tagging and report generation do not need an immediate answer. OpenAI's rate limits guide points to its Batch API for submitting large collections of requests without affecting your synchronous rate limits, and Anthropic documents a Message Batches API with its own queue limits. Check the current batch discount on each provider's pricing page; we did not verify a figure today, so we leave it out here.

A simple split: anything a user is waiting on goes through the normal API, and anything else goes into a queue.

Layer 5: limit each customer yourself​

Provider limits protect you from the provider's bill. They do not protect one customer's usage from another's, or your margin from your heaviest user. For that you need a counter in your own database.

  1. Record the tokens (or the cost) of each call against the customer ID. Both providers return token usage in the response.
  2. Define a monthly allowance per plan, in the unit your pricing uses.
  3. Check the counter before each call. If the customer is over, either block the request, downgrade to a cheaper model, or offer an upgrade.
  4. Show the customer their usage so a block is not a surprise.

How you set the allowance is a pricing question. Usage-based vs seat-based pricing for AI products compares the models, and usage-based billing after Metronome and Orb covers the tooling landscape if you want billing to meter usage for you.

A worked example​

Say your app summarizes support tickets. Each call sends a 3,000-token system prompt, a 500-token ticket and expects about 150 tokens back.

  • Output cap: set it to 300, not the model's maximum.
  • Caching: the 3,000-token prompt is identical on every call, so it is the part to cache and it sits at the top.
  • Batching: summaries for old tickets run in a nightly batch.
  • Per-customer: the free plan gets 200 summaries a month; the paid plan gets 5,000.

None of this depends on one provider's price, so it holds if you change models.

A note on credits and promos​

Provider credits can fund experiments. Our post on ChatGPT Pro API credits for builders covers one such offer. Plan unit economics on list prices, not on a promo.

Frequently asked questions​

What is the fastest way to stop an AI bill from running away?​

Set a hard spend limit on the provider project and cap output tokens per request. OpenAI says a hard limit makes affected requests return a 429 error, so it works even if your own code has a bug.

Does prompt caching reduce cost or just latency?​

Both. Anthropic bills cache reads at a reduced rate, 10% of base input price for most models, and OpenAI discounts cached input up to 95%. The cache only helps when the start of the prompt is identical between calls.

Should I use a cheaper model to save money?​

Often, for simple tasks such as classification or extraction. Test the cheaper model on your own examples first, and keep the stronger model for the requests where quality drops.

How do I stop one customer from using too much?​

Track usage per customer ID in your own database and check it before each call. Provider-level limits apply to your whole account, so they cannot enforce a per-customer cap.