AI / Core OpenAI Codex Application Fundamentals Interview Questions
What is the Batch API and when should you use it?
The Batch API allows you to submit many API requests asynchronously in a single file, receive results up to 24 hours later, and pay approximately 50% less than standard synchronous pricing. It is designed for large-scale, non-time-sensitive workloads.
from openai import OpenAI import jsonlines client = OpenAI() # 1. Create a JSONL file with requests batch_requests = [ { "custom_id": "review-001", "method": "POST", "url": "/v1/responses", "body": { "model": "gpt-5.5", "input": "Review: def fib(n): return fib(n-1) + fib(n-2)", "max_output_tokens": 500, } }, # ... thousands more requests ] # Write JSONL with open("batch_input.jsonl", "w") as f: for req in batch_requests: f.write(json.dumps(req) + "\n") # 2. Upload the file batch_file = client.files.create( file=open("batch_input.jsonl", "rb"), purpose="batch" ) # 3. Create the batch batch = client.batches.create( input_file_id=batch_file.id, endpoint="/v1/responses", completion_window="24h", ) print(f"Batch ID: {batch.id}") # 4. Poll for completion (or use webhooks) batch_status = client.batches.retrieve(batch.id) if batch_status.status == "completed": results = client.files.content(batch_status.output_file_id) # Parse JSONL results
| Aspect | Synchronous | Batch API |
|---|---|---|
| Cost | Standard pricing | ~50% reduction |
| Latency | Real-time (<1 min typical) | Up to 24 hours |
| Rate limits | Counts against RPM/TPM | Separate batch limits |
| Use cases | Interactive apps, real-time tools | Evals, dataset generation, bulk analysis |
| Max requests per batch | N/A | Up to 50,000 requests |
More Related questions...