Provider Failover Without Duplicate Outputs or Surprise Bills
Design retry and fallback boundaries that move recoverable requests to another provider while protecting streams, reservations, and usage settlement.
Provider failover sounds simple: when one upstream fails, send the request to another. The difficult part is deciding whether the first attempt produced anything valuable, reserved capacity, or incurred billable usage before the second attempt begins.
Define the retry boundary before writing code
A request is safest to retry before any response headers or body bytes have been committed to the caller. At that point the gateway can discard the failed attempt and preserve one clean client response.
After streaming starts, a second provider can create duplicate text, conflicting tool calls, or a second charge. Treat that boundary as a state transition, not a best-effort convention.
caution: Once you stream, you cannot switch
The moment the first byte reaches the client, you are committed. Switching providers mid-stream produces garbled output and confuses the caller’s parser.
Classify failures by recoverability
- Retryable transport failures: Connection resets, timeouts before response, and temporary upstream unavailability may succeed on another route.
- Retryable capacity failures: Rate limits or exhausted marketplace capacity can justify selecting a distinct eligible provider.
- Non-retryable request failures: Invalid parameters, unsupported content, and authentication failures need a client change.
- Ambiguous failures: A disconnect after upstream processing may require idempotency or reconciliation rather than an automatic replay.
The failover decision tree
Not every failure should trigger a retry. The decision depends on whether the failure is recoverable, whether output has been committed, and whether the candidate routes are distinct.
start(Upstream failure detected)
cond:Output committed to client?
cond:Failure is retryable?
cond:New route available?
retry:Retry on distinct route
err:Return terminal error
dedup:Avoid duplicate charge
start -> cond
cond(yes) -> err
cond(no) -> cond2
cond2(retryable,no) -> err
cond2(retryable,yes) -> cond3
cond3(no) -> err
cond3(yes) -> retry
retry -> dedupKeep reservation and settlement atomic
- Reserve. Hold estimated capacity before sending the attempt upstream.
- Release failed attempts. Return unused capacity when the attempt fails before billable completion.
- Settle the winner. Charge actual measured usage only for the route whose response is committed.
- Audit anomalies. Record upstream deductions or partial-use cases that cannot be represented by the normal path.
tip: Reservation and release must be idempotent
If the reservation succeeds but the release message is lost, the system should eventually converge through background reconciliation rather than leaving capacity stranded.
Use distinct routes for fallback
Retrying the same unhealthy credential rarely improves the outcome. Track attempted connections and select a distinct eligible route so fallback actually changes the failure domain.
The candidate list should be rebuilt or revalidated before each attempt because health and capacity can change between selections.
warning: Avoid retry loops
If every candidate in the list shares the same upstream failure, retries will not help. Cap the attempt count and surface the error to the caller.
Streaming changes the rules
Once a stream is visible to the client, the gateway cannot quietly replace its prefix with output from another model or provider. Even semantically similar text will have different token boundaries and tool-call state.
For interactive streams, prefer a clear terminal error after commitment. For non-streaming requests, the gateway has more freedom to fail over before returning a response.
A failover trace in practice
This sequence diagram shows how a request flows when the first provider fails and the gateway retries on a different route, all while preserving a single client contract.
Client(App)
GW(Gateway)
P1(Provider 1)
P2(Provider 2)
Client -> GW: POST /v1/chat/completions
GW -> P1: Forward request
P1 -> GW: 503 timeout
GW -> GW: Classify as retryable, P1 not committed
GW -> P2: Retry on distinct route
P2 -> GW: Stream response
GW -> GW: Meter usage, settle
GW --> Client: Single response + request IDAn operational checklist
- Log every attempt: Include the gateway request ID, attempt number, route, status, and whether output was committed.
- Bound retries: Use a small attempt count and a total request deadline.
- Preserve one client contract: Return one response and one request identifier even when multiple upstream attempts occur.
- Reconcile billing: Compare settled gateway usage with upstream provider usage for ambiguous failures.
caution: Reconcile nightly
Automated reconciliation catches discrepancies before they compound. Compare your gateway usage logs with each provider’s invoice for every day.