Add a per-user rate limit to your AI endpoint with Upstash Redis, show a friendly message when it is hit, and protect your AI budget.
Limit each user to 20 AI requests an hour so nobody can drain my credits
How it works
- Create an Upstash Redis database: Vovy opens Upstash, creates a Redis database near your server, and saves the REST URL and token as secrets.
- Add the rate limiter: Vovy asks Claude Code to add @upstash/ratelimit with a sliding window, like 20 requests per hour, keyed by user ID.
- Check before calling the AI: Claude Code makes your AI endpoint check the limit first and return a 429 status with the reset time when a user is over.
- Show a friendly message: Claude Code updates the UI to say how many requests are left and when they reset, instead of a generic error.
- Test and deploy: Vovy fires 25 test requests to prove the 21st is blocked, then deploys once you approve.
What you provide
- An Upstash account (free tier works)
- Your app's AI endpoint
- The limit you want per user
What you get
- A per-user limit on AI calls
- Clear messages when users hit it
- A tested, deployed guard
FAQ
Can I give paid users a higher limit?
Yes. The limit can depend on the user's plan, like 20 an hour on free and 200 on paid.
Why not just rely on my OpenAI spend cap?
A spend cap stops the whole app for everyone. A per-user limit stops only the one abusing it.
Is Upstash free?
Upstash has a free tier with a generous monthly command allowance, enough for most early apps.
Related tasks
All tasks