Topic 1 of 4 · 4 min read
How provider batch APIs work
Goal: Build a batch request file with unique custom ids and map unordered results back to their inputs.
A batch API takes one file of many independent requests, runs them on the provider's spare capacity within a window of up to 24 hours, and returns the results in no guaranteed order. Your job is to give every request an id you control, so you can match answers to inputs afterwards.
Check yourself · 1 of 3
A batch returns 500 results for 500 requests. Which field should you use to match each result to its input?
Why it matters
Real-time calls are priced for speed. If nobody is waiting for the answer, you are paying for something you do not use. Claude, OpenAI and Gemini all offer a batch mode for this, and the Claude documentation lists a 50% discount on batch tokens. The trade is time: a batch can take up to 24 hours, although the Claude docs say most finish within an hour.
How it works
- Build the requests. Each entry has a
custom_id(OpenAI's file format calls it the same) and the body you would normally send to the real-time endpoint: model, messages, parameters. - Submit. Claude takes the list in one API call. OpenAI takes an uploaded JSONL file, one request per line. You get back a batch id.
- Poll. The batch has a status such as in progress, ended or expired. Check it every few minutes, not every second.
- Collect. When processing ends, read the results. Each result carries the
custom_idof its request and either a success or an error.
Results can come back in any order. Never rely on position. The Claude docs say so directly and tell you to use meaningful custom_id values.
Before and after
Weak: ids that are just the loop index, and results zipped back by position.
requests = [
{"custom_id": str(i), "params": {"model": MODEL, "max_tokens": 200,
"messages": [{"role": "user", "content": doc}]}}
for i, doc in enumerate(docs)
]
# later: labels = [r.result for r in results] # assumes same order
Better: an id that names the input, checked for uniqueness, and a lookup by id.
requests = [
{"custom_id": f"ticket-{t.id}", "params": {"model": MODEL, "max_tokens": 200,
"messages": [{"role": "user", "content": t.body}]}}
for t in tickets
]
assert len({r["custom_id"] for r in requests}) == len(requests)
# later: by_id = {r.custom_id: r.result for r in results}
The index version breaks the first time a result is missing or reordered: every label after the gap is attached to the wrong ticket, and nothing raises an error.
What goes wrong
- Duplicate ids. A batch with repeated
custom_idvalues is rejected, or worse, one answer overwrites another in your dictionary. - Malformed requests found late. A bad parameter in one request may only show up as a per-request error after the batch ends. Send a few requests through the real-time endpoint first.
- Hitting the size cap. A Claude batch holds at most 100,000 requests or 256 MB, whichever comes first. Split larger jobs into several batches.
Examples
Ids that name the input
Use ticket-48213 or doc-2026-11-contract-17, not 0, 1, 2. When a result turns up, the id tells you what it belongs to without any lookup by position, and a log line with the id is searchable.
If the same ticket is reprocessed on purpose, add a run prefix such as v2-ticket-48213 so the two runs never collide.
Mapping results back
by_id = {}
for r in client.messages.batches.results(batch_id):
by_id[r.custom_id] = r.result
missing = [t.id for t in tickets if f"ticket-{t.id}" not in by_id]
The missing list is the check that catches dropped results. Without it a gap goes unnoticed.
Key points
- A batch is a list of independent requests, each with a unique custom id, submitted once and processed within a window of up to 24 hours.
- Results can come back in any order, so you match them to inputs by custom id and never by position.
- Status polling should be slow and scheduled; a batch ends as completed, partly failed or expired.
- Batches have size caps, such as 100,000 requests or 256 MB for Claude, so large jobs are split.
- Test a few requests in real time first, because a bad parameter can otherwise surface only after the batch ends.
Common mistakes
- Using the loop index as the custom id and zipping results back by position.
- Submitting thousands of requests without first sending three through the real-time endpoint to catch a bad parameter.
- Polling every second instead of on a schedule, which adds load and gains nothing.
- Putting more than the documented request or size limit in one batch.
Check yourself · 2 of 3
A team submits 150,000 short requests in one Claude batch and the call is rejected. What is the most likely reason?
Check yourself · 3 of 3
Your script zips the input list with the results list and every label after row 300 is wrong. What is the root cause?
Practice items, not counted towards your mastery.
Start here
- Batch processing (Claude) (opens in a new tab)DocsFree
Request format, custom_id, the 24-hour window, size limits, result handling and the batch discount.
- Batch API (OpenAI) (opens in a new tab)DocsFree
The same pattern with a JSONL input file, a status object and separate output and error files.
- Batch API (Gemini) (opens in a new tab)DocsFree
Gemini's asynchronous batch mode for large jobs, with its own submission and result steps.