स्पीड लिमिट 25 का सड़क बोर्ड, जो याद दिलाता है कि AI chat API पर हर user के लिए request limit चाहिए
Photo by Joshua Hoehne on Unsplash · source · credits

Upstash से अपने AI Chat API पर Rate Limit लगाएँ, ताकि LLM बिल न उड़े

Also available in English: Read this article in English

आपके AI chatbot का हर मैसेज पैसे खर्च करता है, इसलिए खुला /api/chat route एक खुले बटुए जैसा है। एक script रात भर में हज़ारों requests भेज सकती है। Upstash Ratelimit एक छोटी TypeScript library है जो Upstash Redis में HTTP से requests गिनती है। यह Vercel और Cloudflare Workers जैसे serverless functions में चलती है।

जाँच कैसे काम करती है

Request flow: user पहचानें, ratelimit.limit चलाएँ, फिर LLM बुलाएँ या 429 लौटाएँ
Upstash Ratelimit docs से। इलस्ट्रेशन, स्क्रीनशॉट नहीं।

Model बुलाने से पहले limit जाँचें। अगर success false है, तो 429 लौटाएँ और tokens का पैसा न चुकाएँ।

सेटअप

  1. Upstash console में एक Upstash Redis database बनाएँ।
  2. npm install @upstash/ratelimit @upstash/redis चलाएँ।
  3. UPSTASH_REDIS_REST_URL और UPSTASH_REDIS_REST_TOKEN को environment variables में जोड़ें (Vercel पर: Settings, फिर Environment Variables)। इन्हें सिर्फ़ server पर रखें।

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() से limit और remaining भी मिलते हैं, और reset मिलीसेकंड में Unix time है। अगर users लॉग-इन करते हैं, तो IP की जगह user ID इस्तेमाल करें, क्योंकि एक IP कई लोग शेयर कर सकते हैं।

Algorithm चुनें

Upstash fixed window, sliding window और token bucket की तुलना
Upstash Ratelimit docs से। इलस्ट्रेशन, स्क्रीनशॉट नहीं।

AI खर्च के हिसाब से सेट करें

पूरी तस्वीर के लिए हमारी vibe coding security checklist, AI API की लागत कितनी है और vibe-coded ऐप कैसे deploy करें पढ़ें, या Upstash टूल पेज देखें।

स्रोत

FAQ

क्या Upstash Ratelimit Vercel पर चलता है?

हाँ। यह Redis से HTTP पर बात करता है, इसलिए serverless और edge functions में चलता है।

IP से limit करूँ या user ID से?

अगर user ID या API key है तो वही। एक IP कई लोग शेयर कर सकते हैं।

Redis डाउन हो तो क्या होगा?

Timeout के बाद, default 5 सेकंड, request को जाने दिया जाता है।

क्या tokens के हिसाब से limit कर सकते हैं?

हाँ। limit() में rate value दें, ताकि हर request पर 1 से ज़्यादा घटे।

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.