Sub topic 1
Why Batch? Synchronous Calls Versus Asynchronous Jobs
Prerequisites
You should know basic Python, file I/O, JSON, JSON Lines, and the basics of calling an LLM API. You should also understand that API rate limits restrict how many requests you can send in a period.
Why Batch?
A synchronous call keeps the client waiting for one response before continuing. This model fits interactive features, such as a chatbot or a document editor that needs an answer immediately.
A batch API accepts many independent requests as one asynchronous job. The provider processes the job later and makes the results available in an output file. Your program can submit the work, record the job ID, and continue other tasks.
Workloads That Fit Offline Processing
Batch processing works well when results do not need to appear immediately. Typical examples include:
- Classifying a large archive of support tickets.
- Summarizing reports overnight.
- Extracting fields from invoices or contracts.
- Running evaluations across many prompts.
- Generating embeddings or labels for a dataset.
It is a poor fit for interactive requests, strict real-time decisions, or tasks where one result determines the next request. Batch requests should also be reasonably independent, because the provider may process them in any order.
Trading Latency for Lower Cost
Batch APIs usually trade turnaround time for lower cost and less pressure on real-time rate limits. A synchronous request might finish in seconds. A batch job may take minutes or longer, depending on the provider and completion window.
Estimate the trade-off before choosing:
| Factor | Synchronous API | Batch API |
|---|---|---|
| Response time | Usually immediate | Delayed |
| Cost | Standard pricing | Often discounted |
| Monitoring | Per request | Job status and files |
| Best use | Interactive work | Offline bulk work |
A batch is economical only when delayed results are acceptable. A small pilot can reveal actual token use, failure rates, and processing time before you submit the full dataset.
> callout note > Treat batch output as unordered. Always match each result to its input with a stable identifier, not with its line position.
Building the JSONL Request File
JSON Lines (JSONL) stores one complete JSON object on each line. Each request needs a unique custom_id, an HTTP method, an endpoint, and a provider-specific request body.
import json
documents = {
"doc-001": "The meeting moved to Thursday.",
"doc-002": "Quarterly revenue grew four percent.",
}
with open("batch_input.jsonl", "w", encoding="utf-8") as output:
for custom_id, text in documents.items():
request = {
"custom_id": custom_id,
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "Summarize the text in one sentence."},
{"role": "user", "content": text},
],
"max_tokens": 100,
},
}
output.write(json.dumps(request) + "\n")The endpoint and body must match the provider's batch specification. Keep the original input records so failed requests can be identified and retried later.
Exercise
Why should a batch pipeline use custom_id instead of assuming output lines match input lines?
Show answer
Providers may process requests in any order, so custom_id provides a stable way to correlate each result with its input.