Bedrock Runtime: InvokeModel, exceeded maximum number of attempts, 3, https resp...

2025年11月13日 22:47 状态: processing

🚨 错误信息

错误:模型 claude-sonnet-4-5-20250929: InvokeModel: operation error Bedrock Runtime: InvokeModel, exceeded maximum number of attempts, 3, https response error StatusCode: 429, RequestID: 56f4cc2f-6ec3-4d93-a4be-c645eb6feddb, ThrottlingException: Too many requests, please wait before trying again. (request id: 202511132247159064348692NXjPljB) (request id: 20251113224713284791144H6gBRXcN) (request id: 20251113224713279439439buoonGXl) (request id: 202511132247026039415342d6BK2ho)

🤖 AI解决方案

错误翻译

原文:ThrottlingException: Too many requests, please wait before trying again.
译文:限流异常:请求过多,请稍后再试。

原因分析

  • 根本原因:AWS Bedrock服务的API调用频率超过了限制阈值,触发了速率限制(Rate Limiting)

  • 具体位置:在调用claude-sonnet-4-5-20250929模型时,系统在3次重试后仍然收到429状态码响应
  • 解决方案

    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("达到最大重试次数,请求失败")

    预防措施

  • 实现指数退避算法:在重试时使用递增的等待时间(如1s, 2s, 4s, 8s...)

  • 监控API使用量:定期检查AWS Bedrock服务的配额和使用情况,合理规划调用频率

建议:在生产环境中使用令牌桶算法或漏桶算法来控制请求速率,避免突发流量触发限流。