6 min read
How to stop someone running up your OpenAI bill
If your OpenAI, Anthropic or Gemini usage jumped and you don't know why, someone is probably using your AI for free. Attackers scan new sites for AI endpoints that answer without a login. Here's what to do, in order.
1. Stop the bleeding now
- Open your provider's usage page and look at requests per hour. A flat wall of traffic at night is a script, not your users.
- Rotate the API key: create a new one, put it in your server's environment variables, redeploy, then delete the old key.
- Set a monthly budget and usage alerts in the provider's console. Where the provider offers a hard cap, set one: the worst case becomes your AI pausing, not an invoice. Google Cloud budgets only alert, they don't stop spending.
2. Find how they got in
It's almost always one of three things:
- An endpoint that answers without a login:
/api/chat, a Supabase Edge Function, a server action. Anyone who reads your page's code can find and call it. - The key in the browser. If your front end calls the AI provider directly (look for
dangerouslyAllowBrowser: true), the key is in the code every visitor downloads. - No limit per user. Even signed-in users can script thousands of requests if nothing stops them.
3. Lock the endpoint
- Check the signed-in session at the very top of every AI endpoint, before calling the provider. No session: return 401.
- Limit each user, for example 20 requests a minute and a daily cap, and return 429 when it's exceeded. Your host's rate limiting or a small store like Upstash Redis works.
- Cap the work per request: set
max_tokens, limit how long the user's input can be, and don't let users pick an expensive model. - Keep the key on the server only, in an environment variable without a public prefix (not
NEXT_PUBLIC_orVITE_).
Prompt for your AI tool
Find every endpoint or function that calls an AI provider. At the top of each, reject requests without a valid signed-in session (401). Add a per-user rate limit per minute and per day (429 when exceeded), cap max_tokens and input length, and make sure the API key is only read on the server. Show me each endpoint and what you changed.
Let Vern test your AI endpoints the way an attacker would
The AI spend check finds the AI endpoints your app calls, sends each one tiny test message without logging in, checks for a rate limit and spots AI keys in the browser. It costs you a fraction of a cent.
Who gets it: Builder, Pro and Agency, on apps you've verified
See plans