Sub topic 11
Respecting limits and splitting large workloads
This chapter assumes you can already build a JSONL request file, submit a batch, and read its output and error files. A workload that fits in one file today will eventually outgrow it, so you need a way to split it.
The limits that matter
A batch API caps how much one job can hold. For the OpenAI Batch API at the time of writing, the main limits are:
| Limit | What it constrains |
|---|---|
| Requests per batch | A single input file holds at most 50,000 requests. |
| Input file size | A single input file may be at most 200 MB. |
| Enqueued tokens | Each model has a ceiling on tokens waiting in queued batches across your organization. |
Providers change these numbers, and the token ceiling depends on your account tier. Check the current documentation before you hard-code values. Keep the numbers in one configuration place so an update touches one line.
The first two limits apply to a single file, so you meet them while building files. The third applies across all your jobs, so you meet it while submitting. A batch that would push your queued tokens over the ceiling fails, and the batch object reports the reason.
Splitting by count and size
Chunk on both the request count and the byte size. Long prompts can reach the file-size cap long before 50,000 lines. Measure each encoded line, and start a new file when the next line would break either limit.
import json
MAX_REQUESTS = 50_000
MAX_BYTES = 190 * 1024 * 1024 # stay under the 200 MB cap
def write_chunks(requests, prefix="batch_part",
max_requests=MAX_REQUESTS, max_bytes=MAX_BYTES):
paths = []
f = None
count = size = 0
for request in requests:
line = (json.dumps(request) + "\n").encode("utf-8")
if f is None or count >= max_requests or size + len(line) > max_bytes:
if f:
f.close()
path = f"{prefix}_{len(paths):04d}.jsonl"
paths.append(path)
f = open(path, "wb")
count = size = 0
f.write(line)
count += 1
size += len(line)
if f:
f.close()
return paths
def make_request(doc_id, text):
return {
"custom_id": doc_id,
"method": "POST",
"url": "/v1/chat/completions",
"body": {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": text}],
"max_tokens": 100,
},
}
docs = ((f"doc-{i:06d}", f"Document number {i}") for i in range(120_000))
paths = write_chunks(make_request(k, v) for k, v in docs)
print(len(paths), "files:", paths)With 120,000 small requests this prints three files: two full ones and a remainder. The 190 MB budget leaves headroom, because it is safer to be slightly under a documented limit than exactly on it.
Keep custom_id independent of the chunk
Derive custom_id from the document, not from its position in a file. If you used line-17, the same id would appear in several chunks, and you could not tell which result belongs to which document. A stable id such as doc-000042 is unique across the whole workload. It also lets you move a document to a different chunk when you retry it.
Submitting chunks without hitting the token ceiling
You could create every batch at once, but the enqueued-token ceiling may reject the later ones. A safer pattern is to keep a small number of batches in flight. Submit a chunk, wait until an earlier one reaches a terminal status, then submit the next.
Record each chunk file name and the batch id it produced. If your script stops halfway, that record tells you which chunks are done, so you do not resubmit them.
Common errors
- Counting only requests and ignoring file size, so a chunk is rejected at upload.
- Sizing chunks by characters instead of encoded bytes. Non-ASCII text takes more bytes than characters.
- Submitting all chunks at once and exceeding the enqueued-token ceiling.
- Reusing the same
custom_idin different chunks.
Exercise
A workload has 30,000 requests, but each line averages 15 KB. Does it fit in one batch file, and how many files should you create with a 190 MB size budget?
Show answer
No. 30,000 × 15 KB is about 450 MB, which is over the 200 MB cap even though the count is under 50,000. At about 190 MB per file you need three files (roughly 12,700 requests each). The byte limit decides the split here, not the request count.