pythonadvanced
OpenAI Batch API for Cost Reduction
Submit large workloads via the OpenAI Batch API for 50% cost reduction with async processing.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonintermediate
Batch Embedding Large Text Corpora
Embed thousands of documents efficiently by batching requests to the OpenAI Embeddings API.
Best for: corpus embedding
#openai#embeddings
pythonbeginner
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
Best for: prompt chaining
#langchain#openai
pythonintermediate
Async OpenAI Client in Python
Use the AsyncOpenAI client with asyncio to run concurrent chat completions without blocking.
Best for: concurrent LLM calls
#openai#async
pythonbeginner
OpenAI DALL-E Image Generation
Generate and save images using the DALL-E 3 API with quality and style control.
Best for: AI image creation
#openai#dall-e