Bedrock Runtime: InvokeModel, exceeded maximum number of attempts, 3, https resp...
🚨 错误信息
🤖 AI解决方案
原文:ThrottlingException: Too many requests, please wait before trying again.
译文:限流异常:请求过多,请稍后再试。
原因分析
claude-sonnet-4-5-20250929模型时,系统在3次重试后仍然收到429状态码响应解决方案
python
import time
import boto3
from botocore.config import Config配置重试策略和退避机制
config = Config(
retries={
'max_attempts': 5, # 增加重试次数
'mode': 'adaptive' # 自适应重试模式
}
)bedrock_runtime = boto3.client('bedrock-runtime', config=config)
def invoke_model_with_backoff(prompt, max_retries=5):
for attempt in range(max_retries):
try:
response = bedrock_runtime.invoke_model(
modelId='claude-sonnet-4-5-20250929',
body=json.dumps({"prompt": prompt})
)
return response
except Exception as e:
if 'ThrottlingException' in str(e):
# 指数退避:2^attempt 秒
wait_time = 2 attempt
print(f"第{attempt+1}次重试,等待{wait_time}秒...")
time.sleep(wait_time)
else:
raise e
raise Exception("达到最大重试次数,请求失败")
预防措施
建议:在生产环境中使用令牌桶算法或漏桶算法来控制请求速率,避免突发流量触发限流。