Skip to main content
Every Mesh API SDK ships two client-side resilience layers plus observability into both them and the gateway’s own routing:
  • Transport retry — automatic retries on transient HTTP failures, with a configurable policy.
  • Model fallback chain — try the next model when the primary fails.
  • debug / logger — see exactly which requests were retried and which were served by a fallback (client- and gateway-side).
This is the SDK counterpart to the gateway’s Resilient Routing. The two layers are independent and safe to combine — here is how a single create() call flows through both: Read it bottom-up for the guarantees: a terminal error (auth, validation, billing) short-circuits every layer; a transient one is first absorbed by gateway-side retries/fallback (no client involvement), then by SDK transport retries of the same request, and only then does the SDK switch models.

Transport retry

Every non-streaming request retries on 429 / 502 / 503 / 504 with exponential backoff + jitter, honouring Retry-After (default: 3 retries, 500 ms base, 30 s max). Streams never retry. The policy is configurable:
The legacy top-level maxRetries / max_retries option still works and maps onto retry.maxRetries; an explicit retry value wins when both are set.
Network-error retry is opt-in (retryOnNetworkError) and never applies to timeouts or cancellation — a timed-out POST may already be executing server-side, and completions are not idempotent.

Model fallback chain

Non-streaming chat completions can fall back to other models when the primary fails with a transient error (default 502 / 503 / 504, after transport retries are exhausted). Configure a chain client-wide, or override it per call:
Terminal errors (auth, validation, billing) never advance the chain.
The client-side fallbackModels chain is distinct from the models request parameter. models is a server-side, provider-handled ordered list sent in the request body; fallbackModels is a client-side directive the SDK acts on locally and never puts on the wire.

Seeing what happened: debug and logger

Set debug to print a readable line to stderr on every retry and fallback:
For structured logging, pass a logger — it receives every retry, fallback, and gateway-routing event:

gateway-routing events

A gateway-routing event reports the server-side resilience the gateway itself performed for your request — the per-key routing_policy’s same-target retries and cross-provider fallback. The SDK builds it by parsing the X-Mesh-Routing-Attempts and X-Mesh-Routing-Fallback response headers, so it only appears when your API key has an active routing policy. Which upstream provider served the request is internal and is not reported.
Streaming responses carry no routing headers (headers are flushed before the stream body). For per-request routing detail on streamed calls, check your Mesh API dashboard Logs.