DeepSeek
DeepSeek API rate limits
Two numbers decide throughput on the gateway: how many requests you send, and how many streams you hold open. The second one surprises people, because a stream keeps its slot until the last token.
The two limits
| Limit | What consumes it | Symptom |
|---|---|---|
| Requests per window | Every call, streaming or not | Bursty 429s right after a queue drains |
| Concurrency | Each open request | 429s only on slow or streaming calls |
| Account balance | Spend, not rate | 402, and retrying cannot fix it |
Long-context generations hurt twice: more tokens billed, and a slot held for longer. If your p95 completion time is 40 seconds and you open 20 streams, you are consuming 20 slots for most of a minute.
429 vs 402 — the distinction people get wrong
- 429 is temporary. Back off, retry, it clears on its own.
- 402 is a billing state. The key stays blocked until the balance is topped up; a retry loop just burns log lines. See 402 explained.
Correct backoff
import random, time
from openai import OpenAI, RateLimitError, APIStatusError
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)
except APIStatusError as exc:
if exc.status_code == 402: # balance, not rate
raise
raise
Two details matter more than the numbers: jitter (so parallel workers do not retry in lockstep) and a ceiling (so a long outage does not become an unbounded wait).
Reducing pressure without buying more quota
- Cap concurrency explicitly. Most SDKs will happily open as many streams as you ask for; the limit is yours to enforce.
- Trim context. A 10k-token system prompt on every call costs tokens and stretches generation time, which holds the slot.
- Queue instead of burst. A small worker pool with a bounded queue produces fewer 429s than unbounded parallelism.
- Split across keys when one key is the bottleneck — balance is per account, limits are per key.
Retry policy in one line
Retry 429 and 5xx with exponential backoff plus jitter; never retry 401, 402, 403 or 404 — the request or the account state has to change first. Full table in 429 explained.
Streaming failures
A stream that dies mid-answer is not a clean error: you may have been billed for the tokens produced. Treat it as failed, discard the partial output unless you can resume, and retry the call. The mechanism is the same on every model — see 429 and retry policy.