# Adaptive routing

**Last updated:** 2026-11-17T17:10:39-03:00 (UTC-3)

With two or more targets for one alias, the gateway can order them by how healthy each provider connection and its keys have been on its own recent traffic: recent failures, a cooldown after consecutive failures, a credential pool with every key cooling, requests in flight and response-header latency. It learns only from real requests and never sends one of its own, so there are no paid health checks. Flag: `adaptive_routing` (off by default).

It goes further than [latency routing](/docs/features/latency-routing/), which compares only header latency: adaptive routing reacts to errors and rate limits, keeps a bounded history and a decision log, and honors key-pool health.

## How to turn it on

```yaml
features:
  adaptive_routing: true
models:
  - id: chat
    capabilities: [chat]
    adaptive_routing:
      history: 50            # outcomes kept per target
      failure_threshold: 3   # consecutive failures before cooldown
      cooldown: 30s          # how long a cooled target goes last
    targets:
      - {provider: primary, model: gpt-4.1-mini}
      - {provider: secondary, model: gpt-4.1-mini}
```

| Field | Values | Meaning |
| --- | --- | --- |
| `history` | 10 to 1,000 | How many recent outcomes each target keeps; older ones fall out of the failure share |
| `failure_threshold` | 1 to 100 | Consecutive failures that put a target in cooldown |
| `cooldown` | 1s to 10m | How long a cooled target goes last; afterwards one real request may try it again |

All three fields are required when the policy exists; the console offers 50, 3 and 30s when you add it. The policy applies to aliases with two or more targets and the `chat`, `embeddings`, `moderations`, `rerank`, `messages`, `responses` or `generate_content` capability (the last three only order requests with `adaptive_routing_native`, below). It does not combine with target weights, `latency_routing`, `auto_route` or a `routing_strategy` other than `ordered`. In the console it lives in the model editor (**Model catalog → Edit routing → Adaptive routing**); the API is the versioned model `PUT` with the `adaptive_routing` object. The configuration is validated even with the flag off, and the flag is classified as a model-policy feature for plan entitlements.

## Native Messages, Responses and generateContent

With the operator flag `adaptive_routing_native` (off by default, requires `adaptive_routing`), the same policy also orders the native routes of the alias: Anthropic `POST /v1/messages`, OpenAI `POST /v1/responses` and Gemini `POST /v1beta/models/{alias}:generateContent` (and `:streamGenerateContent`), when the alias routes them across two or more targets.

```yaml
features:
  adaptive_routing: true
  adaptive_routing_native: true
models:
  - id: claude
    capabilities: [messages]
    adaptive_routing: {history: 50, failure_threshold: 3, cooldown: 30s}
    targets:
      - {provider: anthropic-primary, model: claude-sonnet-4-5}
      - {provider: anthropic-secondary, model: claude-sonnet-4-5}
```

The guards are the same as on Chat: the decision reads the configuration snapshot of the request, a cooled target goes last until one recovery trial, the destination `admission.max_concurrency` ceiling and circuit breakers still refuse before send, and only real traffic is observed. One health history per alias is shared by its capabilities, so a Messages failure also demotes that target for the alias's Chat requests. Calls that must stay on one target keep their order: a Responses request that is not replayable (`previous_response_id`, `conversation`, a stored `prompt`, `background`, hosted tools or encrypted reasoning items), and Messages with native skills; their outcomes still count. With the flag off these routes keep the declared order and record nothing.

## How it decides

Each request orders the targets it may use by, most significant first:

1. **Available before cooling.** A target goes last while it is in cooldown, while another request is trying it after a cooldown (recovery trial), or, for Chat, while its [credential pool](/docs/features/provider-credentials/) has every key cooling (key health, as recorded by the pool's last selection; other capabilities send with the primary key and ignore the pool's state).
2. **Recovery trial first.** A target whose cooldown just ended goes ahead of the others for one request, so it can prove it recovered even while a peer stays healthy.
3. **Lower failure share** over the target's bounded history. An outcome counts for ten cooldowns (5 minutes with `cooldown: 30s`) and then is forgiven, so one transient error does not demote a target for good.
4. **Fewer requests in flight** to that destination on this replica.
5. **Lower header latency**, a moving average (`0.8 × previous + 0.2 × new`) of the time to the answer of buffered successful calls (for translated protocols this includes reading and translating the answer). A target with no sample yet sorts first, so a new or reset target gets real traffic.
6. **Declared order** breaks any remaining tie.

The order covers the fallbacks too: the next target tried is the next in this ranking. The decision is a pure function of the observed state; there is no randomness, so the same history always gives the same order.

**What counts as an outcome.** A transport failure, `401`, `403`, `408`, `429` and `5xx` count as failures of the target (its provider or its key), and so does a gateway deadline (the model's request timeout or a stream clock) that expires before the target answers. For Chat through a credential pool, `401`, `403` and `429` belong to one key, which the pool cools by itself, and do not count against the target. Successful answers count as successes; a successful answer the gateway then fails to translate counts as a failure. Other `4xx` answers belong to the client and are not counted. A client that disconnects and refusals before send (capacity, circuit breaker, credential pool with no key, network policy) are not outcomes. Streams count by their status; only buffered answers feed the latency average.

**Cooldown and recovery.** After `failure_threshold` consecutive failures a target goes last for `cooldown`. When the cooldown ends, the next request puts it first as its recovery trial, and other requests keep treating it as cooling until that trial's result arrives (counted or not) or another cooldown passes; a trial refused before send leaves it cooling for that cooldown. A success clears the cooldown; a failure starts a new one. There is no probe: the trial is a client's own request.

**Pinned and narrowed requests.** A pinned continuation or [tag routing](/docs/features/routing-strategies/#by-tags) narrows the candidates; only those compete, and the state of the others is neither read nor reset. Stateful calls (Responses with server state, native skills) keep their declared order. The [circuit breaker](/docs/features/resilience/), admission limits, quotas and retries apply as before; adaptive routing only changes the order.

## How to monitor it

**Providers → Routing & destination capacity → Adaptive routing observations** shows, for each alias, the decisions made, and for each target its outcomes and failures in the window, consecutive failures, cooldown or recovery trial, whether every key of its pool is cooling, requests in flight, header latency and how often it was the first choice, plus the latest decisions with their reason (`best_health`, `unobserved`, `recovery_trial`, `all_cooling`). The same data is in `GET /api/v1/admin/routing-status` under `adaptive` (`adaptive_routing` tells whether the flag is on), for global sessions with `providers:read`. The last 64 decisions are kept per alias.

## Limits

- **Per replica.** Each process has its own history and decisions; there is no consensus across replicas and no persistence. A restart starts every target unobserved.
- **State reset.** Any edit of the alias or of one of its target providers, or rotating a target's credential, resets that alias's state. Aliases removed from the configuration drop their state. A wildcard alias cannot use the policy.
- **Ceiling:** 4,096 adaptive aliases across the configuration.
- It adds no retry and no request. Turning the flag off or removing the policy goes back to the declared order immediately, without a migration.
