pythonadvanced

OpenAI Batch API for Cost Reduction

Submit large workloads via the OpenAI Batch API for 50% cost reduction with async processing.

python
import json
from openai import OpenAI
from pathlib import Path

client = OpenAI()

# Prepare batch JSONL
requests = [
    {'custom_id': f'req-{i}', 'method': 'POST', 'url': '/v1/chat/completions',
     'body': {'model': 'gpt-4o-mini', 'messages': [{'role': 'user', 'content': f'Summarize: {text}'}], 'max_tokens': 100}}
    for i, text in enumerate(['Hello world.', 'AI is transforming industries.', 'Python is versatile.'])
]

batch_file = Path('batch_requests.jsonl')
batch_file.write_text('\n'.join(json.dumps(r) for r in requests))

with open(batch_file, 'rb') as f:
    file_obj = client.files.create(file=f, purpose='batch')

batch = client.batches.create(input_file_id=file_obj.id, endpoint='/v1/chat/completions', completion_window='24h')
print(f'Batch ID: {batch.id}, Status: {batch.status}')

Use Cases

  • batch inference
  • cost optimization
  • large-scale processing

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.