orq.aiAdvent of RoutingDocs

Door 7 · still sealed

Hands off the wax, elf. This door opens on Dec 7.

-days--hours--min--sec

Rudolph is still polishing this one. Back to the calendar

Door 7 · Dec 7 Fallbacks, retries & timeouts

Rudolph through the fog

A provider goes down on Christmas Eve. Three fields make the router retry, switch provider, and still hand your app one normal answer.

Forty-six minutes to take-off. Fog has rolled over the sleigh-route planner and it has stopped answering. Sprocket is sweating through his hat.

  1. Sprocket

    The route planner is throwing 503s! Santa leaves at midnight and right now his map is blank.

  2. Tinsel

    Is the provider down, or just having a moment?

  3. Sprocket

    Does it matter? I was going to wrap the call in a while True.

  4. Tinsel

    Put the loop down. Three fields: a timeout so nothing hangs, retries for the hiccup, a fallback on another provider for the outage.

  5. Rudolph

    Visibility zero over OpenAI. Diverting to Azure. Cargo lands in one piece.

clientYour apporq.aiAI Routertimeout · retry · fallbacksprimaryopenai/gpt-6.2-soldownfallbackazure/gpt-6-sol12503 ×4fallback
  1. clientYour app
  2. orq.aiAI Routertimeout · retry · fallbacks
  3. primaryopenai/gpt-6.2-soldown
    fallbackazure/gpt-6-sol
Four 503s from the primary, one call plus three retries, then the router takes the Azure route. Your app gets one normal answer.

How it works

Tinsel’s fix fits on one request. Requests to /responses and /chat/completions (and embeddings, rerank and decisions) can carry three extra fields. They work in this order:

  1. Timeout. timeout.call_timeout sets a time limit in milliseconds for each call. A call that runs past it is cancelled and fails with 408. The router always retries 408, and 408 is a default fallback code.
  2. Retries. retry.count (0 to 5) tries the same model again when the provider answers with one of the codes in on_codes. The router always adds 408 to that list, so a call that hits your timeout is retried too. The wait between tries grows each time (up to 1s, 2s, 4s, …) and is randomised, so a thousand clients do not all retry in the same instant. If the provider sends a Retry-After header, the router waits that long instead, up to 60 seconds.
  3. Fallbacks. When the retries run out on a fallback code (by default 408, 429, 500, 502, 503, 504 and 529), the router moves to the next model in fallbacks and starts again, with the same retry rules. Set fallback_on_codes to choose those codes yourself.

Your app gets back one normal response, from whichever model answered. It never sees the failed attempts.

Try it with curl

curl -X POST https://my.orq.ai/v3/router/responses \
  -H "Authorization: Bearer $ORQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-6.2-sol",
    "input": "Plan the sleigh route",
    "retry": {"count": 3, "on_codes": [429, 500, 502, 503, 504]},
    "timeout": {"call_timeout": 5000},
    "fallbacks": [{"model": "azure/gpt-6-sol"}]
  }'

Or from the OpenAI SDK

The fields are not part of the OpenAI request types, so pass them in extra_body:

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["ORQ_API_KEY"],
    base_url="https://my.orq.ai/v3/router",
)

response = client.responses.create(
    model="openai/gpt-6.2-sol",
    input="Plan the sleigh route",
    extra_body={
        "retry": {"count": 3, "on_codes": [429, 500, 502, 503, 504]},
        "timeout": {"call_timeout": 5000},
        "fallbacks": [{"model": "azure/gpt-6-sol"}],
    },
)
print(response.output_text)

Good timeout values

From the docs’ recommended values:

Use case call_timeout
Real-time features 5,000 ms
Chat applications 15,000 ms
Streaming (time to first token) 30,000 ms
Batch processing 60,000 ms

Without a timeout, one non-streaming call may take up to 12 minutes.

If you send no retry at all, the router still retries twice on the usual error codes. Errors like 400 or 401 are not in the default retry codes: trying again will not fix a bad request or a wrong key.

Streaming

The router only retries or falls back before any output arrives (text, reasoning or a tool call). Once text is streaming to your user, a failure ends the stream with an error instead of starting over halfway through a sentence.

On the Anthropic endpoint (/v3/anthropic), only timeout is read from the request. Retries stay at the default of two on the default codes, and fallbacks come from Routing Rules.

Nice list

  • Pair a short call_timeout with a fallback. A hanging call becomes a 408, which is retried and then falls back, so the wait is bounded by timeout × attempts instead of the 12-minute default.
  • Set call_timeout to what your user will wait for: the docs suggest 5,000 ms for real-time features and 15,000 ms for chat.
  • Keep the chain short. The docs recommend at most three fallback models, with similar capabilities.

Naughty list

  • Sending retry with on_codes but no count. A retry object without count means zero retries: one call per model.
  • Wrapping the call in your own retry loop. The router already retries twice by default, so three tries in your loop can become nine calls to the provider.
  • Setting fallback_on_codes: [400] and expecting the defaults to stay. A custom list replaces them, so 429 and 503 stop falling back.
  • Expecting fallbacks to rescue a wrong model id. 404 and 403 are not default fallback codes, so the error comes straight back.

Elf's challenge

Roll in your own fog: send "timeout": {"call_timeout": 1} with no fallbacks. What status comes back, and how many attempts did the router make?

Stuck? Ask Tinsel for a hint

A timeout answers HTTP 408 with code timeout_error. 408 is in the default retry codes, and with no retry object the router retries twice, so expect three attempts before the error reaches you. The response does not list the attempts; the count comes from the default of two retries.

Christmas crackerWhy did Sprocket's while True get coal this year?Pull it for the punchline

It kept retrying 400s. Some requests are just naughty.