匠人学院 JR Academy
Skip to article
Learning guide
Claude API Guide

Build with the Claude API for messages, streaming, multimodal input, and production integrations.

3 chapters·9 guides·Access varies by chapter
Batch API
On this page

Claude Message Batches

Use Message Batches for independent jobs that do not need an immediate interactive answer. A batch is processed asynchronously; its overall completion does not mean every item succeeded.

#Create a batch

This Python example requires the anthropic package, an authorized API key and ANTHROPIC_MODEL. Creating a batch starts real processing and may incur charges.

python
import os from anthropic import Anthropic client = Anthropic() batch = client.messages.batches.create(requests=[{ "custom_id": "sample-001", "params": { "model": os.environ["ANTHROPIC_MODEL"], "max_tokens": 128, "messages": [{"role": "user", "content": "Define a Python iterator."}], }, }]) print(batch.id, batch.processing_status)

Retain the batch ID and a durable mapping from custom_id to the original input. Do not create another batch simply because a client timed out; reconcile the first operation first.

#Retrieve status and results

The following fragment uses the same client and saved batch.id:

python
status = client.messages.batches.retrieve(batch.id) if status.processing_status == "ended": for item in client.messages.batches.results(batch.id): print(item.custom_id, item.result.type)

Poll with a bounded interval and a deadline. Match results by custom_id, because output order is not guaranteed. Handle succeeded, errored, canceled and expired separately. Retry only eligible failed items after recording their original outcome.

Batch size, retention, processing window and discounts can change. Check the official reference and your workspace's rate/spend limits before submitting a large job. Cancellation can still leave already processed items; it is not proof of zero usage.

Batch processing↗ · Batches API↗

Published by
JR Academy
Learning resources

Individual authors and reviewers are credited where recorded.

System Design

Core system design concepts and practical case studies

Learn the trade-offs and patterns that matter in technical interviews.

Open System Design →

Related Roadmaps

Done reading? Next steps