Sub topic 12
Comparing batch APIs across providers
This chapter assumes you have run the OpenAI Batch API example from earlier chapters. You should be comfortable with custom_id, the batch statuses, and the output and error files. Here you will see where other providers' batch APIs differ, and how to keep that difference out of your pipeline logic.
What stays the same
The major providers follow the same model. You submit many requests as one job, the job runs asynchronously, and you collect results later. Each request carries your own custom_id. Batch pricing is discounted, typically about 50% off synchronous prices. Results are not guaranteed to arrive in input order, so you match them by custom_id.
Where they differ
| Aspect | OpenAI Batch API | Anthropic Message Batches | Google Gemini Batch |
|---|---|---|---|
| Input | Uploaded JSONL file | List of requests in the create call | Inline requests or an uploaded JSONL file |
| Request shape | `custom_id`, `method`, `url`, `body` | `custom_id`, `params` | Key per request plus a request body |
| Completion signal | Status `completed` | `processing_status` of `ended` | A succeeded job state |
| Result retrieval | Output file and separate error file | One result stream, one entry per request | Output file or inline responses |
| Per-request outcome | HTTP status and `error` field | `succeeded`, `errored`, `canceled`, or `expired` | Per-response error or response |
Limits, windows, and model support change often. Check each provider's current documentation before you size jobs.
The practical differences
- One result list versus two files. OpenAI separates successes from errors in two files. Anthropic returns one stream where each entry has a result type. Your collection code must handle both.
- A request fails in different ways. In OpenAI, a request can land in the error file, or appear in the output file with a non-200 status. In Anthropic, an
expiredresult means the job hit its time limit before that request ran. It belongs in your retry set. - Different request shapes. OpenAI wraps a full endpoint call. Anthropic takes only the message parameters. Do not reuse request-building code unchanged.
- Different terminal states. Write a small mapping from each provider's status to your own states, such as
runninganddone.
Example: the same job on Anthropic
This code submits the two documents from the OpenAI example, then reads results by custom_id.
import time
import anthropic
client = anthropic.Anthropic()
documents = {
"doc-001": "The meeting moved to Thursday because of a venue conflict.",
"doc-002": "Quarterly revenue grew 4% while costs stayed flat.",
}
requests = [
{
"custom_id": custom_id,
"params": {
"model": "claude-sonnet-4-5",
"max_tokens": 100,
"system": "Summarize the text in one sentence.",
"messages": [{"role": "user", "content": text}],
},
}
for custom_id, text in documents.items()
]
batch = client.messages.batches.create(requests=requests)
while batch.processing_status != "ended":
time.sleep(60)
batch = client.messages.batches.retrieve(batch.id)
results, failed = {}, {}
for entry in client.messages.batches.results(batch.id):
if entry.result.type == "succeeded":
results[entry.custom_id] = entry.result.message.content[0].text
else:
failed[entry.custom_id] = entry.result.type
missing = set(documents) - set(results) - set(failed)
print(len(results), "succeeded;", len(failed), "failed;", len(missing), "missing")The last three lines match the OpenAI version. Only the submission and parsing differ.
Isolating provider code
Put each provider behind a small adapter. The adapter builds requests, submits a job, reports a normalized status, and yields (custom_id, text_or_None, error_or_None) records. The rest of the pipeline, including retries and storage, then sees one shape.
Exercise
Your pipeline reads the OpenAI error file to find failed requests. You add Anthropic support. Which result types should feed your retry set, and why is checking only for errors not enough?
Show answer
Retry every result that is not succeeded, meaning errored, expired, and canceled, plus any custom_id that is absent from the results (the "missing" set). Checking only for errors misses expired and canceled requests, which never ran and so never produced an error message.