api429/429-survival-kit 是一个托管在 GitHub 上的开源项目,采用 MIT License 许可证。
仓库全名
api429/429-survival-kit
README 摘要
One key. No 429. («Один ключ. Без 429.») 429 Survival Kit Stop retry storms. Tell rate limits, quota caps and provider overloads apart, and handle each one correctly. Works with any OpenAI-compatible endpoint: OpenAI, local servers (llama.cpp server, vLLM, Ollama), other gateways and API429. Status: early preview. The copy-paste snippet below works today. The packaged Python and Node versions are being added, so…
Stop retry storms. Tell rate limits, quota caps and provider overloads apart, and handle each one correctly.
Works with any OpenAI-compatible endpoint: OpenAI, local servers (llama.cpp server, vLLM, Ollama), other gateways and API429.
Status: early preview. The copy-paste snippet below works today. The packaged Python and Node versions are being added, so star or watch the repo to get notified.
Why
An HTTP 429 can mean three different things:
“Slow down”: you sent too many requests per minute.
“You’re out of quota”: you hit a usage, billing or spend cap.
“The provider is overloaded”: the upstream is busy right now.
Most retry loops treat them all the same. That’s fine until one of them is a spend cap, and your workers spend the night retrying a request that can never succeed.
The three kinds of 429
Kind
What it means
What to do
Rate limit
Too many requests per minute or per day
Back off with jitter, then retry
Quota / spend cap
You hit a usage or billing cap
Do not retry. Alert a human
Overload
The upstream is busy
Back off, consider a fallback model
Quick start (copy-paste, Python)
Requires pip install openai. Set OPENAI_BASE_URL, OPENAI_API_KEY and MODEL for your endpoint. The SDK’s built-in retries are turned off, so this loop is the only one. The same code is in examples/quickstart.py.
import os, random, time
from openai import OpenAI, RateLimitError
client = OpenAI(
base_url=os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1"),
api_key=os.environ["OPENAI_API_KEY"],
max_retries=0, # we handle retries ourselves
)
QUOTA_WORDS = ("insufficient_quota", "quota", "billing", "spend", "credit", "balance")
OVERLOAD_WORDS = ("overloaded", "capacity", "server is busy")
def classify_429(err: RateLimitError) -> str:
"""Return 'QUOTA', 'OVERLOAD' or 'RATE_LIMIT' for a 429 error."""
body = (err.response.text or "").lower()
if any(w in body for w in QUOTA_WORDS):
return "QUOTA"
if any(w in body for w in OVERLOAD_WORDS):
return "OVERLOAD"
return "RATE_LIMIT"
def with_retries(call, budget_s=60.0, base_s=1.0, cap_s=20.0):
"""Retry rate limits and overloads with full jitter inside a total time budget."""
start, attempt = time.monotonic(), 0
while True:
try:
return call()
except RateLimitError as err:
if classify_429(err) == "QUOTA":
raise # retrying a spend cap only burns time: alert a human
retry_after = err.response.headers.get("retry-after", "")
try:
delay = float(retry_after)
except ValueError:
delay = random.uniform(0, min(cap_s, base_s * 2 ** attempt)) # full jitter
if time.monotonic() - start + delay > budget_s:
raise # retry budget spent
time.sleep(delay)
attempt += 1
reply = with_retries(lambda: client.chat.completions.create(
model=os.environ["MODEL"], # any model ID your endpoint lists in GET /v1/models
messages=[{"role": "user", "content": "Say hi in five words."}],
))
print(reply.choices[0].message.content)
Every provider words its error bodies differently, so treat QUOTA_WORDS and OVERLOAD_WORDS as a starting point and add what you see in your logs.
What’s coming in the package
classify_429() that reads status, headers and body and returns RATE_LIMIT, QUOTA or OVERLOAD
Backoff with full jitter and a total retry budget
One shared cooldown for all workers (no thundering herd)
A fallback model list per task
An async queue example for batch jobs (e.g. 10k images)
Python and Node versions, MIT license
Use it with any endpoint
The kit only needs a base URL and a key:
Endpoint
OPENAI_BASE_URL
OpenAI
https://api.openai.com/v1
llama.cpp server (local)
http://localhost:8080/v1
vLLM (local)
http://localhost:8000/v1
Ollama (local)
http://localhost:11434/v1
API429
https://gateway.api429.com/v1
Need one key for many models?
The kit is maintained by the team behind API429, an AI API gateway: one OpenAI-compatible key and one balance for GPT, Claude, Gemini, Grok, plus image, video and voice models, with public per-model prices.
评论