One API key, many upstreams: how failover works when a seller goes quiet

Published 2026-11-27 · LONG AI Engineering Notes · snapshot 2026-11-26 19:02 UTC+8

The promise of a gateway key is that you stop thinking about upstreams: one base URL, one key, and the model name you already have in your client. That holds right up to the moment one seller stops answering. Then the interesting question appears — failover is not "retry the same request", it is "decide whether a different seller can take this request, and what it costs to find out".

The marketplace shape behind the key is uneven by design. In the current public snapshot there are 109 live entries, and 58 of them have exactly one seller behind them; 16 have two, and the rest spread across three or more, with the widest entry reaching 15. Certification is uneven too: 193 certified sellers sit across 106 entries, and 3 entries have no certified seller at all. Failover quality is a property of that distribution, not of the gateway software.

What one key actually routes to

The key maps to a (model, seller) pair at request time, and the mapping is re-decided on every call unless you pin it. Three consequences follow:

The seller-count distribution per model is on the model plaza; the model reference pages that document units and tiers are on docs.

Three failures, three different actions

Most "failover" bugs come from treating every error as retryable. The distinction that matters is whether the request was rejected before or after the upstream started work.

FailureWhat it meansRight action
429 with Retry-Afterthe seller is congested but healthywait the header value on the same seller, or re-route if the deadline is tight
5xx / timeout mid-streamthe seller started work and gave upre-route, but treat the partial output as wasted billing
401 / 403 on a pinned paircredentials or quota on that seller changeddo not retry; unpin and pick another seller
connection refused / DNSthe seller is gone from the poolre-route immediately, log the seller out

The mid-stream case is the expensive one: tokens were already generated, so a retry re-bills the same prompt and pays twice for the failed half. That is why the retry budget should be denominated in money or tokens, not attempts.

Failover is a routing decision, not a retry loop

A retry loop that walks the same seller is amplification. A failover path changes the target and keeps the budget. Three rules keep it cheap:

  1. Try at most two sellers per request. Beyond two, the request is probably not going to finish inside the caller's deadline anyway.
  2. Never fail over on 400-class validation errors. The body is wrong, and the next seller will reject it identically.
  3. Carry one deadline across all attempts, not one per attempt — otherwise three attempts silently triple your tail latency.

A failover wrapper that does not double-bill

The snippet below keeps the deadline and the token budget outside the loop, and only re-routes on errors that are worth re-routing. It runs against any OpenAI-compatible endpoint, so it can be tested without a gateway.

import httpx, time

REROUTE_ON = {429, 500, 502, 503, 504}

def call_with_failover(client, payload, deadline_s=25.0):
    t0 = time.monotonic()
    for seller in ["a", "b"]:                     # at most two hops
        if time.monotonic() - t0 > deadline_s:
            break
        try:
            r = client.post("/v1/chat/completions", json=payload,
                            headers={"X-Seller": seller}, timeout=deadline_s)
        except (httpx.ConnectError, httpx.ReadTimeout):
            continue                              # seller unreachable, next hop
        if r.status_code not in REROUTE_ON:
            r.raise_for_status()
            return r.json()
        retry_after = r.headers.get("Retry-After")
        wait = min(float(retry_after or 2), 8.0)
        if time.monotonic() - t0 + wait > deadline_s:
            raise RuntimeError("deadline exceeded before hop 2")
        time.sleep(wait)
    raise RuntimeError("no seller answered inside the deadline")

Two details make it useful rather than illustrative: the deadline is checked before every hop, and the wait is capped so a bad Retry-After cannot consume the whole budget. The seller header is a stand-in for whatever routing hint your gateway accepts; the point is that the target changes while the budget does not.

What to watch on the status page

Failover only pays off if you can tell whether it worked. Three numbers answer that: the share of requests that needed a second hop, the p95 latency of second-hop requests versus first-hop ones, and the fraction of re-routes that ended in success. A rising second-hop share with a falling success rate means the pool is degrading and failover is hiding it.

The per-upstream view is on the status page; the definitions used for 429 share and retry success are in the FAQ.

The short version: pin when you need determinism, do not when you need price, and always measure how often you needed the second hop — because that number is the real quality of the pool you are buying from.