Sub topic 7
Respecting the Retry-After header
This chapter assumes you can read HTTP status codes and headers, and that you have seen the exponential backoff with jitter from earlier chapters. Here you will add one more input to the wait calculation: the server's own advice.
What the header says
When a provider returns a 429 (rate limited) or a 503 (temporarily unavailable), it may include a Retry-After header. The header tells the client the earliest time a retry is likely to succeed. The standard allows two forms:
- A number of seconds, such as
Retry-After: 12. - An HTTP date, such as
Retry-After: Wed, 21 Oct 2026 07:28:00 GMT.
Many LLM providers send only the seconds form. Some send no header at all, and some use their own headers, such as reset times for request or token limits. Treat Retry-After as optional.
Why your own backoff is not enough
Your backoff schedule is a guess. The server knows when its limit window resets. If your guess is shorter than the real wait, you spend an attempt and probably get another 429. Each failed attempt also uses part of your retry limit. If your guess is longer, you wait longer than needed.
The header gives you a better estimate, but you should not follow it blindly. A server could send a very large value, such as 3600 seconds, and your application would stall for an hour.
The rule: take the larger value, then cap it
Combine the two sources like this:
- Compute the jittered backoff delay as usual.
- Read
Retry-After, if present and valid. - Use the larger of the two values, so you never retry earlier than the server asked.
- Cap the result at your maximum delay or your remaining deadline.
def parse_retry_after(value):
"""Parse a Retry-After header given in seconds. Return None otherwise."""
if value is None:
return None
try:
return max(0.0, float(value))
except ValueError:
return None # an HTTP-date form is not handled in this sketch
def choose_delay(backoff_delay, retry_after_header, cap=30.0):
retry_after = parse_retry_after(retry_after_header)
if retry_after is None:
return backoff_delay
return max(backoff_delay, min(retry_after, cap))
print(choose_delay(0.8, "5")) # 5.0, the server asks for more
print(choose_delay(8.0, "2")) # 8.0, our backoff is already longer
print(choose_delay(1.0, "3600")) # 30.0, capped
print(choose_delay(1.0, None)) # 1.0, no header
print(choose_delay(1.0, "soon")) # 1.0, unparseable value is ignoredThe full retry loop from the earlier chapter uses the same logic inside its post_with_retries function.
A decision when the cap is hit
If Retry-After is larger than your cap, the capped retry will probably fail again. A better choice in many cases is to stop retrying and surface the error, or to switch to a fallback. This is common when a daily quota is exhausted, because waiting a few seconds cannot help.
Common mistakes
- Ignoring the header and retrying on your own schedule.
- Trusting the header without a cap.
- Crashing on an HTTP-date value because the code only expects a number.
- Retrying all workers at the same moment after a shared
Retry-After. Keep jitter in the calculation.
Exercise
Your backoff delay for the third attempt is 2.0 seconds, the response carries Retry-After: 10, and your cap is 30 seconds. How long do you wait? What if the header says Retry-After: 600?
Show answer
With a header of 10, wait 10 seconds, the larger of 2.0 and 10. With 600, the capped value is 30 seconds, but a wait that long is unlikely to help, so consider stopping the retries or switching to a fallback.