Skip to content

OpenAI 429 报错:rate_limit_exceeded 与 insufficient_quota 排查指南 ​

调用 OpenAI API 返回 HTTP 429 Too Many Requests 时,实际对应两种截然不同的原因:触发了每分钟请求/Token 速率限制(rate_limit_exceeded),或者账户余额/月度预算已耗尽(insufficient_quota)。前者应该退避重试,后者盲目重试只会死循环报错。

一句话结论

先看响应体 JSON 里的 error.code(或 error.type):

  • rate_limit_exceeded(RPM / TPM / RPD 限流):按响应头 retry-after 加指数退避 + 随机抖动重试,同时调低过大的 max_completion_tokens / max_output_tokens,避免提前占满 TPM 配额。
  • insufficient_quota(You exceeded your current quota):重试无效,应立刻停止并告警,检查 API 预付余额、组织月度硬上限或令牌额度上限。

先分清这两种 429 ​

两者 HTTP 状态码都是 429,如果代码只判断 status_code == 429 就重试,遇到欠费会一直空转。

关键字段 / 现象rate_limit_exceeded(临时限流)insufficient_quota(余额/配额耗尽)
error.coderate_limit_exceededinsufficient_quota
error.typerequests 或 tokensinsufficient_quota
常见提示片段Rate limit reached for ... Limit: ..., Used: ..., Requested: ...You exceeded your current quota, please check your plan and billing details.
是否应该重试可以,按 Retry-After / 退避重试不要重试,立刻中止并检查账单/余额

rate_limit_exceeded 典型响应:

json
{
  "error": {
    "message": "Rate limit reached for gpt-6-sol in organization org-xxx on tokens per min (TPM): Limit 30000, Used 28400, Requested 4200. Please try again in 5.2s.",
    "type": "tokens",
    "param": null,
    "code": "rate_limit_exceeded"
  }
}

insufficient_quota 典型响应:

json
{
  "error": {
    "message": "You exceeded your current quota, please check your plan and billing details.",
    "type": "insufficient_quota",
    "param": null,
    "code": "insufficient_quota"
  }
}

修法 1:按 error.code 分流并遵守 Retry-After ​

仅在 code == "rate_limit_exceeded" 时重试;优先读取响应头里的 retry-after,并叠加随机抖动(jitter)防止多并发同时撞墙。

python
import random
import time
from openai import OpenAI, RateLimitError

client = OpenAI()


def call_with_backoff(messages, model="gpt-6-sol", max_attempts=5):
    for attempt in range(max_attempts):
        try:
            return client.chat.completions.create(
                model=model,
                messages=messages,
                max_completion_tokens=1024,
            )
        except RateLimitError as exc:
            code = getattr(exc, "code", None)
            if code == "insufficient_quota":
                raise RuntimeError("余额或配额已用尽,停止重试。") from exc
            if attempt == max_attempts - 1:
                raise
            retry_after = exc.response.headers.get("retry-after") if exc.response else None
            delay = float(retry_after) if retry_after else min(2 ** attempt, 30)
            time.sleep(delay + random.uniform(0.1, 0.5))

修法 2:别用超大输出上限白白占满 TPM ​

OpenAI 会在请求发出前按 input_tokens + max_completion_tokens(Responses API 为 max_output_tokens)预扣 TPM 配额。如果你做一个只需输出 80 个 token 的分类任务,却把 max_completion_tokens 设成 32768,每次请求都会预占 32K TPM,极易提前触发 429。

  • 将 max_completion_tokens / max_output_tokens 设为该任务真实所需的合理上限。
  • 批量任务可根据响应头 x-ratelimit-remaining-requests、x-ratelimit-remaining-tokens、x-ratelimit-reset-tokens 主动限速。

修法 3:排查 insufficient_quota ​

如果 error.code 是 insufficient_quota:

  1. 确认 API 账户有预付余额(ChatGPT Plus/Pro 网页版订阅不包含 API 额度)。
  2. 检查组织后台的**月度消费硬上限(Hard limit)**和项目级预算上限。
  3. 排查终端环境变量里是否残留了旧的 OPENAI_API_KEY(参见 OpenAI 401 invalid_api_key)。
  4. 若通过聚合网关调用,另请排查网关账户余额与单个令牌的额度上限(参见 402 / insufficient_quota 排查)。

经由网关排查 ​

使用 半页酥 API(byesu) 时,可在渠道状态页实时查看各线路健康度,并在控制台核对账户总余额与单令牌额度设置。

遇到问题?联系客服或加交流群。