Vibe Code Guardian
← All posts

Rate Limiting in AI-Generated Code: Why Your API Has No Brakes

AI coding tools build features fast but rarely add rate limits. The result: open endpoints that anyone can hammer until your bill spikes or your database locks up.

Aaron Allen·

The endpoint that worked fine until it didn't

You asked Cursor to add a password reset flow. It generated the route, the email template, the Supabase call. You tested it. It worked. You deployed.

Two weeks later your SendGrid bill is 400 because someone wrote a script that hits your reset endpoint ten thousand times in an hour. The emails still send. The endpoint still returns 200. The only thing that changed is who is calling it.

This is not an edge case. It is the default for AI-generated code.

Why AI tools skip rate limiting

When you describe a feature to an AI coding assistant, you describe the happy path. The model writes code that does exactly that. It does not write code that prevents someone from calling it fifty times a second.

Rate limiting is a cross-cutting concern. The AI optimises for the feature you asked for. It does not optimise for the operational reality of a public endpoint.

The same gap appears on authentication endpoints, search endpoints, file upload endpoints, webhook receivers, any API route that calls a paid service, and any endpoint that writes to your database.

What no rate limit looks like in production

The bill shock

Your app uses OpenAI for a chat feature. No limit on requests per user. No limit on prompt length. A user sends 100,000 token prompts in a loop. Your OpenAI bill arrives at 3,000.

The database lock

A search endpoint runs a full-text search with no pagination limit. Someone sends a query that matches every row. The query runs for forty seconds. Ten parallel requests later your database CPU is at 100 percent.

The email reputation hit

The password reset endpoint sends an email on every request. No cooldown. An attacker cycles through a list of emails. Your domain gets flagged for spam.

The webhook flood

Stripe sends a checkout.session.completed event. No idempotency check. Stripe retries because your handler took too long. You process the same order five times.

The minimal rate limiting setup

You need three layers.

Layer 1: Hosting-level protection

Enable built-in DDoS and rate limiting on Vercel, Netlify, Cloudflare or AWS. They are free and stop volumetric attacks before they reach your code.

Layer 2: Framework middleware

Next.js, Astro, Express, Fastify, Hono — every modern framework has rate limiting middleware. Add it globally, then tune per route.

Layer 3: Application-level logic for paid operations

For any endpoint that calls a paid API or sends email, add a per-user quota in your application code.

How to test your own endpoints

You need curl and a loop.

Test 1: Basic rate limit with a for loop hitting your endpoint 15 times.

Test 2: Per-user quota on authenticated endpoints.

Test 3: Expensive endpoint estimation.

Test 4: Webhook idempotency.

What the free surface check sees

The free surface check at Vibe Code Guardian looks at what is publicly visible. It cannot test rate limits. That is why the launch-readiness audit includes live endpoint testing.

A checklist you can run this afternoon

  1. List every API route.

  2. Mark routes that call paid services, send email, write to database, or handle auth.

  3. Ask what happens if someone calls each 100 times a minute.

  4. Enable hosting-level rate limiting.

  5. Add framework middleware with a generous global limit.

  6. Add stricter per-route limits.

  7. Add per-user quotas for paid endpoints.

  8. Test each one with curl.

  9. Document the limits in your README.

The pattern that prevents this problem

The root cause is not I forgot rate limiting. The root cause is I asked the AI for a feature and it gave me the happy path.

Every time you prompt for an endpoint, add: Include rate limiting, per-user daily quota for paid operations, idempotency for webhooks.

If you would rather a developer verify the whole thing, the launch-readiness audit checks 137 points across 14 areas including rate limiting.

Want a developer to look at your app?

Built your app with AI tools? Run the free surface check yourself in a few minutes. Nothing to buy, and we never look at your app unless you run it.