How to stop a surprise OpenAI or Anthropic bill in your AI-built app

Updated

How do I stop people from running up my OpenAI bill?

Never call the AI provider from the browser: put the call in a server function, require a signed-in user, add a per-user rate limit, cap input length and max tokens, and set a hard monthly spending limit in the provider dashboard. Keys in VITE_ or NEXT_PUBLIC_ variables are public.

How the bill happens

  • The API key is in the browser (a VITE_OPENAI_API_KEY or dangerouslyAllowBrowser: true). Bots scrape these.
  • A server function calls the AI for anyone, without login.
  • Logged-in users have no limit, so one account or script can make thousands of calls.
  • No max_tokens, so a single answer can be very long.

The checklist

  • Move every AI call into a server function (Edge Function, API route or server action).
  • Check the user at the top of that function.
  • Rate limit per user and per IP (for example 20 requests per hour), using a counter table or Upstash Ratelimit.
  • Cut user input to a sensible length and always set max_tokens / maxOutputTokens.
  • Set a monthly spending limit and alerts in the OpenAI, Anthropic or Google console.
  • Rotate any key that was ever in front-end code.

Questions

Is it fine if users bring their own API key?

Then the cost is theirs, but never store their keys in plain text, and still rate limit your endpoints.

Do I need rate limits if only paying users can use the feature?

Yes. A single paying account can still cost you more than it pays without a usage cap.