You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure OpenAI调用GPT触发rate_limit_exceeded错误(RPM未达上限)

调用GPT模型时反复触发rate_limit_exceeded错误的排查

问题描述

调用GPT模型时连续多日触发rate_limit_exceeded错误,原本以为是触发了RPM(每分钟请求数)限制,但模型部署允许约100 RPM,当前实际RPM不足10,无法定位问题原因。

代码实现

from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

project_connection_string="mystring"

project = AIProjectClient.from_connection_string(
  conn_str=project_connection_string,
  credential=DefaultAzureCredential())


print ("测试AI代理服务 ")

import os
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import CodeInterpreterTool
from azure.identity import DefaultAzureCredential
from typing import Any
from pathlib import Path

# 从连接字符串创建Azure AI客户端,该字符串复制自您的Azure AI Foundry项目。
# 当前格式应为"<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
# 可通过访问discovery_url,移除开头的"https://"和结尾的"/discovery"来获取HostName
# 要查找discovery_url,请运行CLI命令:az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
# 项目连接示例:eastus.api.azureml.ms;12345678-abcd-1234-9fc6-62780b3d3e05;my-resource-group;my-project-name
# 用户需要通过Azure CLI登录Azure订阅并设置环境变量

project_client = project


with project_client:
    # 创建CodeInterpreterTool实例
    code_interpreter = CodeInterpreterTool()

    # 创建代理时需要包含CodeInterpreterTool
    agent = project_client.agents.create_agent(
        model="gpt-4o-mini-standardtriedhublevel",#"gpt-4o-mini",
        name="my-agent",
        instructions="您是一个乐于助人的代理",
        tools=code_interpreter.definitions,
        tool_resources=code_interpreter.resources,
    )
    print(f"已创建代理,代理ID: {agent.id}")

    # 创建会话线程
    thread = project_client.agents.create_thread()
    print(f"已创建会话线程,线程ID: {thread.id}")

    # 创建消息
    message = project_client.agents.create_message(
        thread_id=thread.id,
        role="user",
        content="能否请您根据以下数据创建一张营业利润的柱状图并提供文件给我?A公司:120万美元,B公司:250万美元,C公司:300万美元,D公司:180万美元",
    )
    print(f"已创建消息,消息ID: {message.id}")

    # 运行代理
    run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
    print(f"代理运行结束,状态: {run.status}")

    if run.status == "failed":
        # 检查是否出现"Rate limit is exceeded.",若是则需要申请更多配额
        print(f"运行失败: {run.last_error}")

    # 获取会话线程中的消息
    messages = project_client.agents.list_messages(thread_id=thread.id)
    print(f"消息: {messages}")

    # 获取发送者的最后一条消息
    last_msg = messages.get_last_text_message_by_sender("assistant")
    if last_msg:
        print(f"最后一条消息: {last_msg.text.value}")

    # 为柱状图生成图像文件
    for image_content in messages.image_contents:
        print(f"图像文件ID: {image_content.image_file.file_id}")
        file_name = f"{image_content.image_file.file_id}_image_file.png"
        project_client.agents.save_file(file_id=image_content.image_file.file_id, file_name=file_name)
        print(f"图像文件已保存至: {Path.cwd() / file_name}")

    # 打印消息中的文件路径
    for file_path_annotation in messages.file_path_annotations:
        print(f"文件路径:")
        print(f"类型: {file_path_annotation.type}")
        print(f"文本: {file_path_annotation.text}")
        print(f"文件ID: {file_path_annotation.file_path.file_id}")
        print(f"起始索引: {file_path_annotation.start_index}")
        print(f"结束索引: {file_path_annotation.end_index}")
        project_client.agents.save_file(file_id=file_path_annotation.file_path.file_id, file_name=Path(file_path_annotation.text).name)

    # 使用完成后删除代理
    project_client.agents.delete_agent(agent.id)
    print("已删除代理")

错误信息

测试AI代理服务 
已创建代理,代理ID: asst_ZhagK90WpAVEzfYNTD1Thebb
已创建会话线程,线程ID: thread_VXxeHYDgTd1Cl2Jn4pTlc0k2
已创建消息,消息ID: msg_gIvOE2mClicg0FfkfJ8eUC7A
代理运行结束,状态: RunStatus.FAILED
运行失败: {'code': 'rate_limit_exceeded', 'message': 'Rate limit is exceeded. Try again in 86400 seconds.'}
消息: {'object': 'list', 'data': [{'id': 'msg_gIvOE2mClicg0FfkfJ8eUC7A', 'object': 'thread.message', 'created_at': 1737400903, 'assistant_id': None, 'thread_id': 'thread_VXxeHYDgTd1Cl2Jn4pTlc0k2', 'run_id': None, 'role': 'user', 'content': [{'type': 'text', 'text': {'value': 'Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million', 'annotations': []}}], 'attachments': [], 'metadata': {}}], 'first_id': 'msg_gIvOE2mClicg0FfkfJ8eUC7A', 'last_id': 'msg_gIvOE2mClicg0FfkfJ8eUC7A', 'has_more': False}
已删除代理

配额情况

配额截图

当前使用情况

当前使用情况截图

问题原因分析

  1. 错误属于Azure AI Agents的日配额限制,而非模型RPM
    错误提示中Try again in 86400 seconds(24小时)是日配额耗尽的典型特征,和模型的分钟级RPM限制无关。Azure AI Agents服务有独立的配额体系,比如每日创建Agent的数量、每日执行Run的次数等,这些配额耗尽时会触发限流。

  2. 代码重复创建Agent的操作消耗日配额
    你的代码每次运行都会创建新Agent,使用后立即删除。即使单次请求的RPM很低,但如果每日运行次数较多,会快速消耗Agent创建的日配额,导致触发rate_limit_exceeded错误。

  3. 模型名称可能异常
    代码中指定的模型名gpt-4o-mini-standardtriedhublevel不符合标准命名格式,可能是自定义部署名但存在配置问题,导致请求路由到了配额不足的资源池,建议确认模型部署的正确名称。

  4. 检查Azure AI Agents专项配额
    登录Azure门户,进入你的AI项目,查看Agents服务的配额详情(比如Assistants Created Per Day、Runs Per Day等指标),确认是否已达到上限。

内容的提问来源于stack exchange,提问作者Manu Chadha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:33:10