How to add error handling to agent tools
Quick answer
Add error handling to
agent tools by wrapping API calls and tool executions in try-except blocks to catch exceptions like RateLimitError or TimeoutError. Use retries with exponential backoff and fallback logic to ensure your agent recovers gracefully from failures. ERROR TYPE
api_error ⚡ QUICK FIX
Add exponential backoff retry logic around your API call to handle
RateLimitError automatically.Why this happens
Agent tools often call external APIs or perform complex operations that can fail due to network issues, rate limits, or unexpected input. Without error handling, these failures cause the entire agent to crash or behave unpredictably.
Example broken code calling an OpenAI model without error handling:
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Generate a summary"}]
)
print(response.choices[0].message.content) output
Traceback (most recent call last):
File "agent_tool.py", line 7, in <module>
response = client.chat.completions.create(...)
openai.error.RateLimitError: You have exceeded your current quota, please check your plan and billing details. The fix
Wrap the API call in a try-except block to catch errors like RateLimitError or TimeoutError. Implement retries with exponential backoff to handle transient failures. This prevents the agent from crashing and allows it to recover or fallback gracefully.
import time
from openai import OpenAI
import os
from openai import RateLimitError, Timeout
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
max_retries = 3
retry_delay = 1 # seconds
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Generate a summary"}]
)
print(response.choices[0].message.content)
break
except (RateLimitError, Timeout) as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt < max_retries - 1:
time.sleep(retry_delay * 2 ** attempt) # exponential backoff
else:
print("Max retries reached. Exiting.") output
Attempt 1 failed: You have exceeded your current quota, please check your plan and billing details. Attempt 2 failed: You have exceeded your current quota, please check your plan and billing details. Max retries reached. Exiting.
Preventing it in production
- Use retries with exponential backoff for transient errors like rate limits or timeouts.
- Validate inputs before calling tools to avoid predictable errors.
- Implement fallback logic, such as default responses or alternative tools, when retries fail.
- Log errors with context for monitoring and debugging.
- Set timeouts on API calls to avoid hanging.
Key Takeaways
- Always wrap agent tool calls in try-except blocks to catch runtime errors.
- Use exponential backoff retries to handle transient API failures gracefully.
- Validate inputs and implement fallback logic to maintain agent reliability.