速率限制
速率限制可保护 API 免遭滥用,并确保所有用户公平访问。限制按 API Key 施加,并因账户等级而异。
各等级限制
| 等级 | RPM(请求/分钟) | TPM(token/分钟) | 并发请求数 |
|---|---|---|---|
| Free | 10 | 40,000 | 2 |
| Standard | 60 | 200,000 | 10 |
| Pro | 200 | 1,000,000 | 30 |
| Enterprise | 自定义 | 自定义 | 自定义 |
RPM = 每分钟请求数,TPM = 每分钟 token 数。两个限制独立生效——你可能先触发其中任意一个。
速率限制响应头
每个 API 响应都包含指示当前用量的响应头:
| 响应头 | 说明 |
|---|---|
x-ratelimit-limit-requests | 每分钟最大请求数 |
x-ratelimit-limit-tokens | 每分钟最大 token 数 |
x-ratelimit-remaining-requests | 当前窗口内剩余请求数 |
x-ratelimit-remaining-tokens | 当前窗口内剩余 token 数 |
x-ratelimit-reset-requests | 距请求限制重置的秒数 |
x-ratelimit-reset-tokens | 距 token 限制重置的秒数 |
处理速率限制
429 响应
当超出速率限制时,API 会返回 429 Too Many Requests 响应:
{
"error": {
"message": "Rate limit exceeded",
"type": "rate_limit_error",
"code": "rate_limit_exceeded"
}
}重试策略
收到 429 错误时,请实现指数退避:
import time
def api_call_with_retry(client, max_retries=5, **kwargs):
for attempt in range(max_retries):
try:
return client.chat.completions.create(**kwargs)
except Exception as e:
if "rate_limit" in str(e) and attempt < max_retries - 1:
wait_time = (2 ** attempt) + 1
time.sleep(wait_time)
else:
raise最佳实践
- 监控响应头 — 跟踪
x-ratelimit-remaining-*以提前预判限制 - 批量请求 — 尽可能通过批处理减少 API 调用次数
- 缓存响应 — 避免对相同内容重复请求
- 升级等级 — 联系销售获取更高限制
- 使用流式响应 — 流式响应会逐步消耗 token,降低突发 TPM
模型特定限制
部分高需求模型在峰值期间可能有较低的实际速率限制。请查看响应头获取实时限制。
升级
如需提高速率限制,请联系 support@linkwo.com 升级到更高等级。企业客户可申请自定义限制。