Docs/Reliability and Error Handling

Reliability and Error Handling

Failover, retries, and error semantics

Token Hub fronts multiple independent upstream routes for every model. When one route fails, the gateway fails over automatically — most transient upstream problems never reach your code.

Automatic failover

PropertyBehavior
Retry scopeRetries happen before the first byte of a response — never mid-stream, so you never see duplicated partial answers.
AttemptsUp to two tries per upstream route, bounded by the gateway's global attempt budget.
Retryable statuses408, 409, 429, 500, 502, 503, 504, 529 (capacity).
Different-model fallbackIf all routes for a model are unhealthy the request fails fast with 502 — the gateway never silently substitutes a different model.

Error envelope

Every non-2xx response uses one envelope, so client handling stays uniform:

json
{ "error": { "code": "rate_limited", "message": "rate limit exceeded", "details": { "retry_after": 12 } }, "request_id": "req_01j9..." }

Always log request_id — it correlates directly with rows in your console request log and with operator traces.

Status codes

StatusCodeMeaningClient action
401invalid_api_key / key_revoked / key_expiredAuthentication problemFix the key; do not retry
402budget_exceededOrg or key spend cap reachedRaise budget in console, then retry
403model_not_allowed / insufficient_scopeKey lacks accessUse an allowed model or reissue key
404model_not_foundUnknown model idCheck the catalog; do not retry
429rate_limitedKey or org rate limit hitBack off; honor details.retry_after
500internalGateway faultRetry with backoff
502no_upstream / upstream_errorAll upstream routes unhealthyRetry after a short backoff
504upstream_timeoutUpstream exceeded the time budgetRetry with backoff; consider a smaller max_tokens

Client retry strategy

python
import time, random def with_retry(fn, tries=4): for i in range(tries): try: return fn() except Exception as e: if i == tries - 1: raise time.sleep((2 ** i) + random.random() * 0.5) # 1, 2, 4s + jitter
TipThe gateway already absorbs upstream blips for you — client retries are only for the residual cases (429s from your own rate limits, or full upstream outages). Prefer a few well-jittered retries over aggressive hammering.
Reliability and Error Handling — Token Hub Docs