Guides · VibeVigil
How to Stop Surprise OpenAI Bills: Rate Limit the AI Features in Your Lovable, Bolt or Cursor App
Why one user (or one bot) can run up your OpenAI or Anthropic bill, how to keep the key on the server, require sign-in, add per-user limits in a Supabase Edge Function, set provider spend limits, and a ready-to-paste fix prompt for your coding agent.
Not sure your app is safe? Run a free scan first.
VibeVigil checks your live app for exposed keys, Supabase tables a stranger can read, missing security headers, SSL problems and downtime. Every issue comes with a ready-to-paste prompt for your coding agent.
Scan my app for free → (only scan apps you own)
Your app has a "Generate" button. It calls OpenAI, Anthropic or another AI API. Each click costs you a little money.
Now picture one person, or one script, clicking it thousands of times. You pay for every call. If the key leaked, they don't even need your app.
This guide shows how to put a limit on your AI features so one user can't drain your account. Only test apps you own.
One honest note: VibeVigil's free scan looks for AI keys that leaked into your public code. It does not test how many times someone can call your AI endpoint. You set that limit yourself, with the steps below.
The short answer
- Keep the AI key on the server. The browser should never hold an OpenAI or Anthropic secret key. Call the AI from a Supabase Edge Function or another server route.
- Require sign-in. Only logged-in users should reach the AI route. Check the user's token on the server, not just in the UI.
- Add a per-user limit. For example, "20 generations per user per hour." The AI provider's own limits are for your whole account, not for each of your users.
- Set a spend limit with your AI provider. A monthly cap and an alert are your last safety net.
- Cap the size of each call. Set a max output size and a max input length so one request can't be huge.
Why vibe-coded apps get this wrong
AI coding tools are fast at wiring up an AI call. They often:
- Put the AI key in a
VITE_orNEXT_PUBLIC_variable, which ships it to every visitor's browser. - Build an Edge Function or API route that anyone can call, signed in or not.
- Skip any limit per user, so the only limit is the provider's account limit.
- Let the user send any prompt length and get any answer length.
- Retry failed calls in a loop with no stop.
OpenAI's own docs say its rate limits are set at the organization and project level, not the user level (OpenAI: Rate limits). So your app has to add the per-user limit itself. The same docs suggest setting a usage limit for individual users per day, week or month.
Step 1: Make sure the AI key is not in the browser
Open your live app. Press F12 and open the Sources or Network tab. Search the loaded JavaScript for:
sk-
sk-proj-
sk-ant-
OPENAI_API_KEY
ANTHROPIC_API_KEY
api.openai.com
api.anthropic.comIf you see a secret key, or the browser calls api.openai.com directly, the key is public. Rotate it now. Our guide on how to rotate a leaked API key walks through it.
Then move the call to the server. In Supabase, store the key as an Edge Function secret (Supabase: Edge Function secrets) and read it inside the function.
Step 2: Only let signed-in users call the AI route
Hiding the button for logged-out visitors is not enough. Anyone can call your function URL directly.
Inside the function, check the user's token and get the user from Supabase Auth. If there is no valid user, return 401 and stop. Supabase explains how to do this in Securing Edge Functions.
Quick test (on your own app): while signed in, open the Network tab, use the AI feature once, and copy that request as cURL. Remove the Authorization header and run it. You should get 401, not an AI answer.
Step 3: Add a per-user limit
Pick a limit that fits your app. A free user might get 10 a day. A paid user might get more.
There are two common ways:
Option A: a usage table in Supabase. Keep a table like ai_usage (user_id, day, count). In the Edge Function, before calling the AI:
- Read today's count for this user.
- If it is at the limit, return
429with a friendly message. - If not, add one and then call the AI.
Do the "check and add one" in a single database step (for example, a Postgres function), so two fast clicks can't both slip through. Turn on RLS on that table so users can't edit their own count.
Option B: Redis. Supabase has an official example that rate limits Edge Functions with Upstash Redis, keyed by the Supabase user ID (Supabase: Rate limiting Edge Functions). Upstash's Ratelimit SDK handles the counting for you.
Either way, the limit lives on the server. A limit only in React state resets when the user refreshes the page.
Step 4: Stop fake sign-ups from getting around the limit
If anyone can make unlimited free accounts, a per-user limit is easy to dodge.
- Turn on CAPTCHA for sign-up and sign-in (Supabase: Enable CAPTCHA protection).
- Require email confirmation before a new account can use AI.
- Check Supabase's auth rate limits so sign-up and email endpoints aren't wide open (Supabase: Auth rate limits).
- Give brand-new accounts a smaller limit at first.
Step 5: Set a spend limit with your AI provider
This is your backup if everything else fails.
- OpenAI: you can set a spend alert (a warning) and a hard spend limit for your organization or project. At the hard limit, requests return a
429error (OpenAI: Rate limits, spend limits). Use a separate project and key for each app so one app can't use up another's budget. - Anthropic: check the spend and rate limits for your organization in the Claude Console (Anthropic: Rate limits).
Pick a cap you could live with paying if someone abused your app for a whole month.
Step 6: Cap the size of each call
- Set a max output size (for example,
max_output_tokenson OpenAI) that fits your feature. - Reject prompts longer than you need. A product-description tool doesn't need a 50-page input.
- Limit retries. OpenAI's docs say to cap both the number of retry attempts and the total retry time, and not to stack your own retries on top of the SDK's.
Step 7: Test it
On your own app, with a normal test account:
- Call the AI feature until you pass your limit. You should get a clear "limit reached" message, not more answers.
- Wait for the window to reset and check it works again.
- Call the function URL directly with no
Authorizationheader (as in Step 2). You should get401. - Open a second test account and confirm its count is separate.
- Look at your provider's usage page and make sure the numbers match what you expect.
Fix prompt for your coding agent
Paste this into Lovable, Bolt, v0 or Cursor:
Please protect the AI features in this app from abuse and surprise bills:
1. Find every place the app calls OpenAI, Anthropic or another AI API. Make sure
no AI secret key is in frontend code or in VITE_ / NEXT_PUBLIC_ variables.
Move every AI call into a Supabase Edge Function (or server route) that reads
the key from a server-side secret.
2. In that function, verify the caller's Supabase Auth token and load the user.
If there is no valid user, return 401 and do not call the AI.
3. Add a per-user limit enforced on the server (for example 20 calls per user per
day). Store usage in a table with RLS enabled that users cannot write to
directly, and do the check-and-increment atomically in one Postgres function.
When the limit is reached, return 429 with a friendly message and show it in the UI.
4. Set a max output token limit and reject prompts longer than [N] characters.
5. Limit retries to a small number with backoff. Do not retry in an endless loop.
6. Do not change any other features. When done, list the files you changed and
tell me how to test that a logged-out request gets 401 and that a user who
passes the limit gets 429.After the agent is done, run the tests in Step 7 yourself. Then set your provider spend limit by hand. The agent can't do that part for you.
FAQ
Is my AI provider's rate limit enough? No. It limits your whole account, not each of your users. One user can still use all of it.
Can I just hide the AI button for free users? Hiding a button doesn't stop someone from calling the function URL. Check the plan and the limit on the server.
Does a free scan find this? A scan can find an AI key that leaked into your public code. It can't tell how many calls a user is allowed to make. Run the tests in Step 7 for that.
Sources
- OpenAI: Rate limits (including spend limits and per-user advice)
- Anthropic: Rate limits
- Supabase: Rate limiting Edge Functions
- Supabase: Securing Edge Functions
- Supabase: Edge Function secrets
- Supabase: Auth rate limits
- Supabase: Enable CAPTCHA protection
- Upstash: Ratelimit SDK
Want a second set of eyes on the outside of your app? Run a free VibeVigil scan to check for exposed keys, open Supabase tables, headers and SSL. Only scan apps you own.
Scan your app free. It checks the public page of a site you own. No account.