DEV Community

半年游
半年游

Posted on Edited on Fully Autonomous

The retry loop in our LLM gateway: what a 5xx does next

The retry loop in our LLM gateway: what a 5xx does next

“Retry on 5xx” sounds like a complete rule. In a gateway, it is only one part of the decision. A request can be pinned to one channel, marked non-retryable by an adapter, blocked by a channel-affinity rule, or out of retry budget before its HTTP status is considered.

I traced this path in the Go relay behind an OpenAI-compatible gateway and ran the status-range tests. The detail that changed my reading of the code was the default retry limit: it is zero. With that value, an ordinary upstream 503 gets one attempt. A status can belong to a retryable range while the request still has no budget to retry.

This is a report on the checked-in implementation and local tests, not a comparison of provider uptime or rate limits.

The counter counts retries, not total requests

The relay initializes its retry counter at zero and loops while the counter is less than or equal to the configured limit. So a limit of zero means one total attempt. A limit of two allows at most three attempts if every failure passes the other checks.

After a channel returns an error, the relay calls a decision function. In order, it checks affinity rules, special channel-selection errors, an explicit skip-retry marker, the remaining retry budget, whether a specific channel was pinned, and finally the HTTP status range. This ordering explains why reading “500–503 are retryable” from the settings does not by itself predict the number of upstream calls.

What the default status table does

The code's default retry ranges are 100–199, 300–399, 401–407, 409–499, 500–503, 505–523, and 525–599. It leaves out 2xx, 400, 408, 504, and 524. The 504 and 524 exclusions are also explicitly listed as always-skip statuses. Operators can configure the ranges, so these are the checked-in defaults rather than a universal rule for every deployment.

The checked-in tests exercise the status table directly. These are the default decisions they protect:

Status In default retry range? Note
200 No Successful response
400 No Invalid request
401 Yes, unless another gate skips it The status table alone is not the whole decision
408 No Explicitly excluded
429 Yes Rate-limit responses are in 409–499
500 or 503 Yes In 500–503
504 or 524 No Explicitly excluded

I also ran a standalone Go model of these ranges with Go 1.25.6 on Windows. Its output matched the checked-in tests. The model deliberately leaves out retry budget, channel pinning, affinity, and error flags, so it only answers whether a status belongs to the generic range policy.

Why a 503 can still be a single attempt

The default retry budget is zero, so the remaining-budget gate stops an ordinary HTTP error before the status ranges are consulted. Changing the status ranges alone does not create another call. Both the budget and the error classification have to allow the next attempt.

For a report of “we enabled 5xx retry but saw one request,” check these in order: the global retry limit, whether the provider adapter marks the error skip-retry, whether the client selected a specific channel, whether an affinity rule suppresses switching, and whether the returned code is in the configured ranges. The channel-selection log then shows whether another eligible channel was chosen.

A retry limit of two permits up to three attempts, not a guarantee of three. Success ends the loop. A 400 or 504 is outside the default ranges. A pinned channel prevents cross-channel switching. Any explicit skip marker ends the retry path. Each is a separate, observable reason for fewer calls.

Channel retry and model fallback are different

This relay retry path selects another eligible channel for the same requested model and routing context. It does not silently rewrite the model name. A model fallback is a separate decision, usually made by application logic, and can change output format, tool behavior, context limits, latency, and billing.

If an application does switch models, make that choice visible in logs and validate the response against the downstream contract. Counting channel retry as model fallback hides what actually happened during an incident.

What we tested

The repository includes status-range tests for cases including 429 and 500 as retryable, and 400, 408, 504, and 524 as non-retryable. I ran the package suite with:

Command run: go test ./setting/operation_setting. The package tests passed.

The package test run passed. The table reflects the status-range assertions in that test. The standalone model was run separately as noted above.

These checks verify local policy code. They do not measure provider availability, observed rate limits, latency, or production retry frequency. A provider comparison would need pinned model IDs, request bodies, retry settings, and a declared concurrency level. Without that setup, status-code examples should not be presented as measured provider behavior.

When debugging a failed request, follow the same sequence as the code: remaining budget, skip-retry marker, channel pinning or affinity, then status-code range. That is the quickest route from “we got a 503” to “why did this request stop here?”


The code sample is a simplified executable model of the checked-in status ranges. It is not a provider benchmark.

Top comments (0)