Skip to main content
This guide walks you through setting up and using the Convex Rate Limiter component with real examples.
1

Install the Package

First, install the rate limiter component:
2

Configure convex.config.ts

Create or update convex/convex.config.ts to register the component:
convex/convex.config.ts
3

Define Your Rate Limits

Create a new file (e.g., convex/rateLimits.ts) and define your rate limits:
convex/rateLimits.ts
Rate limit strategies:
  • Token bucket: Tokens are added at a steady rate, allowing bursts up to capacity
  • Fixed window: All tokens granted at once every period, with optional rollover
4

Use in a Mutation - Global Rate Limit

Apply a global rate limit that applies to all users:
convex/users.ts
5

Use in a Mutation - Per-User Rate Limit

Apply a rate limit specific to each user using a key:
convex/messages.ts
6

Error Handling with throws Option

For cleaner code, use the throws option to automatically throw errors when rate limits are exceeded:
convex/auth.ts
When using throws: true, the rate limiter throws a ConvexError with data {kind: "RateLimited", name, retryAfter}. Use isRateLimitError() to check if an error is a rate limit error.

What You’ve Learned

You now know how to:
  • Install and configure the Convex Rate Limiter component
  • Define rate limits with different strategies (token bucket and fixed window)
  • Apply global rate limits that affect all users
  • Apply per-user rate limits using keys
  • Handle rate limit errors gracefully
  • Reset rate limits when needed

Next Steps

Explore more advanced features:
  • Custom token counts: Consume multiple tokens in a single request for LLM token limiting
  • Sharding: Scale to high throughput with configurable sharding
  • Reservations: Reserve capacity to avoid starvation on larger requests
  • React hook: Check rate limits from your frontend with useRateLimit
For detailed information, check out the full Stack post on rate limiting.