429 Too Many Requests。
维度
限流通常按 API 密钥与模型分组计量。具体额度与窗口以控制台中你的套餐为准。处理 429
收到429 时应退避后重试,采用指数退避 + 随机抖动,避免雪崩:
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
限流维度、429 处理与退避策略
429 Too Many Requests。
429 时应退避后重试,采用指数退避 + 随机抖动,避免雪崩:
import time, random
from openai import OpenAI, RateLimitError
client = OpenAI(base_url="https://tokendog.io/v1", api_key="YOUR_TOKENDOG_API_KEY")
def chat_with_retry(**kwargs):
for attempt in range(5):
try:
return client.chat.completions.create(**kwargs)
except RateLimitError:
time.sleep((2 ** attempt) + random.random())
raise RuntimeError("rate limited after retries")
