Errors
429 rate limit
The request was valid, the key is valid, and the balance is fine — but a limit was hit. 429 is the one error where retrying is the correct answer, provided you back off.
What causes it
- Concurrency: every open streaming request holds a slot. Ten parallel streams is ten slots, not one.
- Requests per minute: a burst after a queue drains is the classic trigger.
- Shared keys: several services using one key share one limit.
How to fix it
import random, time
from openai import OpenAI, RateLimitError
client = OpenAI(api_key="sk-...", base_url="https://todapi.work/v1")
def call(**kwargs):
delay = 0.5
for attempt in range(6):
try:
return client.chat.completions.create(**kwargs)
except RateLimitError:
if attempt == 5:
raise
time.sleep(delay + random.uniform(0, delay / 2))
delay = min(delay * 2, 8)
- Back off exponentially with jitter — synchronised retries recreate the burst.
- Cap concurrency in your worker pool; do not let the SDK open unlimited streams.
- If one key is the bottleneck, split work across keys instead of raising the retry count.
What not to do
Retrying immediately in a tight loop turns a short limit into a longer one, and every retry still costs you the round trip. Retries should be bounded — three to five attempts, then surface the error.
Should you retry?
Yes, with backoff. If it happens every day at the same hour, the fix is architectural, not a longer timeout.
FAQ
How long does a 429 last?
Usually milliseconds to a few seconds, because the limit is a window rather than a lock. A Retry-After header, when present, is the precise answer.
Is 429 the same as 402?
No. 402 means the prepaid balance is empty and retrying cannot help.