When you build an AI app, calling an API looks simple: send a question and get an answer. But what happens when the API is unavailable, your internet connection fails, or you send too many requests?. Your app might crash or leave users waiting without an answer.
In this article, we'll learn how to handle these problems using Python packages and the OpenAI API. You'll see how to retry failed requests, handle errors, set timeouts, and provide a fallback message when something goes wrong.
1. Why do AI API calls fail?
AI applications depend on external services, so failures can happen for several reasons.
| Problem | What it means | What to do |
|---|---|---|
| Rate limit | Too many requests were sent | Wait and retry |
| Server error | The AI service has a temporary problem | Retry |
| Connection error | The app cannot reach the API | Retry |
| Timeout | The response takes too long | Retry carefully |
| Invalid API key | Your credentials are incorrect | Fix the configuration |
| Bad request | The request contains invalid data | Fix the request |
The important thing to understand is that not every error should be retried.
Temporary problems may disappear if you try again. But if your API key is incorrect, repeating the same request won't help.
2. Let the OpenAI SDK retry and define timeout
The easiest way to handle temporary failures is to use the retry feature already available in the OpenAI Python SDK.
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
timeout=30.0,
max_retries=3
)The SDK automatically retries certain errors, including rate limits, connection problems, and server errors. For example, if a request fails because of a temporary network problem, the SDK can try again before returning an error.
Without a suitable timeout, users may wait longer than necessary. You can configure a timeout when creating your client.
For many small AI applications, this is a good starting point.
●What the SDK retries
Connection errors, 408, 409, 429, and 5xx responses are retried automatically with a short backoff. Errors such as 401 (invalid key) and 400 (bad request) are not.
3. Handle errors with try and except
Retries cannot fix every problem. Sometimes you need to tell your application what to do when a request fails.
Python provides try and except for this.
from openai import (
RateLimitError,
APIConnectionError,
APIStatusError,
)
try:
response = client.responses.create(
model="gpt-5.1",
input="Explain AI agents."
)
answer = response.output_text
except (RateLimitError, APIConnectionError, APIStatusError):
answer = (
"Sorry, the AI assistant is temporarily unavailable. "
"Please try again in a moment."
)
print(answer)
Here's how it works:
The try block runs your API request. The except block handles errors if something goes wrong. You can also handle different types of errors, such as rate limits and connection problems, in different ways.
For example, you can show a friendly message to the user instead of displaying a long Python error.
You can also handle timeouts separately with APITimeoutError if your application needs specific timeout behaviour.
4. Use smarter retries with Tenacity
Sometimes you need more control over retries.
Imagine that your application retries after one second, but the API is still overloaded. Trying again immediately may cause the same problem.
A better approach is exponential backoff. It means waiting longer after each failed attempt.
For example, your app might wait 2 seconds, then 4 seconds, then 8 seconds.
The Python library Tenacity makes this easy.
Install it with:
pip install tenacityThen use it in your application:
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
)
@retry(
wait=wait_exponential(
multiplier=1,
min=2,
max=10
),
stop=stop_after_attempt(3),
)
def ask_ai(question):
return client.responses.create(
model="gpt-5.1",
input=question
)
response = ask_ai("What is RAG in AI?")
print(response.output_text)This code retries the request up to three attempts, with increasing wait times.
Conclusion
Making AI applications reliable doesn't have to be complicated.
Start with the retry features in the OpenAI SDK and handle common errors with try and except. As your application becomes more complex, add controlled retries, timeouts, logging, and fallback responses.
✓The short version
The goal isn't to build an application that never fails. It's to build one that handles failures gracefully and gives users a better experience.
Want to build practical AI applications?
At Auragile, we're creating an AI Engineering course that helps developers learn by building real-world projects, from LLM-powered applications to more advanced AI systems.
Want to build practical AI applications?
Auragile's AI Engineering course teaches developers by building real-world projects, from LLM-powered applications to more advanced AI systems.
