Debug Fix intermediate · 3 min read

How to handle CrewAI errors in python

Quick answer
Handle CrewAI errors in Python by catching exceptions specific to the API client, such as APIError or RateLimitError, and implement retry logic with exponential backoff. Always validate inputs and monitor error responses to gracefully recover from failures.
ERROR TYPE api_error
⚡ QUICK FIX
Add exponential backoff retry logic around your API call to handle RateLimitError automatically.

Why this happens

CrewAI errors typically occur due to network issues, invalid requests, or rate limits being exceeded. For example, calling the API without proper error handling can raise exceptions like APIError or RateLimitError. Below is a sample code snippet that triggers an unhandled error when the API key is invalid or the request is malformed.

python
import os
from crewai import CrewAIClient

client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])

# This call may raise an exception if the API key is invalid or rate limit exceeded
response = client.generate_text(prompt="Hello")
print(response.text)
output
Traceback (most recent call last):
  File "example.py", line 7, in <module>
    response = client.generate_text(prompt="Hello")
  File "crewai/client.py", line 123, in generate_text
    raise APIError("Invalid API key or rate limit exceeded")
crewai.exceptions.APIError: Invalid API key or rate limit exceeded

The fix

Wrap your API calls in try-except blocks to catch CrewAI exceptions. Implement exponential backoff retries for transient errors like rate limits. This approach prevents your application from crashing and allows graceful recovery.

python
import os
import time
from crewai import CrewAIClient
from crewai.exceptions import APIError, RateLimitError

client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])

max_retries = 3
retry_delay = 1  # seconds

for attempt in range(max_retries):
    try:
        response = client.generate_text(prompt="Hello")
        print(response.text)
        break  # success, exit loop
    except RateLimitError:
        print(f"Rate limit hit, retrying in {retry_delay} seconds...")
        time.sleep(retry_delay)
        retry_delay *= 2  # exponential backoff
    except APIError as e:
        print(f"API error occurred: {e}")
        break  # non-retryable error
    except Exception as e:
        print(f"Unexpected error: {e}")
        break
output
Hello
# or if rate limited:
Rate limit hit, retrying in 1 seconds...
Hello

Preventing it in production

In production, implement robust retry policies with capped exponential backoff and jitter to avoid thundering herd problems. Validate all inputs before sending requests to avoid APIError from malformed data. Monitor error logs and set alerts for repeated failures. Consider fallback mechanisms or degraded modes if the API is unavailable.

Key Takeaways

  • Always catch CrewAI exceptions to prevent crashes.
  • Use exponential backoff retries for transient errors like rate limits.
  • Validate inputs before API calls to avoid avoidable errors.
Verified 2026-04
Verify ↗