LLM RATE LIMITS
Under the request limit, out of tokens, and the retries make it worse
Ojaline's job throttles itself to 50 requests a minute; the provider allows 60 requests, 30,000 input tokens and 6,000 output tokens a minute. Most requests are 600 input tokens. Requests 9 to 12 are 12,000-token category pages, sent one a second. What happens?
An LLM API limits three things per model class: requests, input tokens and output tokens per minute. Anthropic’s rate limits page (as of 11 November 2026) calls them RPM, ITPM and OTPM and enforces them with the token bucket from rate limiting: capacity “is continuously replenished up to your maximum limit, rather than being reset at fixed intervals”. A 429 names the limit you hit, and its retry-after header gives the seconds to wait; earlier retries fail.
A requests-per-minute throttle cannot see tokens. One 12,000-token request costs the same as twenty short ones. The fix is a client-side copy of all three buckets, a queue in front of it, and retries that wait as long as the provider asks.
limiter = RequestsPerMinute(50) # "the limit is 60 RPM, so 50 is safe"
for req in job:
limiter.wait()
for attempt in range(5):
try: return client.messages.create(**req)
except RateLimitError: continue # retry at once, ignore retry-afterbudget = Buckets(rpm=60, itpm=30_000, otpm=6_000) # your tier's limits, refilled per second
for req in fifo_queue: # head of line waits; nobody starves it
need = (1, count_tokens(req), req["max_tokens"])
budget.wait_until_covers(*need) # input counted, output reserved
for attempt in range(5):
try: return client.messages.create(**req)
except RateLimitError as e:
ra = float(e.response.headers.get("retry-after", 0))
sleep(ra + random.uniform(0, min(8, 2 ** attempt))) Watch the input-token bucket and the three invariants. SHORT REQUESTS is the test run, and it stays clean. In RPM THROTTLE, RETRY AT ONCE the job peaks at 50 requests a minute, gets 10 × 429 on input tokens, sends 8 retries before their retry-after, and drops 2 pages after 5 attempts in 5 seconds. HONOUR RETRY-AFTER sends nothing early and still drops page #12: each time it comes back, the short requests have already taken the refill. The FIX finishes all 48 with 0 × 429 by t = 94 s. That is close to the floor: 74,400 input tokens against a 30,000-a-minute limit cannot finish much before 90 s. With ANOTHER JOB spending a full minute of the shared limit, the fix gets one 429, resyncs from the response, waits out a 59-second retry-after, and still drops nothing.
This job of short requests runs clean under a 50-requests-a-minute throttle. Inject one event so the job gets a 429 while it still never sends more than 60 requests in a minute.
GOAL: at least one 429 and at most 60 requests in any minute
Rewrite tryAcquire, the job's own limiter. It is called once per second for the request at the head of a FIFO queue; return true to send it now, false to wait. The provider limits requests (60), input tokens (30,000) and output tokens (6,000) per minute, each a bucket that holds one minute's worth and refills continuously at limit ÷ 60 per second. It counts a request's input when it arrives and its output as it is generated. Your function cannot see the output in advance, so budget req.maxTokens for it. The grader runs bursts of short and long requests, an idle stretch, an output-heavy burst, and a burst either side of a minute boundary; it passes with zero 429s, every request finished, and the job no more than 25% slower than a correct three-bucket limiter.
What the sim simplifies. The limits are a toy tier at the same 5:1 input-to-output ratio as Anthropic’s Start tier for Claude Opus 5.5 (4,000 RPM, 2,000,000 ITPM and 400,000 OTPM on the page as read). Every request takes 3 seconds; the naive job sends at most one a second. The provider checks the request and input buckets when a request arrives and debits output when it finishes. Anthropic’s page says output is counted in real time as it is generated and max_tokens “does not factor into OTPM rate limit calculations”, so reserving max_tokens is your own safety margin, not the provider’s rule. Refund the unused part when the response’s usage.output_tokens comes back. Count input with the model’s tokeniser (tokenisation and the tokens chapter explain why), and note that on most Claude models cache reads do not count toward ITPM, so caching raises your throughput; prompt cache prefix shows how to make the reads happen.
Your local buckets drift, because other jobs and other workspaces in the organisation share the limit. Each response carries anthropic-ratelimit-requests-remaining, anthropic-ratelimit-input-tokens-remaining and anthropic-ratelimit-output-tokens-remaining (token counts rounded to the nearest thousand), with -reset times in RFC 3339. Resync from them, as the fix does. The retry rules themselves are in retries, backoff and jitter; the API chapter lists which errors to retry, including the spend-cap 429 that has no retry-after and must not be retried. Anthropic also warns of acceleration limits: a sharp jump in traffic can 429 on its own, so ramp a new job up.
Batch jobs over documents (summaries, extraction, embeddings, evals), RAG ingestion, agents that read long tool results, several services sharing one organisation's API key. Symptoms: 429s while request rate is under the limit, errors clustered on the longest inputs, a retry counter that climbs in seconds, a job that finishes everything except its biggest items, and a teammate's job failing when yours starts.
“You get 429s from an LLM API although you are under its requests-per-minute limit. Walk me through it.” Name the token limits (input and output per minute) and the token bucket. Then the fix: a client-side limiter on all three, input counted with the tokeniser and output reserved at max_tokens; a FIFO queue so long requests are not starved; honour retry-after with jittered backoff and a bounded number of attempts; resync from the rate-limit headers because the limit is shared. Mention caching, which on most Claude models does not count cache reads toward ITPM, and the Batch API for work that can wait.
USED IN
Chapters that build on this lesson.
- An AI platform for product teamsAI Engineer
- Cost, latency and choosing the modelAI Engineer
- Monitoring quality in productionAI Engineer