Bedrock Runtime: InvokeModelWithResponseStream, exceeded maximum number of attem...

2026年04月12日 20:00 processing

错误信息

InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, exceeded maximum number of attempts, 3, https response error StatusCode: 429, RequestID: a69a4e5d-a54a-4629-b839-e4dc4cb32a6c, ThrottlingException: Too many tokens per day, please wait before trying again. (request id: 2026041211174855714867LeFTQ0kB) (request id: 202604121117149429921547SmN8Xuz) (request id: 20260412111650720820295OU2vgy0d)

AI 解决方案

报错分析报告

1. 错误翻译


原文:ThrottlingException: Too many tokens per day, please wait before trying again.
译文:限流异常:每日令牌使用量超过上限,请等待后再试。

2.原因分析


  • 根本原因:调用Bedrock RuntimeInvokeModelWithResponseStream接口时,当日累计令牌消耗超过服务配额,触发限流(HTTP 429)。

  • 具体位置:API请求阶段(https response error StatusCode: 429),上下文是3次重试均因令牌超限失败。
  • 3. 解决方案


    需等待配额重置(通常次日零点)或优化令牌使用。以下是带退避重试的调用示例(以Python为例):
    import time
    import boto3
    from botocore.exceptions import ClientError
    
    bedrock = boto3.client('bedrock-runtime')
    
    def invoke_model_safely(model_id: str, prompt: str, max_retries: int = 3):
        retry_delay = 60  # 初始等待60秒(配额恢复需时间)
        for attempt in range(max_retries):
            try:
                response = bedrock.invoke_model_with_response_stream(
                    modelId=model_id,
                    body=prompt
                )
                return response
            except ClientError as e:
                if e.response['Error']['Code'] == 'ThrottlingException':
                    print(f"⚠️ 第{attempt+1}次限流,等待{retry_delay}秒后重试...")
                    time.sleep(retry_delay)
                    retry_delay *= 2  # 指数退避(避免再次冲击配额)
                else:
                    raise  # 非限流错误直接抛出
        raise Exception("❌ 超过最大重试次数,请检查令牌配额")
    
    # 使用示例
    try:
        result = invoke_model_safely(
            model_id="your-model-id",
            prompt='{"input": "test"}'
        )
    except Exception as e:
        print(f"调用失败:{e}")
    

    4. 预防措施


  • 配额监控:通过AWS CloudWatch设置Throttles指标警报,实时跟踪令牌使用量。

  • 令牌优化:缩短Prompt长度、复用历史结果,或拆分大请求为小批次(减少单次令牌消耗)。

  • 配额升级:若业务需高频调用,可通过AWS控制台申请提高Bedrock令牌配额。


  • :若需进一步分析代码中的具体调用逻辑,请补充完整代码片段。