Sub topic 1
Why batch? Synchronous calls versus asynchronous jobs
Prerequisites
This chapter assumes you have the following:
- Basic Python, including file I/O and JSON.
- Experience calling an LLM chat or messages API.
- An idea of what JSON Lines is (one JSON object per line).
- A basic sense of rate limits and tokens.
You do not need an API key to read this chapter. Later chapters build and submit a real batch.
Two ways to call a model
A synchronous call sends one request and waits for the answer on the same connection. Your code blocks until the model replies, usually within seconds. Chat interfaces, autocomplete, and agents all work this way because a person or a program is waiting.
A batch job works differently. You package many requests into one file, upload it, and ask the provider to run them later. The provider processes the requests when it has spare capacity. You check back, then download a file of results. The provider promises a completion window, for example 24 hours with the OpenAI Batch API, but not a specific response time.
What changes when you go asynchronous
Batch processing changes several things in your code and your design.
| Aspect | Synchronous call | Batch job |
|---|---|---|
| Response time | Seconds | Minutes to the full completion window |
| Unit of work | One request | A file of many requests |
| Result delivery | Returned in the response | Downloaded later from an output file |
| Result order | Tied to the call | Not guaranteed, so you match by ID |
| Failures | One error per call | Some requests fail while others succeed |
| Cost | Standard price | Typically discounted (OpenAI documents 50%) |
| Rate limits | Per-minute limits apply | Separate, larger queue limits |
Check your provider's current pricing and limits. They change over time.
Two consequences matter most. First, you lose the ability to react to a single result inside a request. You cannot hold an open connection and stream tokens. Second, a job can partly succeed. Your code must read results, find the failures, and deal with them. Later chapters cover that.
Choosing between them
Use a simple test. Ask whether anyone or anything needs the answer within the next few minutes.
- If yes, use synchronous calls. Examples are a support chatbot, a code assistant, or a form that validates input as the user types.
- If no, and you have many independent requests, consider a batch. Examples are labeling a backlog of 200,000 support tickets, summarizing an archive, or running an evaluation set against a new prompt.
Three conditions favor batch:
- The requests are independent. No request needs the output of another.
- The deadline is hours, not seconds.
- The volume is large enough that a discount or higher throughput is worth the extra code.
For a few dozen requests, a plain loop is simpler. For a chain of dependent calls, such as an agent that decides its next step from the last answer, batching each step adds waiting time for little gain. You can still batch such a pipeline in stages, with one batch per stage, if every item in a stage is independent.
A mental model of the workflow
Every batch workflow in this tutorial follows the same five steps:
- Write one request per line to a JSONL file, each with a unique
custom_id. - Upload the file and create the batch job.
- Poll the job until it reaches a terminal status.
- Download the output and error files.
- Match results to inputs by
custom_idand handle failures.
The remaining chapters take these steps one at a time, then cover cost, limits, other providers, and a restartable design.
Common mistakes at this stage
- Using batch for work a user is waiting on. A job that usually finishes in an hour can still take the full window.
- Assuming results come back in input order. They do not, so the ID is the only reliable link.
- Treating a completed job as proof that every request worked. A job can finish with some requests failed.
Exercise
A team must tag 500,000 archived product reviews with a sentiment label by next Monday. Separately, the same team runs a live chat widget that answers customers. Which workload suits a batch API, and why? Name one risk you must handle in your code.
Show answer
The review tagging suits batch. The requests are independent, the deadline is days away, and the volume is large, so the discount and throughput matter. The chat widget needs answers in seconds and must stay synchronous. A risk to handle: a completed batch may contain failed requests, and results are not in input order, so you must match results to inputs by custom_id and resubmit failures.