Skip to content

Weights and per-target capacity

Last updated: 2026-11-25T02:22:38-03:00 (UTC-3)

Two tools for when you have more than one target for the same model:

  • Weights (weighted_routing) split traffic between targets, such as 3 for the main account and 1 for the backup.
  • Per-target capacity (destination_limits) puts a ceiling on concurrent requests to each target, with a short queue. It keeps a small deployment, a tight quota or a self-hosted model from taking more load than it can handle.

The two flags are independent and can be turned on and off without a restart. The gateway’s global limits still apply.

features:
weighted_routing: true
destination_limits: true
models:
- id: chat-balanced
capabilities: [chat]
targets:
- provider: openai-primary
model: gpt-4o-mini
weight: 3
admission:
max_concurrency: 4 # up to 4 requests at the same time
queue_capacity: 8 # up to 8 waiting
queue_timeout: 1s # how long each one waits
- provider: alternate
model: upstream-id
weight: 1
Field Values Default
targets[].provider the ID of a configured provider required; the connection this target sends to
targets[].model the upstream model ID required; the model name the provider receives
weight integer from 1 to 10,000 1, if another target of the alias declares a weight
admission.max_concurrency 1 to 1,024 required
admission.queue_capacity 0 to 1,024; zero does not queue —
admission.queue_timeout 1 ms to 30 s 1 s

In the console, Model catalog → Edit routing edits the weight and capacity of each target. The API is PUT /api/v1/admin/models/{id}. The configuration is validated even with the flag off, and editing these fields neither turns the flag on nor gives anyone access.

The weight decides the first target of each request. The others remain as fallbacks, in the declared order, without repeating a target. The distribution is deterministic (smooth weighted round robin): at a 3:1 ratio, out of every four requests, three start on the first target.

  • Weight splits first choices, not tokens, cost or latency.
  • Each gateway replica has its own sequence; the ratio is exact per process, not across the cluster.
  • Changing an alias’s targets restarts that alias’s sequence.
  • When tag-based routing leaves a request only some of the targets, the weights split the first choice among those, with a sequence of their own; requests that see every target keep theirs. The selection counts are per alias target.
  • Without a weight or with the flag off, the declared order applies. A stateful Responses request (previous_response_id, conversation, background, containers, hosted tools) also keeps the declared order and a single send; a stateless one follows the weights and falls back like chat (Responses fallback).
  • Fallback only happens before the response is sent to the client; there is never a replay after the first byte.

A target is the combination of provider connection, protocol, Base URL, API version and upstream model. Every alias and route that reaches the same target shares the same slots. The credential is not part of the identity, so rotating the key does not reset the slots in use.

  • Combined ceiling. If several aliases declare admission for the same target, the most restrictive applies: lowest concurrency, smallest queue, shortest wait. An alias without admission inherits the ceiling of another active alias on the same target, so there is no unlimited path.
  • Queue. FIFO per target, within each process, with bounded size and wait, unless the target declares tenant fairness.
  • No slot available. A full queue or an expired wait becomes a 503 when no fallback is available. Cancelling the request frees its place in the queue.
  • Slot duration. The slot is taken before the circuit breaker and is only released when the upstream response ends, including in a stream (SSE). An error, EOF or cancellation releases the slot.
  • Not a provider failure. A refusal for lack of a slot does not open the circuit breaker.
  • Configuration change. Lowering concurrency does not drop what is already running; it just stops granting slots until usage is below the new ceiling. Shrinking the queue discards the last ones in the queue. Removing the target or turning off the flag removes those waiting, and in-progress responses finish normally.
  • State limit: up to 4,096 observed targets per process. Above that, a new admission is denied instead of deleting a target in use.

There is no token reservation, financial limit or distributed queue here. For money and tokens, use budgets and TPM.

A share of an alias’s traffic can also go to one canary target, with sticky sides, shadow copies and automatic rollback: see canary routing.

With the operator flag admission_fairness (off by default, requires destination_limits), a target’s admission policy can share its queue fairly across tenants, so one team or organization that fills the queue no longer starves the others.

features:
destination_limits: true
admission_fairness: true
models:
- id: chat
targets:
- provider: primary
model: gpt-4.1-mini
admission:
max_concurrency: 8
queue_capacity: 64
queue_timeout: 2s
fairness:
by: team # team or organization
weights: # optional; unlisted tenants weigh 1
team-checkout: 3
Field Values Default
admission.fairness.by team (the key’s team) or organization (the team’s organization, with organizations on) required
admission.fairness.weights tenant ID → turns per round, 1 to 100; at most 64 entries every tenant 1

How it admits. While a slot is free and nobody waits, a request is admitted at once, as before. When requests wait, each freed slot goes to the next tenant in a weighted round robin over the tenants that have waiters: a tenant takes up to its weight of consecutive turns, then the next waiting tenant goes; within one tenant the order stays first in, first out. A tenant with nobody waiting leaves the round and joins at the end when it waits again.

When the queue is full. A tenant’s share of the queue is queue_capacity × its weight ÷ the weights of the tenants waiting (with the arrival). An arrival whose tenant holds less than its share takes the place of the newest waiter of the tenant furthest over its own share; that waiter is refused with the usual overflow (503 destination_overloaded, or the next target) and counted as displaced. An arrival whose tenant is already at its share is refused as before. So a noisy tenant can use the whole queue while it is alone, and gives way as soon as another tenant arrives.

Tenant identity. The tenant comes from the authenticated key’s server-owned team and, for organization, the organization read with the team’s ceiling; no client header can choose it. Requests without a team (or without an organization, for by: organization) share one tenant with an empty ID. The gateway’s own calls on a request’s behalf (guardrails, shadow copies) count for that request’s tenant.

Shared targets. All aliases that reach the same target share one queue; the first fairness policy declared for that target, in configuration order, applies to it. Turning the flag off goes back to FIFO at once, for the waiters already queued too. Fairness is per replica, like the queue.

Where to see it. GET /api/v1/admin/routing-status adds, per destination with fairness, fairness_by and tenants (tenant, weight, waiting now, admitted, queued, overflow, displaced, timeouts, total and longest wait; at most 256 tenants per destination, the rest counted as (other)), and counters.displaced. The console shows them under Providers → Routing & destination capacity, and the policy in the model editor (Edit routing → Routing & destination limits → Share the queue fairly across tenants, weights one tenant=weight per line). With Prometheus, gateway_destination_admission_total{result="displaced"} is added while the flag is on, without a tenant label.

With routing_affinity on (catalog flag, default false, a model-policy right of the team plan), an alias with an affinity policy keeps the requests of one session, or of one key, on the target that last answered them. A conversation then stays on the same provider deployment, which keeps its prompt cache warm and its behavior consistent; a canary keeps each session on the side it was assigned to.

features:
weighted_routing: true
routing_affinity: true
models:
- id: chat-canary
capabilities: [chat]
affinity:
mode: session # or key
header: X-Conversation-Id # session mode; default X-Gateway-Session-Id
ttl: 30m # default 1h
max_entries: 20000 # default 10000
targets:
- {provider: openai-primary, model: gpt-4o-mini, weight: 9}
- {provider: openai-canary, model: gpt-4o-mini, weight: 1}
Field Values Default
affinity.mode session: the value of header; key: the authenticated key required
affinity.header 1–64 letters, digits or dashes; never a credential or connection header (Authorization, Cookie, X-Api-Key, Host…); session mode only X-Gateway-Session-Id
affinity.ttl 1m to 24h after the pin’s last use 1h
affinity.max_entries 100 to 100,000 pins per replica for this alias; all aliases together at most 200,000 10,000

The client sends the session in the declared header, for example X-Conversation-Id: 7f3c…. A value of 1–256 visible ASCII characters pins; an empty, longer or repeated header routes as if it were absent. The session is never read from the request body.

How a request is routed:

  1. Without a live pin (a new session, an expired pin, no header), the weights, the routing strategy, latency or adaptive routing choose as usual. The target that answers with a status below 400 becomes the pin.
  2. With a pin, the pinned target goes first and the others follow in the declared order. The weighted sequence, latency probes and adaptive decisions are not consumed by that request, so weights split new sessions, not requests.
  3. The pin is not used, and the strategies choose, when its target is no longer a candidate for this request: removed from the alias, excluded by tag routing, target conditions, health-based routing or the organization’s model ceiling; cooling under adaptive routing; or at its admission.max_concurrency ceiling.
  4. Everything after the order is unchanged. A pinned target refused by its circuit breaker or its queue, or one that fails (429, 5xx, transport), falls back to the next target, and the target that answers becomes the new pin. Admission, budgets, TPM, rate limits, organization ceilings and organization aliases are checked as for any request; affinity never lets a request reach a target those would refuse.

Scope. A pin belongs to one organization, team, key, alias and capability, digested together with the session value. The same session identifier sent by another key, team or organization never reads or steers that pin. Rotating a key starts new pins.

Pin IDs are keyed. The pin ID is an HMAC-SHA256 of that tuple, not a plain hash, so a reader of the shared routing state cannot confirm a guessed session value, key ID or team against a stored pin. The HMAC key is derived from the current provider encryption key (storage.provider_encryption_key_env), which every replica of a deployment shares; without one, from the console session signing key; without either, from a random key per process, so pins then stay on the replica that wrote them even with the shared routing state. Rotating the source key changes every pin ID once: each session is routed by the strategies on its next request and pinned again. Pins written by a release before this one are not read again and expire. Stateful Responses (previous_response_id, conversations, containers) and native skills keep their own pinned provider and ignore affinity.

Bounds and lifetime. Pins live in memory, in a least-recently-used table per alias capped at max_entries; the oldest is evicted past the cap. Each use extends a pin by ttl. Editing a target’s weight or admission keeps its pins; removing or replacing the target ends them; removing the policy or turning the flag off drops every pin of the alias. Pins are per replica unless the shared routing state is on.

The policy needs two or more targets and is refused on a wildcard alias or with auto_route. It is validated even with the flag off. In the console it is the Routing affinity section of Model catalog → Edit routing; the API is PUT /api/v1/admin/models/{id} with the affinity object.

By default each replica keeps its own latency samples, in-flight counts and pins. With shared_routing_state on (operator flag, default false) and PostgreSQL as the store, the replicas share them:

  • Latency. A replica uses the header latency the others observed for the same alias, capability and targets, averaged with its own by sample count (each replica’s weight capped at 100), so a new replica routes to the fast target from its first request.
  • Occupancy. least_busy and adaptive routing count the requests in flight to a destination on every replica.
  • Destination ceiling. admission.max_concurrency becomes a ceiling for the whole deployment: this replica admits up to max_concurrency minus what the others hold. Waiting requests are admitted when a local slot frees or when a cycle shows the others released theirs.
  • Affinity pins. A pin written by one replica routes the same session on the others.
features:
shared_routing_state: true
routing_state: # every field optional; read on each cycle, no restart
sync_interval: 1s # 250ms to 60s
timeout: 500ms # one store round trip, 50ms to 10s
replica_ttl: 10s # another replica's state counts this long; 2s to 10m, at least twice sync_interval
Field Values Default
routing_state.sync_interval 250ms to 60s 1s
routing_state.timeout 50ms to 10s 500ms
routing_state.replica_ttl 2s to 10m, at least twice sync_interval 10s

How it works. A worker per replica runs one PostgreSQL transaction each sync_interval (migration 182, tables gateway_routing_replicas and gateway_routing_affinity): it writes this replica’s document (fresh latency samples, in-flight count per destination) and the pins written since the last cycle, and reads the other replicas’ documents written within replica_ttl and the pins changed since its last read. The request path never touches the store: it reads the snapshot the worker built and adds its own local state. Pins are batched (up to 5,000 per cycle, 20,000 waiting); a pin keeps its absolute expiry while it waits, its lifetime is counted by the database clock and never exceeds the alias’s ttl, and a pin the store refuses five cycles in a row is dropped (pins_dropped). A replica that stops deletes its row, so its in-flight counts stop counting at once; a replica that crashes stops counting after replica_ttl. Only destinations of the current configuration are counted.

When the store fails. A cycle that fails or takes longer than timeout drops the remote part at once: the replica routes on its local state alone, the ceiling is the local one again, and no request fails because of the store. Pins written meanwhile wait for the next cycle. A later successful cycle returns to shared mode by itself. A snapshot older than replica_ttl is never used, even if the worker stalls.

Accuracy. The shared values are as old as one cycle. Two replicas can admit in the same interval, so the global ceiling can be exceeded by at most what the replicas admit within one sync_interval; a crashed replica’s in-flight count stops counting after replica_ttl. Remote state only reorders targets or tightens a ceiling: it never admits a request the local checks refuse. Malformed documents, negative or absurd counts and pins for aliases without a policy are ignored.

Where to see it. GET /api/v1/admin/routing-status adds shared_routing_state and shared_state (mode: shared, local_fallback with a reason — starting, storage_unavailable, store_error, store_timeout, stale — or off; other replicas read, last sync and failure, consecutive failures, pins written, read, pending and dropped, and the effective settings), and remote_in_flight per destination. The console shows it in Providers → Routing & destination capacity → Shared routing state. With Prometheus, /metrics adds gateway_routing_state_shared (1 shared, 0 local fallback), gateway_routing_state_replicas and gateway_routing_state_sync_total{result="ok"|"failed"}.

The same pins table carries the session state of the trajectory strategy of auto routing (the tier a session stands on and the turns left of its hold), under the router alias’s name: a hold written on one replica reaches the others on their next cycle and a restarted replica reads it back on its first one.

Without PostgreSQL the flag is accepted and the replica reports local_fallback with storage_unavailable.

Providers → Routing & destination capacity shows, for anyone with global providers:read:

  • per alias: the strategy the router applies (ordered, smooth_weighted, cost, least_busy, latency or adaptive); a strategy whose feature flag is off, or an alias with one target, reads ordered, because that is how its requests are routed;
  • per target, for every strategy: how many requests it was the first choice of (selected: the request’s first provider send went to it; a fallback or a retry is not counted, and the counts start over when the configuration changes); for a weighted alias its weight; for a least_busy alias the requests in flight to it (in_flight), which is what the strategy ranks by;
  • per target: effective ceiling, in progress, queue, wait, overflows, cancellations and timeouts, and with tenant fairness the per-tenant rows.
  • with tool_aware_routing, per alias running the trajectory strategy: the sessions tracked, how many stand on each tier, how many are held and how many requests came without a session. No session value or key ID is shown;
  • with routing_affinity, per alias with a policy: mode, header, lifetime, pins held out of max_entries, and how many requests the pinned target answered (hits), found no live pin (misses), moved to another target (moves), evicted or expired. No pin key, session value or key ID is shown.

The API is GET /api/v1/admin/routing-status; it answers when any routing feature is on (weighted_routing, routing_strategies, destination_limits, latency_routing, adaptive_routing, routing_affinity, canary_routing, tool_aware_routing or shared_routing_state). The response carries no host, path, key or payload.

Read the numbers with these caveats:

  • scope: process and process_instance_id tell you which replica the response comes from; another replica shows another queue. With the shared routing state, remote_in_flight is what the other replicas hold.
  • The declared order and Responses do not increment selected; zero there does not mean no traffic.
  • observed: false is a ceiling that is only configured, with no admissions yet. retired: true is a removed target that is still finishing responses.
  • An admission is not a paid call: it can be cancelled before it reaches the network. The counters reset when the process restarts.

With Prometheus and destination_limits on, /metrics publishes gateway_destination_in_flight, gateway_destination_queued and gateway_destination_admission_total{result=...}, aggregated per process, with no model, user or request labels. With Prometheus and routing_affinity on, /metrics adds gateway_routing_affinity_entries (pins held by the process) and gateway_routing_affinity_total{result="hit"|"miss"|"moved"|"evicted"|"expired"}, with no alias, key or session label.

These limits protect a target from all callers together. To keep one key or team from taking the whole capacity, add a concurrency cap per key or team, checked when the request is admitted, before any target is chosen.

  • Does not combine weights with latency routing or with the cost and occupancy strategies on the same alias.
  • Affinity is applied on Chat Completions and the routes translated through it, and on the native operation routes (Messages, Responses, embeddings, images and the others); native Gemini generateContent keeps its weighted order without affinity.
  • The weight ratio and the queues apply per replica; with shared_routing_state the destination ceiling, occupancy, latency and pins are shared, within one sync interval.
  • Responses keeps the declared order.

The parameter registry shares the existing destination admission constraints with Go validation and console controls. An explicit admission block requires positive max_concurrency; the form starts at 1 without making it a backend default. Omitted queue_capacity is the real value zero (no queue); an empty queue_timeout uses 1s. Selecting a policy does not enable destination_limits, grant access or add a new retry. Existing versioned model edits and per-operation ownership remain unchanged.

Target conditions narrow the pool before these strategies and capacity controls. A matching condition is eligibility, not priority, and an unconditional target remains eligible. Fallback cannot restore an excluded target.

m7gtw[email protected]