TTodAPI

Blog

Why streaming requests hit rate limits

You did not increase your request rate. You enabled streaming. And suddenly a share of calls return 429. This is not a coincidence — it is how concurrency is counted.

Two counters, not one

Rate limiting usually means two independent limits:

A non-streaming call is open for the duration of the generation. A streaming call is open for the duration of the generation plus however long the client takes to read the stream. Same request count, longer occupancy.

The arithmetic that bites

Suppose your p95 generation is 25 seconds non-streaming, and 35 seconds when streamed because the client reads slowly. With a concurrency limit of 20:

throughput ≈ concurrency / average duration

non-streaming: 20 / 25s ≈ 0.8 req/s   (~48/min)
streaming:     20 / 35s ≈ 0.57 req/s  (~34/min)

Same limit, 30% less throughput. The 429s are real: they are the limit doing its job, not a bug.

What makes it worse

Fixes, in order of effort

  1. Cap concurrency explicitly. A semaphore of 10 is better than an unbounded pool that trips the limit and retries.
  2. Close streams on abort. Wire the cancellation signal through; a cancelled generation should release the socket, not sit until timeout.
  3. Queue instead of burst. A bounded queue smooths the spike that caused the 429s in the first place.
  4. Keep answers shorter. max_tokens is both a cost control and a concurrency control.
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(api_key="sk-...", base_url="https://todapi.work/v1")
sem = asyncio.Semaphore(10)

async def one(prompt: str):
    async with sem:
        stream = await client.chat.completions.create(
            model="deepseek-v4.1-flash",
            messages=[{"role": "user", "content": prompt}],
            stream=True,
            max_tokens=400,
        )
        async for chunk in stream:
            delta = chunk.choices[0].delta.content if chunk.choices else None
            if delta:
                yield delta

Separating the two failures

429 from concurrency looks identical to 429 from request rate in the response body, so instrument both counters on your side: requests started, and streams currently open. Without the second number you will keep "fixing" the wrong limit.

And do not confuse either with 402 — that one is a balance state and will not clear no matter how patient you are.

FAQ

Does streaming count as more requests?

No. It counts as longer-held requests, which consumes concurrency for longer.

Why do only long answers get 429?

Because duration is the variable. Short answers release the slot quickly; long ones do not.

Will retrying help?

Briefly, with backoff — but if the cause is occupancy, retrying raises occupancy. Fix the client, not the loop.

429 reference

Backoff, limits and what not to do.

DeepSeek limits

Same mechanics on the DeepSeek routes.

429 vs 402

The other error people misread.