Documentation menu

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 before the provider is called, so a refused request costs nothing.

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:

SubjectApplies to
customerEvery customer, unless the customer has limits of its own
memberEvery member a request names, on top of its customer's limits: the same limits for each member
keyRequests 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, or null (or left out) for no limit.
  • spend: up to 16 limits, each with an id unique among them (1 to 256 bytes, not starting with hixel:, which is reserved), a window and limit_micros, an amount as a decimal string. Windows are calendar windows in UTC: day, week (starting Monday), month, or total for 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" }]
    }
  }'

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#

RefusalStatuscodeWhen
Spend limit reached402spend_limit_reachedA spend limit's window is used up. param is the limit's id.
Rate limit reached429rate_limitedToo 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 idApplies to
hixel:team_budgetEvery request of the customer, whichever member it names, or none
hixel:member_budgetEach 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:

HeaderValue
x-ratelimit-limit-requestsThe requests-per-minute limit
x-ratelimit-remaining-requestsRequests left in the current minute
x-ratelimit-reset-requestsTime until it resets, such as 12s
x-ratelimit-limit-tokensThe tokens-per-minute limit
x-ratelimit-remaining-tokensTokens left in the current minute
x-ratelimit-reset-tokensTime 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.

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. A request that only has rate or spend limits goes through, and its spend is counted once the check works again.