Speed limit 25 road sign, a reminder that an AI chat API needs a request limit per user
Photo by Joshua Hoehne on Unsplash · source · credits

Rate-Limit Your AI Chat API With Upstash Before Abuse Drains Your LLM Bill

Also available in Hindi: यह लेख हिंदी में पढ़ें

Every message to your AI chatbot costs money, so an open /api/chat route is an open wallet. One script can send thousands of requests overnight. Upstash Ratelimit is a small TypeScript library that counts requests in Upstash Redis over HTTP. It works in serverless functions such as Vercel and Cloudflare Workers.

How the check works

Request flow: identify the user, call ratelimit.limit, then call the LLM or return 429
From Upstash Ratelimit docs. Illustration, not a screenshot.

Check the limit before you call the model. If success is false, return a 429 and never pay for the tokens.

Setup

  1. Create an Upstash Redis database in the Upstash console.
  2. Run npm install @upstash/ratelimit @upstash/redis.
  3. Add UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN to your environment variables (on Vercel: Settings, then Environment Variables). Keep them server-side.

The route

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(10, "60 s"),
  prefix: "ratelimit:chat",
});

export async function POST(req: Request) {
  const id = req.headers.get("x-forwarded-for") ?? "anonymous";
  const { success, reset } = await ratelimit.limit(id);
  if (!success) {
    const wait = Math.ceil((reset - Date.now()) / 1000);
    return new Response("Too many requests", {
      status: 429,
      headers: { "Retry-After": String(wait) },
    });
  }
  // call your LLM here
}

limit() also returns limit and remaining, and reset is a Unix time in milliseconds. If users log in, use their user ID instead of the IP address, because many people can share one IP.

Pick an algorithm

Upstash fixed window, sliding window and token bucket compared
From Upstash Ratelimit docs. Illustration, not a screenshot.

Tune it for AI costs

For the bigger picture, read our vibe coding security checklist, how much an AI API costs and how to deploy a vibe-coded app, or see the Upstash tool page.

Sources

FAQ

Does Upstash Ratelimit work on Vercel?

Yes. It talks to Redis over HTTP, so it works in serverless and edge functions.

Should I limit by IP or user ID?

User ID or API key when you have one. IPs can be shared by many people.

What happens if Redis is down?

After the timeout, 5 seconds by default, the request is allowed through.

Can I limit by tokens?

Yes. Pass a rate value to limit() to subtract more than 1 per request.

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.