When you need to run the same operation across thousands or millions of records — classifying customer feedback, generating embeddings for a product catalog, summarizing a document archive — making synchronous API calls one at a time is slow and expensive. The OpenAI Batch API is designed for this pattern: you submit up to 50,000 requests in a single JSONL file, the API processes them asynchronously within a 24-hour window, and you retrieve the results when the batch completes. The cost is 50% less than the equivalent synchronous calls, and the per-request rate limits are much higher since the API can schedule work across off-peak capacity.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
How the Batch API works
Prepare a JSONL input file
Each line in the file is a JSON object describing one request: a
custom_id you define, the HTTP method and url, and a body identical to what you’d send to the Chat Completions endpoint. The file can contain up to 50,000 lines and must be under 200 MB.Upload the file
Use
client.files.create with purpose="batch" to upload your JSONL file. The API returns a file ID you pass to the next step.Create the batch
Call
client.batches.create with the file ID, the target endpoint, and a completion window. Currently the only supported window is "24h".Poll for completion
Check the batch status periodically using
client.batches.retrieve. The status moves through validating → in_progress → completed (or failed). In practice, batches often complete well under the 24-hour limit.Creating a batch job
The example below classifies a list of customer reviews by sentiment. Each review becomes one request in the batch.Use
gpt-4o-mini for high-volume classification and extraction tasks. It is significantly cheaper than gpt-4o and handles most structured extraction and short-form generation tasks accurately.Polling for completion
After submitting a batch, poll the status until it transitions tocompleted or failed. The interval below starts at 60 seconds and doubles up to a cap — a simple exponential backoff that avoids hammering the API while still detecting completion quickly.
Retrieving and parsing results
The output file is a JSONL file where each line corresponds to one input request, keyed by yourcustom_id. Errors for individual requests appear in the error field rather than causing the whole batch to fail.
custom_id to match outputs back to inputs.
Cost savings
50% off synchronous pricing
Every request in a batch costs half the price of the equivalent synchronous API call. For large workloads, this compounds quickly — a job that would cost 50 via the Batch API.
Higher throughput
Batch jobs are not subject to the same per-minute token limits as synchronous calls. The API schedules them across available capacity, enabling much higher effective throughput for large datasets.
Ideal use cases
Sentiment classification
Classify thousands of customer reviews, support tickets, or social mentions by sentiment or category. Each record maps cleanly to one batch request.
Embedding generation
Generate embeddings for a large document corpus or product catalog to build a search index. The Batch API supports the
/v1/embeddings endpoint alongside chat completions.Document summarization
Summarize a large collection of articles, research papers, or support threads overnight. Retrieve a complete set of summaries the next morning.
Data extraction
Extract structured fields from thousands of invoices, contracts, or forms. Combine with Structured Outputs for schema-validated results at scale.
Full end-to-end example
The snippet below puts all the steps together in a single reusable function:Next steps
Data extraction
Learn how to extract structured data from documents — combine with the Batch API for large-scale document processing pipelines.
Structured Outputs
Add response schemas to batch requests so each result is validated against a Pydantic model or JSON Schema.