Limits
Cap what each customer, member and key may spend and how fast it may call.
Limits stop a customer from spending more than you allow, and from crowding out others. They are checked at the gateway, and a refused request never reaches your client. When the check happens is yours to choose: see Checking limits before the call.
There are two kinds:
- Rate limits: requests per minute, tokens per minute and requests at once.
- Spend limits: at most an amount per calendar day, week, month or in total.
Who limits apply to#
An environment has one set of limits for each kind of subject, set in the dashboard under
Controls → Budgets & limits, or as limits with PATCH /v1/gateway with a project key:
| Subject | Applies to |
|---|---|
customer | Every customer, unless the customer has limits of its own |
member | Every member a request names, on top of its customer's limits: the same limits for each member |
key | Requests that name no customer, per key |
A customer can have its own limits, which replace the environment's customer limits for that
customer. Set them with the management API (limits on a customer, below) or on the customer's
page in the dashboard. null returns the customer to the environment's limits.
A request that names a member must fit both the customer's limits and the member limits.
Beside these, a customer can have budgets of its own: a monthly team budget and a budget per member, set by the customer's admin in the customer portal or by you. They only ever add to your limits, never replace them; see Budgets below.
The shape of a limit#
{
"rate": {
"requests_per_minute": 60,
"tokens_per_minute": 200000,
"max_concurrent": 4
},
"spend": [
{ "id": "daily", "window": "day", "limit_micros": "5000000" },
{ "id": "monthly", "window": "month", "limit_micros": "50000000" }
]
}rate: each field is a positive integer, ornull(or left out) for no limit.spend: up to 16 limits, each with anidunique among them (1 to 256 bytes, not starting withhixel:, which is reserved), awindowandlimit_micros, an amount as a decimal string. Windows are calendar windows in UTC:day,week(starting Monday),month, ortotalfor the customer's lifetime.
Setting a customer's own limits#
curl -X PUT https://api.hixel.dev/v1/customers/org_42 \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"limits": {
"rate": { "requests_per_minute": 120 },
"spend": [{ "id": "monthly", "window": "month", "limit_micros": "100000000" }]
}
}'await hixel.customers.upsert("org_42", {
limits: {
rate: { requests_per_minute: 120 },
spend: [{ id: "monthly", window: "month", limit_micros: "100000000" }],
},
});The whole limits object is replaced: a field you leave out takes its default (no limit). A
field out of bounds is refused with invalid_request, and param names its path, such as
limits.spend.0.limit_micros.
What a spend limit counts#
A spend limit counts what each request was billed: the amount after your markup, whether it was paid from the customer's wallet or from your organization's account. A request on the customer's own provider key is not billed, so it counts its provider cost instead.
Spend is counted when a request settles. Requests already running when a customer reaches a limit still finish, so spend can end slightly above the limit; the next request is refused.
When a limit is reached#
| Refusal | Status | code | When |
|---|---|---|---|
| Spend limit reached | 402 | spend_limit_reached | A spend limit's window is used up. param is the limit's id. |
| Rate limit reached | 429 | rate_limited | Too many requests or tokens this minute, or too many at once |
A 429 carries retry-after in seconds when the gateway knows when the request would fit
(not for the concurrency limit).
To tell which spend limit refused a request, read param, never the message: hixel:team_budget
is the customer's team budget, hixel:member_budget the budget of the member the request named
(see Budgets), and anything else is the id of one of your own limits.
{
"error": {
"type": "payment_required",
"code": "spend_limit_reached",
"message": "the spend limit `hixel:member_budget` has been reached",
"param": "hixel:member_budget"
}
}The first time a customer, or one of its members, is refused at a spend limit in a window, you
get a wallet.spend_limit_reached webhook;
for a key's own limit, key.spend_limit_reached. One event per crossing, not one per refused
request.
Budgets#
A customer's team policy adds two monthly spend limits under reserved ids, checked beside yours and counted the same way:
| Limit id | Applies to |
|---|---|
hixel:team_budget | Every request of the customer, whichever member it names, or none |
hixel:member_budget | Each member a request names: the member's own budget, else the team's default member budget |
Neither takes one of the 16 spend limits a subject may have, and neither can loosen yours: a
request must fit your limits and the budgets. A refusal is 402 spend_limit_reached with the
id in param, like any spend limit. A request that names no member is held to the team budget, never to a
member budget. To see what applies to a member and what it has spent, read its
effective policy.
Rate limit headers#
When a rate limit applies to a request, the response carries its state:
| Header | Value |
|---|---|
x-ratelimit-limit-requests | The requests-per-minute limit |
x-ratelimit-remaining-requests | Requests left in the current minute |
x-ratelimit-reset-requests | Time until it resets, such as 12s |
x-ratelimit-limit-tokens | The tokens-per-minute limit |
x-ratelimit-remaining-tokens | Tokens left in the current minute |
x-ratelimit-reset-tokens | Time until it resets |
On a request that runs on a customer's own provider key, the provider's own rate-limit headers are passed through too; where both have a header of the same name, the gateway's wins.
Model allowlists#
Which models a customer, key or environment may use is set by allowlists: see Models.
Checking limits before the call#
A request paid from a wallet always waits for its balance and limits to be checked before the provider is called. A request no wallet pays (on your account, or on a customer's own key) is, by default, sent to the provider while its limits are checked, so the check adds no wait before the first byte. A request over a limit is still refused and its provider call cancelled, but it may already have reached the provider, which then bills for what it processed; the next requests of that customer or key in the next minute wait for the check.
To have every request checked first, turn on “Check limits before the provider is called” on
the Budgets page, or send {"limits_enforcement": "strict"} to
PATCH /v1/gateway. A request over a limit then
never reaches the provider, at the cost of one check before the first byte. "alongside" is the
default.
If the balance cannot be checked#
A request paid from a wallet is refused with 503 and limiter_unavailable when the
gateway cannot reach the customer's balance: nobody else knows it. So is every request of an
environment whose limits_enforcement is strict. Otherwise a request that only has rate or
spend limits goes through, and its spend is counted once the check works again.