Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build LLM-powered apps with the Anthropic Claude API or SDK across Python, TypeScript, Java, Go, Ruby, C#, and PHP.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
python/claude-api/batches.md
1# Message Batches API — Python23The Batches API (`POST /v1/messages/batches`) processes Messages API requests asynchronously at 50% of standard prices.45## Key Facts67- Up to 100,000 requests or 256 MB per batch8- Most batches complete within 1 hour; maximum 24 hours9- Results available for 29 days after creation10- 50% cost reduction on all token usage11- All Messages API features supported (vision, tools, caching, etc.)1213---1415## Create a Batch1617```python18import anthropic19from anthropic.types.message_create_params import MessageCreateParamsNonStreaming20from anthropic.types.messages.batch_create_params import Request2122client = anthropic.Anthropic()2324message_batch = client.messages.batches.create(25requests=[26Request(27custom_id="request-1",28params=MessageCreateParamsNonStreaming(29model="claude-opus-4-8",30max_tokens=16000,31messages=[{"role": "user", "content": "Summarize climate change impacts"}]32)33),34Request(35custom_id="request-2",36params=MessageCreateParamsNonStreaming(37model="claude-opus-4-8",38max_tokens=16000,39messages=[{"role": "user", "content": "Explain quantum computing basics"}]40)41),42]43)4445print(f"Batch ID: {message_batch.id}")46print(f"Status: {message_batch.processing_status}")47```4849---5051## Poll for Completion5253```python54import time5556while True:57batch = client.messages.batches.retrieve(message_batch.id)58if batch.processing_status == "ended":59break60print(f"Status: {batch.processing_status}, processing: {batch.request_counts.processing}")61time.sleep(60)6263print("Batch complete!")64print(f"Succeeded: {batch.request_counts.succeeded}")65print(f"Errored: {batch.request_counts.errored}")66```6768---6970## Retrieve Results7172> **Note:** Examples below use `match/case` syntax, requiring Python 3.10+. For earlier versions, use `if/elif` chains instead.7374```python75for result in client.messages.batches.results(message_batch.id):76match result.result.type:77case "succeeded":78msg = result.result.message79text = next((b.text for b in msg.content if b.type == "text"), "")80print(f"[{result.custom_id}] {text[:100]}")81case "errored":82if result.result.error.type == "invalid_request":83print(f"[{result.custom_id}] Validation error - fix request and retry")84else:85print(f"[{result.custom_id}] Server error - safe to retry")86case "canceled":87print(f"[{result.custom_id}] Canceled")88case "expired":89print(f"[{result.custom_id}] Expired - resubmit")90```9192---9394## Cancel a Batch9596```python97cancelled = client.messages.batches.cancel(message_batch.id)98print(f"Status: {cancelled.processing_status}") # "canceling"99```100101---102103## List Batches (auto-pagination)104105Iterating the return value of any `list()` call auto-paginates across all pages — do not index into `.data` if you want the full set:106107```python108for batch in client.messages.batches.list(limit=20):109print(batch.id, batch.processing_status)110```111112For manual control, use `first_page.has_next_page()` / `first_page.get_next_page()` / `first_page.next_page_info()`; `first_page.data` holds the current page's items and `first_page.last_id` is the cursor.113114---115116## Batch with Prompt Caching117118```python119shared_system = [120{"type": "text", "text": "You are a literary analyst."},121{122"type": "text",123"text": large_document_text, # Shared across all requests124"cache_control": {"type": "ephemeral"}125}126]127128message_batch = client.messages.batches.create(129requests=[130Request(131custom_id=f"analysis-{i}",132params=MessageCreateParamsNonStreaming(133model="claude-opus-4-8",134max_tokens=16000,135system=shared_system,136messages=[{"role": "user", "content": question}]137)138)139for i, question in enumerate(questions)140]141)142```143144---145146## Full End-to-End Example147148```python149import anthropic150import time151from anthropic.types.message_create_params import MessageCreateParamsNonStreaming152from anthropic.types.messages.batch_create_params import Request153154client = anthropic.Anthropic()155156# 1. Prepare requests157items_to_classify = [158"The product quality is excellent!",159"Terrible customer service, never again.",160"It's okay, nothing special.",161]162163requests = [164Request(165custom_id=f"classify-{i}",166params=MessageCreateParamsNonStreaming(167model="claude-haiku-4-5",168max_tokens=50,169messages=[{170"role": "user",171"content": f"Classify as positive/negative/neutral (one word): {text}"172}]173)174)175for i, text in enumerate(items_to_classify)176]177178# 2. Create batch179batch = client.messages.batches.create(requests=requests)180print(f"Created batch: {batch.id}")181182# 3. Wait for completion183while True:184batch = client.messages.batches.retrieve(batch.id)185if batch.processing_status == "ended":186break187time.sleep(10)188189# 4. Collect results190results = {}191for result in client.messages.batches.results(batch.id):192if result.result.type == "succeeded":193msg = result.result.message194results[result.custom_id] = next((b.text for b in msg.content if b.type == "text"), "")195196for custom_id, classification in sorted(results.items()):197print(f"{custom_id}: {classification}")198```199