Sub topic 3
Understanding rate limits and 429 responses
This chapter assumes you can read Python, know what HTTP status codes and headers are, and have called an LLM API before. It explains what a rate limit is and how a 429 response signals one. It also covers what to do first when you receive one.
What a rate limit is
A rate limit is a cap a provider places on how much traffic your account can send in a window of time. Providers use limits to protect shared capacity and to keep one customer from starving the others.
LLM providers usually enforce two kinds of limit at the same time:
- Requests per minute (RPM) caps how many calls you make, no matter how small they are.
- Tokens per minute (TPM) caps the total tokens you send and are allowed to receive. A few huge prompts can hit this limit while your request count is still low.
You are limited by whichever cap you reach first. Some providers also apply daily limits, limits per model, or limits on concurrent requests. Exact names and numbers differ by provider and by account tier, so check your provider's dashboard or documentation.
How a 429 signals a limit
When you exceed a limit, the server answers with HTTP status 429, "Too Many Requests". The request was not processed. The error body usually says which limit you hit, and the response headers may tell you when to try again.
import httpx
def describe_rate_limit(resp: httpx.Response) -> str:
"""Summarize the useful parts of a 429 response."""
retry_after = resp.headers.get("retry-after")
try:
message = resp.json().get("error", {}).get("message", "")
except ValueError:
message = resp.text[:200]
return f"429 retry-after={retry_after!r} message={message!r}"
resp = httpx.Response(
429,
headers={"retry-after": "7"},
json={"error": {"message": "Rate limit reached for requests per minute"}},
)
print(describe_rate_limit(resp))The retry-after header gives a wait time. A later chapter covers it in detail. Some providers also send headers that report your remaining quota. The names vary, so look up the ones your provider uses.
Two meanings of 429
Not every 429 means the same thing. Read the error body before you decide how to react.
| Cause | Typical message | Does waiting help? |
|---|---|---|
| Short-term rate limit (RPM or TPM) | "Rate limit reached" | Yes. The window resets within a minute. |
| Exhausted quota or billing limit | "You exceeded your current quota" | No. You need to add credit or raise the plan. |
Some providers return the second case as a 429 too. Retrying it only wastes time, so surface it to a human instead.
First responses to a 429
- Treat a rate-limit 429 as retryable. Wait, then send the same request again.
- Use the
Retry-Aftervalue when it is present. Otherwise use exponential backoff, which later chapters build. - Check the message for quota or billing wording, and stop retrying if you find it.
- Reduce the cause. If TPM is the problem, shorten prompts or lower
max_tokens. If RPM is the problem, send fewer, larger requests or slow down.
A 429 also tells you about your design. If you see them often, retries are only hiding a client that sends too fast. A later chapter on client-side throttling shows how to stay under the limit.
Exercise
Your app sends 20 requests per minute, each with a 40,000-token prompt, and gets 429 errors even though the provider's RPM limit is 500. What is the likely cause, and what are two ways to fix it?
Show answer
The tokens-per-minute limit is probably the cause. 20 requests of 40,000 tokens is 800,000 tokens per minute, which can exceed the TPM cap while the RPM cap is far from reached. Fixes: shorten the prompts (or lower max_tokens), slow the request rate so the token total stays under the cap, or ask the provider for a higher limit.