# Latency routing

**Last updated:** 2026-11-21T09:54:10-03:00 (UTC-3)

With two targets for the same model, the gateway can prefer the one that is responding faster right now. It learns from its own traffic: it measures how long each target takes to return the response headers and sends the next request to the fastest one. It creates no extra calls and no paid health checks. Flag: `latency_routing`.

## How to turn it on

```yaml
features:
  latency_routing: true
models:
  - id: text-balanced
    capabilities: [chat]
    latency_routing:
      max_age: 1m          # a sample older than this is forgotten
      probe_every: 10      # every 10 choices, try a target in rotation
    targets:
      - {provider: first-connection, model: upstream-model}
      - {provider: second-connection, model: upstream-model}
```

| Field | Values | What it does |
| --- | --- | --- |
| `latency_routing.max_age` | 1 s to 15 min | A latency sample older than this is forgotten: the target becomes unknown, not fast |
| `latency_routing.probe_every` | 2 to 1,000 | Every this many choices, one target is tried in rotation with the client's request, so a slow target that recovered is seen again |

The policy applies to aliases with two or more targets and the `chat`, `embeddings`, `moderations` or `rerank` capability. In the console, it lives in the model editor; the API is the same as for [versioned aliases](/docs/features/routing-and-admission/#how-to-turn-it-on). The configuration is validated even with the flag off.

## How it works

- **What is measured:** the time between sending to the provider and the arrival of the response headers (or the transport failure). It does not include the gateway's queue, reading the body or the time to the first token. It is neither TTFT nor an SLO.
- **Moving average:** `score = 0.8 × previous + 0.2 × new`. The first valid sample starts the average.
- **Penalty:** a transport failure, 429 or 5xx adds 30 seconds to the sample. Other 4xx responses and cancellations do not touch the score.
- **Forgetting:** a sample older than `max_age` becomes "unknown", not zero.
- **Exploration:** every `probe_every` choices, a target is picked in rotation, using the client's own request. If all are unknown, the rotation decides. Otherwise, the lowest average wins.
- **Fallback:** the other targets remain as fallbacks, in the previous order. Real fallbacks also feed the average.
- **Narrowed requests:** when [tag-based routing](/docs/features/routing-strategies/#by-tags) or a pinned continuation leaves a request only some of the alias's targets, only those compete and the rotation skips the others. The averages belong to the alias's targets, so such a request neither resets nor shifts them.
- A rejection due to capacity or the circuit breaker is not a send and produces no sample. The key's authorization and the team policy are applied before the choice.

## What is left out

To avoid learning from requests that are not comparable, the strategy only applies to:

- **Chat** with text messages (`system`, `user`, `assistant`), without stream, without tools, media or unknown fields;
- **Embeddings and moderations** with a text or a list of texts;
- **Rerank** with a text query and text documents.

Requests outside this profile use the normal order and do not feed the average. Responses, Messages, Gemini and MCP do not use the strategy.

## How to monitor it

**Providers → Routing & destination capacity → Passive latency observations** shows each target's average latency, for global sessions with `providers:read`. A target without a valid sample shows `known: false` and `header_latency_ms: null`. The number of observations does not prove the target is available right now.

## Limits

- **Per replica by default.** Each process has its own samples and choices and nothing is persisted. With `shared_routing_state` on, replicas also use the samples the others observed, averaged by sample count and as old as one sync interval; see [shared routing state](/docs/features/routing-and-admission/#shared-routing-state-across-replicas).
- **State reset.** Changing the configuration or rotating the provider's credential resets that target's observations.
- **Ceiling:** 4,096 eligible alias/capability pairs across the whole configuration; above that, the configuration is rejected.
- It does not combine with weights on the same alias.
- It adds no retry and does not change stream behavior.

Turning off the flag or removing the policy goes back to the previous selection immediately, without a migration.

## See also

- [Adaptive routing](/docs/features/adaptive-routing/): ordering by provider and key health, not latency alone.
- [Weights and capacity per target](/docs/features/routing-and-admission/).
- [Circuit breaker](/docs/features/resilience/).
- Reference: [Envoy's Peak EWMA](https://www.envoyproxy.io/docs/envoy/v1.38.6/api-v3/config/contrib/load_balancing_policies/peak_ewma/peak_ewma). The gateway implements neither P2C nor Peak EWMA; it uses the passive profile described here.

## Shared policy definitions

The [parameter registry](/docs/reference/parameter-registry/) shares the existing sample-age and probe-interval bounds with Go validation and console controls. Both members are required when the policy exists. The form’s 1m and 10-selection values are creation presets, not backend defaults and not an automatically enabled strategy. Existing capability/target restrictions, flags and versioned model editing remain unchanged.
