Skip to main content

Overview

By default, each call to limit() 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

From example/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

When estimating costs (like LLM tokens), err on the side of overestimating to avoid hitting external API limits:
Match your rate limits to the metric you’re counting:
If the count comes from user input, validate it:
For expensive operations, check availability first:

Next Steps