Guide
How to rate limit an OpenAI API route and stop a surprise bill
Updated November 18, 2026
TL;DR
A server route that calls OpenAI, Anthropic, or another paid model with no sign-in and no rate limit is a cost trap. Anyone who finds the URL can loop it and spend your balance.
Require a signed-in user when the feature is not public. Limit requests per user and per IP. Cap max_tokens and how much text you accept. Set a provider budget and a usage alert. The budget caps the bill. It does not stop the abuse.
Why an open AI route is a cost trap
Moving the key off the browser is the right first step. The next bill comes from the route you hid the key behind. A short script can POST to /api/chat, or to a Supabase edge function, thousands of times. Each call is a real model request on your account. The visitor never sees the key.
Lovable, Bolt, Cursor, and v0 apps grow this route quickly: a generate button, a support bot, an image prompt. The happy path works when you click it. It also works for anyone else who copies the request out of DevTools. No exploit is required. A loop is enough.
A leaked key and an open route are different problems. Revoking a key in the bundle is covered in OpenAI API key exposed in your frontend? What to do now. This page is the route that still has a server-side key.
Require auth for anything that is not public
If the feature is for your users, check a session before you call the model. No session, no request: return 401. Anonymous demos are the exception, and they still need the limit below.
A Supabase edge function that sets verify_jwt = false will accept a call with no user. Leave JWT verification on unless you have a specific reason, and still check the user inside the function before you spend money. The service role key does not belong in that check.
Per-user and per-IP rate limits
Count requests. Key the counter by the signed-in user id, and also by IP address so a logged-out demo cannot spin forever. When the counter trips, return 429 and do not call the model.
A few requests a minute is plenty for a person clicking a button. A script doing one request a second is not a person. Pick a number you would notice, and apply it in the same function that calls the provider, before the call.
Where the counter lives depends on what you already run. These are ordinary options, not a ranking and not an endorsement:
- Upstash Ratelimit, if you already have a Redis-style store next to a Next.js route.
- Vercel Firewall rate limiting, on the path of the route, if the app is deployed on Vercel.
- A check at the start of a Supabase edge function: read a small counter, or reject when the caller is over the limit, before you call the model.
An in-memory counter inside one serverless instance resets when that instance goes away, and it is not shared across instances. It is better than nothing for a weekend demo. It is not a limit you should trust once the URL is public.
Cap max_tokens and the input
Rate limits count requests. A single request can still be expensive if you accept a huge prompt or let the client choose max_tokens. Set both caps in your code.
- Reject bodies over a size you picked. A few thousand characters is enough for most chat boxes.
- Pass a fixed
max_tokens, or the provider's output cap, that you chose. Ignore a larger number sent by the client. - Do not forward the raw request body to the model. Take the one field you need.
A streaming response does not change this. The meter runs while the stream is open. Cap the output there too.
Provider budgets cap the bill. They do not stop abuse
In the OpenAI, Anthropic, or other provider dashboard, set a usage limit or a monthly budget you would actually notice, and turn on the email alert. Menu names differ. Look for billing, limits, or usage.
That cap is the last line. It stops an unlimited invoice. It does not stop someone from using the route up to the cap, and it does not fix the route. Set the cap and still add auth and a rate limit. If the alert fires, assume the route was called by someone other than you until you have checked.
How to spot abuse in the usage dashboard
Open the provider's usage page. You are looking for shape, not a single magic number.
- A spike at a time you were not using the app.
- A sudden jump in request count with the same prompt size, which is what a loop looks like.
- Spend on a model or API key your app does not use.
- Traffic that continues after you thought you had taken the feature offline. An old deployment can still be serving.
Then match that window to your host logs for the route: status codes, IPs, and whether the calls were authenticated. The HackOrBroke scan cannot see this dashboard. It never calls OpenAI.
A prompt you can paste into Lovable or Cursor
Paste this into Cursor or Lovable after you have looked at the route. It changes code. It does not set the provider budget, and it should not print the key.
This app has a server route, server action, or Supabase edge function that calls a paid AI API (OpenAI, Anthropic, or similar). Anyone who can POST to it can loop it and spend the account.
Do this, and do not print or log the API key:
- If the feature is not meant for anonymous visitors, require a signed-in user and reject the request when there is no session.
- Add a rate limit keyed by user id when someone is signed in, and by IP address otherwise. A few requests per minute is enough for a demo. Return 429 when the limit is hit. Use whatever this repo already has. If it has nothing, a small counter is fine: Upstash Ratelimit, Vercel Firewall rate limiting, or a check at the top of a Supabase edge function. Do not add a new paid service if a limit already exists.
- Cap the input length and the provider max_tokens (or the equivalent output cap). Reject oversized bodies before calling the model.
- Do not raise max_tokens because a request asked for it.
- Do not put the provider key in a NEXT_PUBLIC_ or VITE_ variable.
When you are done, list the route files you changed, the limit you set, and where a reader can see a 401 and a 429. I will set a budget alert in the provider dashboard myself. A budget is not a substitute for the limit.What the free scan does and does not do
The free scan fetches the public page you submit and a few of its scripts. It notices key-shaped strings and calls to api.openai.com or api.anthropic.com in those files. It notices a Supabase URL or key the same way. It does not log in, query tables, or call OpenAI.
It does not send a POST to your AI route, so it cannot tell you the route is missing a rate limit. A key that never shipped to the page you scanned will not show up either. If you have an AI route, check the route yourself even when the report is a Pass.
Common questions
Can someone run up my OpenAI bill without stealing the key?
- Yes. If a server route holds the key and accepts requests from the public internet, a script can call that route in a loop. The key never leaves your server. The usage still lands on your account. Require auth when the feature is private, and rate limit every route that calls the model.
Does a provider budget stop the abuse?
- No. A budget or usage limit caps how large the bill can get. It does not stop requests, and it does not identify who sent them. Someone can still spend up to the cap. Set the budget, and also limit the route.
Should a public demo still be rate limited?
- Yes. A public demo can skip sign-in, but it still needs a per-IP limit, a small max_tokens cap, and a short input limit. Otherwise the demo URL is an open meter.
Does HackOrBroke test whether my AI route is rate limited?
- No. The free scan is read-only and external. It fetches the page and a few scripts and looks for key-shaped strings and OpenAI or Supabase usage. It does not log in, query tables, or call OpenAI, and it does not POST to your API routes.
Run the free scan
Point it at a URL you own. If the page ships an OpenAI key or calls api.openai.com from the browser, the report says so. It will not tell you that a server route is missing a rate limit, because it never calls that route.
Related: OpenAI API key exposed in your frontend? What to do now