Claude Batch API in Python

Use the Anthropic Batch API in Python to process thousands of requests at 50% off standard pricing. Poll for results and handle partial failures.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

The Batch API processes requests asynchronously at half the standard price — ideal for dataset analysis, eval runs, and bulk content generation.

Submit a batch

import anthropic

client = anthropic.Anthropic()

requests = [
    {
        "custom_id": f"req-{i}",
        "params": {
            "model": "claude-sonnet-4-6",
            "max_tokens": 512,
            "messages": [{"role": "user", "content": f"Summarize: {article}"}]
        }
    }
    for i, article in enumerate(articles)  # your list of texts
]

batch = client.beta.messages.batches.create(requests=requests)
print(f"Batch ID: {batch.id}  Status: {batch.processing_status}")

Poll for completion

import time

def wait_for_batch(batch_id: str, poll_interval: int = 60) -> anthropic.types.beta.BetaMessageBatch:
    while True:
        batch = client.beta.messages.batches.retrieve(batch_id)
        print(f"Status: {batch.processing_status}  "
              f"Succeeded: {batch.request_counts.succeeded}  "
              f"Errored: {batch.request_counts.errored}")

        if batch.processing_status == "ended":
            return batch
        time.sleep(poll_interval)

completed = wait_for_batch(batch.id)

Download results

results = {}
errors = {}

for result in client.beta.messages.batches.results(batch.id):
    if result.result.type == "succeeded":
        results[result.custom_id] = result.result.message.content[0].text
    elif result.result.type == "errored":
        errors[result.custom_id] = result.result.error

print(f"Got {len(results)} results, {len(errors)} errors")

# Save to file
import json
with open("batch_results.json", "w") as f:
    json.dump(results, f, indent=2)

Cost comparison: standard vs batch

ModelStandard inputBatch inputSavings
Claude Sonnet 4.6$3.00 / 1M$1.50 / 1M50%
Claude Haiku 4.5$1.00 / 1M$0.50 / 1M50%
Claude Opus 4.7$15.00 / 1M$7.50 / 1M50%

Use the Cost Calculator to estimate savings for your batch workload. For real-time processing where latency matters, use the standard API with prompt caching instead.

Frequently asked questions

How much does the Batch API cost?
Batch API requests are billed at 50% of the standard per-token price for the model used. There is no minimum batch size. Batches complete within 24 hours; most complete in 1–4 hours.
What is the maximum batch size?
Up to 10,000 requests per batch, with a combined input token limit of 100M tokens per batch. Each individual request in the batch follows the same per-request token limits as the standard API.
Can I cancel a batch?
Yes. Call `client.beta.messages.batches.cancel(batch_id)` while the batch is `in_progress`. Already-processed requests in the batch are still billed at the discounted rate.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript