The purpose of rate limits
We apply rate limits for three reasons. They protect the API against abuse, because a malicious actor can send too many requests and make the service unavailable. They make access fair for all users, because one organization cannot make the API slow for the other users. They hold the total load on our infrastructure at a level that gives a constant response time.The operation of rate limits
We measure rate limits in three ways:- RPS (requests per second), for every endpoint
- TPM (tokens per minute), for the chat completion endpoints
- Audio seconds per minute, for the transcription and translation endpoints
- The request limit applies to your organization. All users and API keys in an organization draw on one request bucket.
- The token limit and the audio limit apply to each member. Prem tracks these two buckets for the member that owns the API key, not for the organization as a whole.
- Each endpoint has its own request allowance. Requests to one endpoint do not consume the allowance of another.
- Rate limits change with your tier. See Tiers for the capacity of each tier.
The token bucket
Each limit is a bucket with a capacity and a refill rate:- The capacity is the largest burst that you can send at one time.
- Each request takes 1 token from the request bucket. Each inference request takes its total token count from the token bucket.
- The bucket refills at a constant rate. This lets you send short bursts, but it stops continuous overuse.
Identifier scope
The rate limiter uses the most specific identifier that is available:- Organization ID: the primary scope. Your API key supplies it.
- User ID: the rate limiter uses this if there is no organization context
- IP Address: the rate limiter uses this only for unauthenticated requests
When you get to a limit
An empty request bucket returns a429 Too Many Requests status with a Retry-After header in seconds:
support_id. Send it to support with a timestamp if you need us to trace a request.
The
Retry-After value on this response is always 1, because every bucket refills at one request per second or faster. Do not use the value as a fixed back-off budget. Use it as a minimum. Then apply your own exponential back-off if the next attempt is also unsuccessful.Token and audio limits return a 400, not a 429
An empty token bucket or audio bucket does not return a429. The API refuses the request with a 400 status and one of these error strings:
The
TOKEN_LIMIT_EXCEEDED response holds your current usage in data.usage:
400.
Other 429 responses
The rate limiter returns the shape in the example. Not every429 that you receive looks like it.
A rate limit can also happen upstream of Prem. Those responses have a
429 status with a generic body and no Retry-After header:
500, but the status code is still 429. Automated clients must obey these rules:
- Branch on the HTTP status code, not the response body. Any
429is a rate limit, whatever theerror.typeor the message text says. - Do not expect a
Retry-Afterheader. Use your own exponential back-off with jitter when the header is not there.
429 responses without a Retry-After header, or with an unexpected body, contact us with the support_id and a timestamp.
Error mitigation
Steps to decrease rate limit errors
Be careful when you give users programmatic access, bulk processing features, and automated posting. Enable these features only for trusted customers. Set a usage limit for each user in a specified period. This protects against automated and high-volume misuse. Add a hard cap or a manual review process for users who go above the limit.Retry with exponential backoff
One simple way to prevent rate limit errors is to retry requests automatically with a random exponential backoff. With this method, the client sleeps for a short time when a rate limit error occurs. Then the client retries the unsuccessful request. If the request is again unsuccessful, the client increases the sleep time and retries again. This continues until the request is successful or until the client reaches a maximum number of retries. This approach has many benefits:- Automatic retries let you recover from rate limit errors without crashes or lost data
- With exponential backoff, the first retries occur quickly, and later retries have longer delays
- Random jitter in the delay prevents all retries from occurring at the same time
Example:
This implementation uses theRetry-After header from the API response. It uses exponential backoff when the response has no such header:
Control how many requests run at one time
Prem limits the number of inference requests that one API key can process at the same time. The limit applies to every inference endpoint, and it is separate from the request and token buckets. If your application sends many requests at one time, put them in a queue. A queue processes the requests in sequence or in controlled batches.Tips for developers
- Group related operations to decrease the number of requests.
- Cache frequently accessed data so that you do not request the same data again and again.
- Branch on the status code, not on the shape of the error body.
- Use the
Retry-Afterheader when the response has one. Apply your own back-off when the response has none. - Watch for
TOKEN_LIMIT_EXCEEDEDandAUDIO_DURATION_LIMIT_EXCEEDEDon400responses, not only for429. - Log the
support_idfor each unsuccessful request.
Higher rate limits
Your limits increase when you move to a higher tier. See Tiers. If you need higher limits, contact us for an enterprise plan with custom rate limits.Related
Tiers
The request, token, and audio limits of each tier.
Errors
Every status code that the API returns, and what to do about it.