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.
Creating a Message Batch
import anthropic
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
requests=[
Request(
custom_id="my-first-request",
params=MessageCreateParamsNonStreaming(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Hello, world",
}]
)
),
Request(
custom_id="my-second-request",
params=MessageCreateParamsNonStreaming(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Hi again, friend",
}]
)
)
]
)
print(message_batch)
Polling for Message Batch completion
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 anthropic
client = anthropic.Anthropic()
message_batch = None
while True:
message_batch = client.messages.batches.retrieve(
MESSAGE_BATCH_ID
)
if message_batch.processing_status == "ended":
break
print(f"Batch {MESSAGE_BATCH_ID} is still processing...")
time.sleep(60)
print(message_batch)
Listing all Message Batches in a Workspace
import anthropic
client = anthropic.Anthropic()
for message_batch in client.messages.batches.list(
limit=20
):
print(message_batch)
Retrieving Message Batch Results
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 anthropic
client = anthropic.Anthropic()
for result in client.messages.batches.results(
MESSAGE_BATCH_ID,
):
print(result)
Canceling a Message Batch
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.
import anthropic
client = anthropic.Anthropic()
message_batch = client.messages.batches.cancel(
MESSAGE_BATCH_ID,
)
print(message_batch)