速率限制
速率限制可保护 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@linkastra.ai 升級到更高等級。企業客戶可申請自定義限制。