Task 3.5
Batch Processing
Message Batches API for cost-effective async workloads
📘What You Need to Know
The Message Batches API
The Message Batches API lets you submit large volumes of requests for asynchronous processing. Instead of sending requests one at a time and waiting for each response, you submit a batch and receive all results when processing completes. This is Anthropic's API-level batch processing — distinct from but complementary to Claude Code's CI integration.
Each request in a batch includes a custom_id field so you can track individual results and correlate them back to source files, repositories, or audit targets. When results return, you match custom_id values to know which response belongs to which input.
- •API: Message Batches API (asynchronous submission)
- •Tracking: custom_id per request for result correlation
- •Submission: batch of many requests at once
- •Retrieval: poll or webhook when batch processing completes
50% Cost Savings and the 24-Hour Window
Batch requests cost 50% less than equivalent synchronous API calls. The tradeoff is latency: Anthropic processes batches within a 24-hour window rather than returning results in seconds. You are trading speed for cost — appropriate when the work does not block anything urgent.
The 50% savings add up quickly at scale. A nightly audit of 500 repositories at half price versus synchronous pricing can save significant budget over a month. But if a developer is waiting for the result before merging a PR, 24 hours is unacceptable.
Batch API = 50% cost savings + up to 24-hour processing window. Sync API = full price + immediate results.
When to Use Batch vs Synchronous
Use the Batch API for non-urgent, high-volume workloads where results can wait: nightly compliance scans across all repositories, weekly documentation generation for stale README files, bulk code quality audits, or mass test case generation for legacy modules. These tasks run on a schedule and nobody is blocked waiting.
Use synchronous API calls for anything real-time or blocking: PR reviews that must complete before merge, interactive code generation in the IDE, customer support responses, or CI steps that gate deployment. If a human or pipeline is waiting, synchronous is the only option.
- •Batch: nightly audits, weekly reviews, non-urgent bulk analysis, scheduled documentation
- •Sync: blocking PR reviews, real-time feedback, interactive sessions, CI merge gates
Designing a Batch Audit Pipeline
A typical batch audit pipeline runs on a cron schedule. It collects targets (files, repos, or modules), constructs a batch request with one message per target and a unique custom_id, submits the batch, and stores the batch ID. A separate retrieval job polls for completion, downloads results, and processes findings — filing issues, updating dashboards, or sending summary reports.
The two-job pattern (submit + retrieve) decouples submission from result processing. Your submit job runs at 2 AM; your retrieve job runs at 6 AM and processes whatever completed. If the batch is not ready yet, the retrieve job retries later — well within the 24-hour window.
# Batch submission pattern
batch_requests = [
{"custom_id": "repo-auth-001", "params": {"messages": [...]}},
{"custom_id": "repo-api-002", "params": {"messages": [...]}},
{"custom_id": "repo-web-003", "params": {"messages": [...]}},
]
batch = client.batches.create(requests=batch_requests)
# Poll later: client.batches.retrieve(batch.id)Cost-Benefit Analysis Framework
Before choosing batch, ask three questions: (1) Is anyone blocked waiting for the result? If yes, use sync. (2) Can the work run on a schedule? If yes, batch is a candidate. (3) Is the volume high enough that 50% savings matter? For 5 requests, the savings are negligible; for 5,000, they are substantial.
The exam presents scenarios and asks which API mode fits. Look for signals: "nightly," "weekly," "non-urgent," and "cost optimization" point to batch. "Blocking," "real-time," "before merge," and "interactive" point to sync.
Like regular mail vs express shipping: batch is cheaper and fine when you do not need an instant answer.
⚠️Exam Traps
Trap: Using the Batch API for blocking PR reviews
Why it's wrong: Batch has up to a 24-hour processing window — far too slow for merge-blocking reviews.
Trap: Using synchronous API for nightly audits of 1,000 repositories
Why it's wrong: Paying full price for high-volume, non-urgent work wastes budget when batch offers 50% savings.
Trap: Forgetting custom_id when submitting batch requests
Why it's wrong: Without custom_id, you cannot correlate results back to specific source files or repositories.
Trap: Expecting batch results in seconds like synchronous calls
Why it's wrong: Batch processing has a 24-hour SLA window — results are not immediate.
✓Concept Check
Your team wants to run a weekly compliance scan across 200 microservice repositories, checking for hardcoded secrets and missing license headers. Results are reviewed every Monday morning. Which API should you use?
🔨Build Exercise
Intermediate40 minutesDesign a Batch Audit Pipeline
What you'll learn
- •Choose batch vs sync for different workload types
- •Design a submit/retrieve batch pipeline with custom_id tracking
- •Calculate cost savings for bulk workloads