Skip to main content
A
Docs

Routing, budgets and limits

Send each request to the right provider with routing rules and fallbacks, and control spending with budgets and rate limits.

How the Gateway selects a provider

The Gateway uses these inputs to select a provider for each request:

  • Routing rules. A matching rule sets the target provider and model.
  • The model name. A provider/model name sets the provider. A bare name goes to a provider when exactly one provider has a key that lists it.
  • Provider weights. When a virtual key allows more than one provider, the Gateway selects one by weight.
  • Key weights. Among the keys of a provider, the Gateway selects a key by weight.
  • Fallbacks. When the call fails, the Gateway tries each fallback in order.

Routing rules

A routing rule has these parts:

PartMeaning
ConditionAn expression about the request, for example the model name
TargetThe provider, and optionally the model, to use
FallbacksOther provider/model targets to try in order
ScopeGlobal, or one team, customer or virtual key
PriorityLower numbers run first in a scope

To create a rule, open Routing Rules in the console and select Create.

Fallbacks in a request

A request can list its own fallbacks:

{
  "model": "provider-a/model",
  "fallbacks": ["provider-b/model"],
  "messages": [{"role": "user", "content": "..."}]
}

Budgets

A budget is a spending limit in US dollars. It resets after a period, for example 1d, 1w or 1M. You can set a budget on:

  • A virtual key
  • A team or a customer
  • A provider or a model

The Gateway calculates the cost of each request from the token counts and the model price. When a budget is used up, the Gateway refuses requests with 402 until the budget resets.

Rate limits

A rate limit caps tokens, requests or both in a period. When a limit is reached, the Gateway refuses requests with 429 until the period resets.

Error types

StatusTypeCause
401access_not_foundThe virtual key does not exist or is revoked
402budget_exceededA budget is used up
403access_blockedThe virtual key is turned off or expired
403model_blockedThe virtual key cannot use this model
403provider_blockedThe virtual key cannot use this provider
429rate_limited, token_limited, request_limitedA rate limit is reached