
Thousands of Requests? Batch Them Instead of Racing the Rate Limit
- كاتب موثّق
- Claude API
محتويات المقال
asyncio script with 50 concurrent requests, tuned to sit just under your organization's rate limit, and start it after lunch. By three o'clock the customer-facing support chat, running on the same model in the same organization, is answering some users with errors: 429s that weren't there in the morning. The script finishes in the evening, and the invoice shows every token at full price.
The common misconception
More concurrency means more throughput, and the rate limit looks like the only ceiling. The SDK already retries 429s with backoff, so pushing right up against the limit feels safe. "Batch API" sounds like an overnight job from another era, slow by design. A parallel script shows progress immediately, and you can stop it the moment something looks wrong.
What actually happens
Parallel calls share everything with production
A batch is one asynchronous job
custom_id and the usual params, processed asynchronously "with each request handled independently". The docs' numbers:
Size: up to 100,000 requests or 256 MB per batch, whichever comes first. Price: all usage at 50% of standard prices, which makes Claude Opus 5.5 $2 input and $10 output per million tokens. The discount covers input, output and "any special tokens". Time: most batches complete within an hour. Results are available when every request has finished or after 24 hours, and requests not processed by then expire. Limits: batches have their own rate limits, and "Usage of the Batches API does not affect rate limits in the Messages API." Retention: results stay downloadable for 29 days after the batch is created.
output-300k-2026-03-24 beta header, a batched Claude Opus 5.5 request can set max_tokens up to 300,000, where the synchronous limit is 128K.
Each request gets its own result
processing_status turns ended, every request has one of four results: succeeded with the message, errored, canceled or expired. You aren't billed for the last three. An errored result with an invalid_request_error needs a fixed request; other errors can be resent as they are. One failure doesn't affect the other requests in the batch.
Relabeling the tickets
import asyncioimport anthropicclient = anthropic.AsyncAnthropic()limit = asyncio.Semaphore(50) # tuned to sit just under our rate limitasync def label(ticket: dict) -> str: async with limit: reply = await client.messages.create( model="claude-opus-5-5", max_tokens=256, messages=[{"role": "user", "content": prompt_for(ticket)}], ) return next(b.text for b in reply.content if b.type == "text")async def main(): labels = await asyncio.gather(*(label(t) for t in TICKETS)) print(len(labels), "labels, every token at the standard price")asyncio.run(main())import anthropicclient = anthropic.Anthropic()batch = client.messages.batches.create(requests=[ { "custom_id": f"ticket-{t['id']}", "params": { "model": "claude-opus-5-5", "max_tokens": 256, "messages": [{"role": "user", "content": prompt_for(t)}], }, } for t in TICKETS # up to 100,000 requests or 256 MB per batch])print(batch.id, batch.processing_status) # in_progress, then endedlabels, to_fix, to_resend = {}, [], []for item in client.messages.batches.results(BATCH_ID): ticket_id = item.custom_id.removeprefix("ticket-") result = item.result if result.type == "succeeded": blocks = result.message.content labels[ticket_id] = next(b.text for b in blocks if b.type == "text") elif result.type == "errored" and \ result.error.error.type == "invalid_request_error": to_fix.append(ticket_id) # resending it unchanged fails again else: # other errors, canceled, expired: resend as they are to_resend.append(ticket_id)print(len(labels), len(to_fix), len(to_resend)) # labeled, fix, resendstream, fast mode speed, fallbacks, max_tokens: 0 |
How to apply it in your project
Sort your workloads by who's waiting Anything a user waits on stays on the Messages API. Evaluations, backfills, nightly summaries and bulk classification are batch candidates. Dry-run one request Send a single request through the Messages API first, as the docs suggest, so a validation error doesn't come back 100,000 times. Design the custom_id Use an ID you can map back to your data, such as ticket-1042, and never rely on result order.Handle every result type Fix invalid_request_errorrequests, resend the other failures and expired ones, and keep the results within 29 days.Watch the 24-hour window Split very large jobs into several batches, and check request_countsso expirations don't go unnoticed.
Key takeaways
Parallel requests pay full price and share your organization's rate limits with production. The Message Batches API bills all usage at 50% of standard prices and has its own rate limits. Most batches finish within an hour, but plan for up to 24 hours: batches are for work nobody waits on. Results arrive in any order; match them by custom_id, never by position.Errored, canceled and expired requests aren't billed, but they still need your code to fix or resend them.
التعليقات (0)