Sub topic 10
Using the Retry Settings Built into Provider SDKs
This chapter assumes you can call an LLM API from Python, know the common HTTP status codes, and have read the earlier chapters on retryable errors, backoff, jitter, and Retry-After. Most provider SDKs already implement those ideas. Before you write your own loop, check what your SDK does and configure it.
What the SDK already does
The official OpenAI Python SDK retries a failed request automatically. It retries these failures:
- Connection errors and network timeouts.
- HTTP 408 (request timeout), 409 (conflict), 429 (rate limit), and 5xx server errors.
It waits between attempts with a short exponential backoff plus jitter, and it respects a Retry-After header when the server sends one. Other 4xx errors, such as 400, 401, and 404, are raised immediately, because retrying cannot fix them.
The default is two retries. Defaults differ between SDKs and change between versions, so check the documentation for the version you have installed.
Configuring retries and timeouts
Set max_retries and timeout when you create the client. Then override them for a single call with with_options.
from openai import OpenAI
# The SDK retries connection errors, 408, 429, and 5xx responses with backoff.
client = OpenAI(max_retries=5, timeout=20.0)
# Settings can also be overridden for a single call.
response = client.with_options(max_retries=2, timeout=10.0).chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello."}],
)
print(response.choices[0].message.content)The client reads your API key from the OPENAI_API_KEY environment variable. Setting max_retries=0 turns automatic retries off. That is useful when you run your own retry logic, so the two layers do not multiply.
How the settings interact
The timeout applies to each attempt, not to the whole call. With max_retries=5 and timeout=20.0, a call can run for six attempts of up to 20 seconds each, plus the waits between them. That is more than two minutes in the worst case. If a user is waiting, pick values that fit your overall deadline.
When the SDK is enough
Use the SDK settings when your calls are simple request and response, and its defaults match your needs. This gives you tested backoff, jitter, and Retry-After handling with no extra code.
Write your own layer when you need something the SDK does not offer:
- A shared retry budget across many calls.
- Logging or metrics on every retry.
- A circuit breaker or a fallback model.
- Retry rules for a provider or HTTP client without built-in support.
If you add your own layer on top of an SDK, set the SDK's max_retries to a low value or to zero. Otherwise a failing service can receive many more requests than you intended.
Catching the final error
When retries run out, the SDK raises an exception. Your code should handle it.
import openai
from openai import OpenAI
client = OpenAI(max_retries=3, timeout=15.0)
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello."}],
)
print(response.choices[0].message.content)
except openai.RateLimitError:
print("Still rate limited after all retries.")
except openai.APIConnectionError:
print("Could not reach the API.")
except openai.APIStatusError as err:
print("API returned an error:", err.status_code)The more specific exceptions come first. RateLimitError and APIStatusError both carry the status code, and APIConnectionError covers network failures and timeouts.
Exercise
Your client is set to max_retries=4 and timeout=10.0. You also wrap each call in your own loop that makes up to 3 attempts. What is the worst-case number of HTTP requests for one call, and what should you change?
Show answer
Each of your 3 attempts can make 1 original request plus 4 SDK retries, so up to 15 requests. Set the SDK's max_retries to 0 (or a small number) so only one layer retries.