Using a Language Model in Code › Handling Errors Gracefully
AI Agent HandbookAll chapters Using a Language Model in Code › Handling Errors GracefullyOne section · Sections in this chapter7 / 15 ↓
API calls can fail. The network might be down, your API key might be invalid, or you might hit rate limits. Production code needs to handle these cases:
from openai import OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def ask_assistant_safe(
user_message, system_prompt="You are a helpful personal assistant."
):
"""
Send a message to the language model with error handling.
"""
try:
# Using GPT-5 for general-purpose text generation
response = client.chat.completions.create(
model="gpt-5.11", # ERROR: On purpose to demonstrate how to handle errors
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_message},
],
timeout=30, # Don't wait forever
)
return response.choices[0].message.content
except Exception as e:
# In production, you'd log this error properly
return f"Sorry, I encountered an error: {str(e)}"
## Now if something goes wrong, we get a friendly message instead of a crash
answer = ask_assistant_safe("What's the weather like?")
print(answer)This isn't bulletproof error handling. In a real application, you'd want to distinguish between different error types (network issues vs. authentication problems vs. rate limits) and handle each appropriately. But it's a start.
For intermediate readers: Robust error handling for LLM APIs typically includes: exponential backoff for rate limits, retry logic for transient failures, fallback to alternative models if the primary is unavailable, timeout handling, and detailed logging for debugging. You might also want to implement circuit breakers if you're making many requests, to avoid cascading failures. The tenacity library is useful for implementing retry logic with backoff.