The Message Batches API supports the same set of features as the Messages API. While this page focuses on how to use the Message Batches API, see Messages API examples for examples of the Messages API featureset.
To poll a Message Batch, you’ll need its id, which is provided in the response when creating request or by listing batches. Example id: msgbatch_013Zva2CMHLNnXjNJJKqJ2EF.
import anthropicclient = anthropic.Anthropic()message_batch =NonewhileTrue: message_batch = client.messages.batches.retrieve( MESSAGE_BATCH_ID)if message_batch.processing_status =="ended":breakprint(f"Batch {MESSAGE_BATCH_ID} is still processing...") time.sleep(60)print(message_batch)
import anthropicclient = anthropic.Anthropic()# Automatically fetches more pages as needed.for message_batch in client.messages.batches.list( limit=20):print(message_batch)
Once your Message Batch status is ended, you will be able to view the results_url of the batch and retrieve results in the form of a .jsonl file.
import anthropicclient = anthropic.Anthropic()# Stream results file in memory-efficient chunks, processing one at a timefor result in client.messages.batches.results( MESSAGE_BATCH_ID,):print(result)
Immediately after cancellation, a batch’s processing_status will be canceling. You can use the same polling for batch completion technique to poll for when cancellation is finalized as canceled batches also end up ended and may contain results.