|
| 1 | +# Batches API Reference |
| 2 | + |
| 3 | +Process multiple messages asynchronously in batches for high-throughput use cases with 50% cost reduction. |
| 4 | + |
| 5 | +## Create Batch |
| 6 | + |
| 7 | +```python { .api } |
| 8 | +def create( |
| 9 | + self, |
| 10 | + *, |
| 11 | + requests: list[MessageBatchIndividualRequest], |
| 12 | + **kwargs |
| 13 | +) -> MessageBatch: |
| 14 | + """ |
| 15 | + Create a batch of message requests. |
| 16 | +
|
| 17 | + Parameters: |
| 18 | + requests: List of individual message requests with custom_id |
| 19 | +
|
| 20 | + Returns: |
| 21 | + MessageBatch with id, processing_status, request counts |
| 22 | + """ |
| 23 | + ... |
| 24 | + |
| 25 | +async def create(...) -> MessageBatch: ... |
| 26 | +``` |
| 27 | + |
| 28 | +## Retrieve Batch |
| 29 | + |
| 30 | +```python { .api } |
| 31 | +def retrieve( |
| 32 | + self, |
| 33 | + message_batch_id: str, |
| 34 | + **kwargs |
| 35 | +) -> MessageBatch: |
| 36 | + """Retrieve batch status and metadata.""" |
| 37 | + ... |
| 38 | + |
| 39 | +async def retrieve(...) -> MessageBatch: ... |
| 40 | +``` |
| 41 | + |
| 42 | +## List Batches |
| 43 | + |
| 44 | +```python { .api } |
| 45 | +def list( |
| 46 | + self, |
| 47 | + *, |
| 48 | + before_id: str = NOT_GIVEN, |
| 49 | + after_id: str = NOT_GIVEN, |
| 50 | + limit: int = NOT_GIVEN, |
| 51 | + **kwargs |
| 52 | +) -> SyncPage[MessageBatch]: |
| 53 | + """List batches with pagination.""" |
| 54 | + ... |
| 55 | + |
| 56 | +def list(...) -> AsyncPage[MessageBatch]: ... |
| 57 | +``` |
| 58 | + |
| 59 | +## Cancel Batch |
| 60 | + |
| 61 | +```python { .api } |
| 62 | +def cancel( |
| 63 | + self, |
| 64 | + message_batch_id: str, |
| 65 | + **kwargs |
| 66 | +) -> MessageBatch: |
| 67 | + """Cancel a batch in progress.""" |
| 68 | + ... |
| 69 | + |
| 70 | +async def cancel(...) -> MessageBatch: ... |
| 71 | +``` |
| 72 | + |
| 73 | +## Delete Batch |
| 74 | + |
| 75 | +```python { .api } |
| 76 | +def delete( |
| 77 | + self, |
| 78 | + message_batch_id: str, |
| 79 | + **kwargs |
| 80 | +) -> DeletedMessageBatch: |
| 81 | + """Delete a batch.""" |
| 82 | + ... |
| 83 | + |
| 84 | +async def delete(...) -> DeletedMessageBatch: ... |
| 85 | +``` |
| 86 | + |
| 87 | +## Get Results |
| 88 | + |
| 89 | +```python { .api } |
| 90 | +def results( |
| 91 | + self, |
| 92 | + message_batch_id: str, |
| 93 | + **kwargs |
| 94 | +) -> JSONLDecoder[MessageBatchIndividualResponse]: |
| 95 | + """Stream batch results as JSONL.""" |
| 96 | + ... |
| 97 | + |
| 98 | +def results(...) -> AsyncJSONLDecoder[MessageBatchIndividualResponse]: ... |
| 99 | +``` |
| 100 | + |
| 101 | +## Response Types |
| 102 | + |
| 103 | +```python { .api } |
| 104 | +class MessageBatch(BaseModel): |
| 105 | + """Batch metadata and status.""" |
| 106 | + id: str |
| 107 | + type: Literal["message_batch"] |
| 108 | + processing_status: Literal["in_progress", "canceling", "ended"] |
| 109 | + request_counts: MessageBatchRequestCounts |
| 110 | + ended_at: str | None |
| 111 | + created_at: str |
| 112 | + expires_at: str |
| 113 | + cancel_initiated_at: str | None |
| 114 | + results_url: str | None |
| 115 | + |
| 116 | +class MessageBatchRequestCounts(BaseModel): |
| 117 | + """Request count statistics.""" |
| 118 | + processing: int |
| 119 | + succeeded: int |
| 120 | + errored: int |
| 121 | + canceled: int |
| 122 | + expired: int |
| 123 | +``` |
| 124 | + |
| 125 | +## Request Types |
| 126 | + |
| 127 | +```python { .api } |
| 128 | +class MessageBatchIndividualRequest(TypedDict): |
| 129 | + """Individual request in batch.""" |
| 130 | + custom_id: str |
| 131 | + params: MessageCreateParams |
| 132 | +``` |
| 133 | + |
| 134 | +## Quick Examples |
| 135 | + |
| 136 | +### Create Basic Batch |
| 137 | + |
| 138 | +```python |
| 139 | +batch = client.messages.batches.create( |
| 140 | + requests=[ |
| 141 | + { |
| 142 | + "custom_id": "request-1", |
| 143 | + "params": { |
| 144 | + "model": "claude-sonnet-4-5-20250929", |
| 145 | + "max_tokens": 1024, |
| 146 | + "messages": [{"role": "user", "content": "What is AI?"}] |
| 147 | + } |
| 148 | + }, |
| 149 | + { |
| 150 | + "custom_id": "request-2", |
| 151 | + "params": { |
| 152 | + "model": "claude-sonnet-4-5-20250929", |
| 153 | + "max_tokens": 1024, |
| 154 | + "messages": [{"role": "user", "content": "What is ML?"}] |
| 155 | + } |
| 156 | + } |
| 157 | + ] |
| 158 | +) |
| 159 | +print(f"Batch ID: {batch.id}") |
| 160 | +``` |
| 161 | + |
| 162 | +### Check Status |
| 163 | + |
| 164 | +```python |
| 165 | +batch = client.messages.batches.retrieve("batch_abc123") |
| 166 | +print(f"Status: {batch.processing_status}") |
| 167 | +print(f"Succeeded: {batch.request_counts.succeeded}") |
| 168 | +print(f"Errored: {batch.request_counts.errored}") |
| 169 | +``` |
| 170 | + |
| 171 | +### Poll Until Complete |
| 172 | + |
| 173 | +```python |
| 174 | +import time |
| 175 | + |
| 176 | +while True: |
| 177 | + batch = client.messages.batches.retrieve(batch_id) |
| 178 | + if batch.processing_status == "ended": |
| 179 | + break |
| 180 | + time.sleep(60) |
| 181 | +``` |
| 182 | + |
| 183 | +### Get Results |
| 184 | + |
| 185 | +```python |
| 186 | +results = client.messages.batches.results("batch_abc123") |
| 187 | + |
| 188 | +for response in results: |
| 189 | + if response.result.type == "succeeded": |
| 190 | + print(f"{response.custom_id}: {response.result.message.content[0].text}") |
| 191 | + elif response.result.type == "errored": |
| 192 | + print(f"{response.custom_id}: Error - {response.result.error.message}") |
| 193 | +``` |
| 194 | + |
| 195 | +## See Also |
| 196 | + |
| 197 | +- [Messages API](./messages.md) - Core message creation |
| 198 | +- [Batch Processing Guide](../guides/batch-processing.md) - Advanced batch patterns |
| 199 | +- [Type System](../reference/types.md) - Complete type definitions |
0 commit comments