Overview
By default, each call tolimit() consumes exactly 1 token. But many use cases require consuming different amounts based on the operation being performed.
The count parameter lets you consume multiple tokens in a single call.
Using the count Parameter
Example: LLM Token Consumption
When calling an LLM API, you want to limit based on tokens consumed, not number of requests:From the README: “Consume multiple in one request to prevent rate limits on an LLM API.”
Example: File Size Limits
Rate limit file uploads based on file size rather than number of uploads:Example: Batch Operations
Consume tokens proportional to batch size:Real Example from Source Code
Fromexample/convex/example.ts:
When to Use Custom Counts
Variable Cost Operations
When different requests have different “costs”:
- LLM API calls (token usage)
- Image generation (resolution/quality)
- Database queries (complexity)
Resource Consumption
When limiting based on resource usage:
- File upload bandwidth
- Storage space
- API credits
Batch Operations
When processing multiple items:
- Bulk inserts
- Batch exports
- Multiple file uploads
Tiered Usage
When requests have different weights:
- Premium vs free features
- Expensive vs cheap operations
- Priority queues
Combining with Per-User Limits
Custom counts work perfectly with per-user rate limiting:Fractional Counts
Counts can be fractional (floating-point numbers):Best Practices
Estimate conservatively
Estimate conservatively
When estimating costs (like LLM tokens), err on the side of overestimating to avoid hitting external API limits:
Use appropriate rate limits
Use appropriate rate limits
Match your rate limits to the metric you’re counting:
Validate count parameter
Validate count parameter
If the count comes from user input, validate it:
Consider checking before consuming
Consider checking before consuming
For expensive operations, check availability first:
Next Steps
- Learn about Checking Limits without consuming tokens
- Understand Error Handling for custom counts
- Explore Sharding for high-throughput limits