AWS Bedrock error codes reference
400 BadRequestException, 401 AccessDeniedException, 429 ThrottlingException, and 500 InternalServerException. Each code indicates specific issues like invalid input, authorization failure, rate limits, or server errors that require tailored handling in your client code.api_error ThrottlingException automatically.Why this happens
AWS Bedrock API errors occur due to various reasons such as invalid request parameters, missing or incorrect permissions, exceeding rate limits, or internal server issues. For example, a BadRequestException is triggered when the request payload is malformed or missing required fields. A AccessDeniedException occurs if your AWS credentials lack necessary permissions. ThrottlingException happens when you exceed the allowed request rate. The API returns these errors with HTTP status codes and JSON error messages.
Example error output for a throttling error:
{
"__type": "ThrottlingException",
"message": "Rate exceeded"
}import boto3
client = boto3.client('bedrock-runtime', region_name='us-east-1')
try:
response = client.converse(
modelId='anthropic.claude-3-5-sonnet-20241022-v2:0',
messages=[{"role": "user", "content": [{"type": "text", "text": "Hello"}]}]
)
print(response['output']['message']['content'][0]['text'])
except client.exceptions.ThrottlingException as e:
print(f"Throttled: {e}")
except client.exceptions.BadRequestException as e:
print(f"Bad request: {e}")
except client.exceptions.AccessDeniedException as e:
print(f"Access denied: {e}")
except Exception as e:
print(f"Other error: {e}") Throttled: An error occurred (ThrottlingException) when calling the Converse operation: Rate exceeded
The fix
Implement robust error handling with retries and validation. Use exponential backoff for ThrottlingException to avoid hammering the API. Validate request payloads to prevent BadRequestException. Ensure your AWS IAM role or user has the correct permissions to avoid AccessDeniedException. Catch and log InternalServerException to retry later or alert support.
import boto3
import time
client = boto3.client('bedrock-runtime', region_name='us-east-1')
max_retries = 5
retry_delay = 1 # seconds
for attempt in range(max_retries):
try:
response = client.converse(
modelId='anthropic.claude-3-5-sonnet-20241022-v2:0',
messages=[{"role": "user", "content": [{"type": "text", "text": "Hello"}]}]
)
print(response['output']['message']['content'][0]['text'])
break
except client.exceptions.ThrottlingException:
print(f"Throttled, retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # exponential backoff
except client.exceptions.BadRequestException as e:
print(f"Bad request error: {e}")
break
except client.exceptions.AccessDeniedException as e:
print(f"Access denied error: {e}")
break
except client.exceptions.InternalServerException as e:
print(f"Internal server error, retrying: {e}")
time.sleep(retry_delay)
retry_delay *= 2
except Exception as e:
print(f"Unexpected error: {e}")
break Hello # or Throttled, retrying in 1 seconds... Throttled, retrying in 2 seconds... Hello
Preventing it in production
- Use exponential backoff with jitter for
ThrottlingExceptionto reduce request bursts. - Validate input data before sending requests to avoid
BadRequestException. - Configure AWS IAM policies with least privilege but sufficient permissions for Bedrock API calls.
- Implement monitoring and alerting on error rates to detect issues early.
- Use fallback logic or degrade gracefully if Bedrock service is unavailable.
Key Takeaways
- AWS Bedrock errors map to standard HTTP exceptions with specific causes.
- Implement exponential backoff retries to handle throttling gracefully.
- Validate inputs and configure IAM permissions to prevent common errors.