Overview
Sometimes you need to check if a rate limit would allow a request without actually consuming any tokens. The Convex Rate Limiter provides two methods for this:check()- Check if a request would be allowedgetValue()- Get the current token count and metadata
The check() Method
Thecheck() method evaluates a rate limit without consuming any tokens:
src/client/index.ts:78-111:
Difference Between check() and limit()
| Feature | limit() | check() |
|---|---|---|
| Consumes tokens | ✅ Yes | ❌ No |
| Can be used in queries | ❌ No | ✅ Yes |
| Can be used in mutations | ✅ Yes | ✅ Yes |
| Returns same format | ✅ {ok, retryAfter} | ✅ {ok, retryAfter} |
Use Cases for Checking Without Consuming
1. Showing Rate Limit Status to Users
Display remaining capacity before the user takes action:2. Validating Before Expensive Operations
Check capacity before starting expensive work:3. Conditional Logic Based on Capacity
4. Real Example from Source Code
Fromexample/convex/example.ts:80-85:
The getValue() Method
For more detailed information about the current state, usegetValue():
src/client/index.ts:168-209:
Return Value Structure
Example: Displaying Quota Information
Using calculateRateLimit
You can calculate the value at a specific timestamp using thecalculateRateLimit helper:
“You can use calculateRateLimit to calculate the value at a given timestamp”
Check with Custom Count
You can check if there’s enough capacity for a specific count:Best Practices
Check before expensive operations
Check before expensive operations
For operations that are expensive to start but cheap to validate:
Use check() in queries for UI state
Use check() in queries for UI state
Queries can’t consume tokens (they can’t modify state), so use
check():Avoid check-then-limit race conditions
Avoid check-then-limit race conditions
If you check in a query and then limit in a mutation, the state might change between calls. For critical operations, just use
limit() directly:Use getValue() for analytics and monitoring
Use getValue() for analytics and monitoring
Track rate limit utilization over time:
Next Steps
- Learn about Resetting Limits to clear rate limit state
- Understand Error Handling with
throws - Explore the React Hook for client-side checking