Batch API
Create Batch
Submit a batch job for asynchronous processing.
POST
Create Batch
Overview
The Batch API enables asynchronous processing of large volumes of requests at reduced costs. It’s ideal for:- Bulk content generation
- Large-scale data analysis
- Embedding generation for datasets
- Any task that doesn’t require immediate responses
Endpoints
File Management
Batch Operations
Authentication
File Upload
Upload JSONL files containing batch requests.Request Format
Endpoint:POST /v1/files
Headers:
Authorization: Bearer YOUR_API_KEY(required)Content-Type: multipart/form-data(required)
Response Format
JSONL Request Format
Each line in the JSONL file must be a valid JSON object:Request Fields
Create Batch
Submit a file for batch processing.Request Format
Endpoint:POST /v1/batches
Request Body:
Parameters
Response Format
Batch Status
Status Values
Retrieve Batch
Get the current status of a batch. Endpoint:GET /v1/batches/{batch_id}
Returns the same format as batch creation response with updated status and counts.
List Batches
List all batches with pagination support. Endpoint:GET /v1/batches
Query Parameters
Response Format
Cancel Batch
Cancel an in-progress batch. Endpoint:POST /v1/batches/{batch_id}/cancel
Returns the batch object with updated cancellation timestamps.
Output Format
Completed batches produce JSONL output files where each line contains:Usage Examples
Complete Workflow
Python Example
JavaScript Example
Best Practices
- Batch Size: Keep batches under 50,000 requests for optimal processing
- File Size: Ensure JSONL files are under 100MB
- Custom IDs: Use meaningful identifiers for easy tracking
- Validation: Validate JSONL format before uploading
- Polling: Check status every 30-60 seconds to avoid rate limits
- Error Handling: Process partial failures gracefully
- Cleanup: Delete processed files to manage storage
- Metadata: Use metadata fields for additional context and filtering
Limitations
- Maximum requests per batch: 50,000
- Maximum file size: 100MB
- Completion window: 24 hours
- Metadata entries: Maximum 16 key-value pairs
- Supported endpoints:
/v1/chat/completions,/v1/embeddings, and other compatible endpoints
Error Handling
The batch API automatically retries transient failures. Failed requests are included in the output file with error details, allowing you to:- Identify specific failures
- Retry failed requests
- Adjust parameters based on error messages
- Track success rates
Supported providers
OpenAI
Native batch API support with cost savings for large-scale processing.
Anthropic
Batch processing for Claude models with automatic retry handling.
Together.AI
Efficient batch inference for open-source models at scale.
Create Batch