Skip to main content

How to stop people running up your AI API bill

Last updated

Keep the AI key on the server, require a signed-in user for every AI route, rate-limit per user and per IP, cap input size and output tokens, use a separate key per environment, and set a spending limit or budget alert in the provider’s console so a leak or a script cannot run up an unlimited bill.

How the bill gets run up

There are two routes. Either the key itself leaks (it is in the browser bundle or a public repository), or the key is safe but your own /api/chat route answers anyone, so a script can call it thousands of times. Both look like normal usage until the invoice arrives.

The checklist

  1. Call the AI provider only from server code; never put the key in a NEXT_PUBLIC_ or VITE_ variable.
  2. Require a session on every AI route and reject anonymous calls.
  3. Rate-limit per user and per IP, for example a few requests a minute and a daily cap per plan.
  4. Cap the prompt length and set a maximum output token count on every call.
  5. Use separate keys for development and production, and rotate a key the moment it appears anywhere public.
  6. Set a monthly spending limit or budget alert in the OpenAI, Anthropic or Google Cloud console.
  7. Watch usage per user and alert on spikes.

Questions

Is a spending limit enough?
It caps the damage but does not stop the abuse. Auth and rate limits stop it; the limit is the backstop.

Related Defexity checks

Defexity reports potential risks it can see from outside your app. It is not a guarantee of security and not legal advice.