Skip to content

4.5 Batch Processing Strategies

The Message Batches API takes many requests in one submission and returns the results later. It is a cost optimisation tool with hard constraints. A synchronous call sends one request and waits for its answer. In a blocking workflow, someone or something waits for the result. A latency-tolerant workflow uses its results later.

This lesson uses one made-up example throughout: an insurance broker. Staff on the phone need a policy summary while the client waits. That work is blocking. An overnight job extracts cover details from renewal documents, and the team reads the extracted details next morning. That work is latency-tolerant.

  • 50% cost savings compared to synchronous API calls
  • Up to 24-hour processing window: results may arrive in minutes or take up to 24 hours
  • No guaranteed latency SLA: you cannot rely on results arriving within any specific timeframe
  • No multi-turn tool calling within a single batch request: the model cannot execute tools mid-request and use the results to continue processing
  • custom_id fields for correlating request/response pairs: each request in a batch gets a unique identifier used to match it with its response

Use the synchronous API for blocking workflows and the batch API for latency-tolerant ones. A batch has no latency guarantee and can take 24 hours. So anyone who waits for a batch could wait up to 24 hours. An overnight job loses nothing when it waits, and it costs half as much.

Blocking workflows include:

  • Pre-merge checks in CI/CD
  • Real-time code review feedback
  • Any workflow where developers are blocked pending completion

Latency-tolerant workflows include:

  • Overnight technical debt reports
  • Weekly code audit summaries
  • Nightly test generation runs
  • Batch document extraction

The broker keeps phone summaries synchronous, because a client is waiting. The broker batches the renewal extraction.

The first call below is synchronous. The second call is a batch.

// Synchronous — developer is waiting for this
const preMergeReview = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 4096,
messages: [{ role: "user", content: prDiffContent }]
});
// Batch — results consumed tomorrow morning
const batchRequest = await client.messages.batches.create({
requests: technicalDebtDocuments.map((doc, i) => ({
custom_id: `debt-report-${i}`,
params: {
model: "claude-sonnet-5-5",
max_tokens: 4096,
messages: [{ role: "user", content: doc }]
}
}))
});

The batch API does not support multi-turn tool calling within a single request. You cannot:

  • Define tools and have the model call them mid-request
  • Process tool results and continue the conversation within the same batch item
  • Run agentic loops within a single batch request

A workflow that needs to execute a tool during processing must use the synchronous API for that step. If the broker’s overnight job queried its rates database in the middle of a document, that step would run synchronously.

Plan batch schedules for the full 24 hours. The 24 hours is a maximum window, not a delivery guarantee. A batch that does not finish inside that window returns as expired. Plan the schedule for that worst case, and treat an expired batch as work to resubmit.

Suppose the broker promises renewal summaries within 30 hours. Work backwards:

  • 30 hours total SLA minus the 24-hour worst case = 6 hours of buffer for collecting requests, validating inputs, or absorbing operational delays
  • Submit batches every 4 hours within that buffer window, so that a new batch is always running
  • Submitting every 6 hours leaves no margin at all

A document that arrives just after a submission waits for the next batch. If you submit every 6 hours, it can wait 6 hours and then take 24 hours to process. That uses all 30 hours.

Not all documents in a batch succeed. The correct way to handle failures has three steps.

1. Identify failures by custom_id. Each request has a unique identifier. Parse the batch results to find which custom_id values failed.

2. Resubmit only failures with modifications. If you resubmit the entire batch, you pay again for documents that already succeeded. Common modifications include:

  • Chunking (splitting into smaller parts) oversized documents that exceeded context limits, such as the broker’s longest commercial policy schedules
  • Simplifying extraction prompts for documents with unusual structures
  • Adding format-specific few-shot examples (input-output pairs included in the prompt) for documents that failed due to structural variety

3. Refine prompts on a sample set before batch processing. This early step maximises the number of documents that succeed on the first submission and reduces the cost of resubmissions.

The handler below retries only failed and expired results.

// Poll until the batch has finished before reading results
let batch = await client.messages.batches.retrieve(batchId);
while (batch.processing_status !== "ended") {
await new Promise(r => setTimeout(r, 60_000));
batch = await client.messages.batches.retrieve(batchId);
}
// results() returns a JSONL async iterable, not an array — accumulate it.
// Treat `expired` as a failure too: that is what an overrun batch returns.
const failedIds: string[] = [];
for await (const result of client.messages.batches.results(batchId)) {
if (result.result.type === "errored" || result.result.type === "expired") {
failedIds.push(result.custom_id);
}
}
// Resubmit only failures with modifications
const retryRequests = failedIds.map(id => {
const originalDoc = documentsById[id];
return {
custom_id: `${id}-retry-1`,
params: {
model: "claude-sonnet-5-5",
max_tokens: 8192, // increased for oversized docs
messages: [{
role: "user",
content: chunkIfNeeded(originalDoc)
}]
}
};
});

Prompt Optimisation Before Batch Submission

Section titled “Prompt Optimisation Before Batch Submission”

The most cost-effective batch strategy is to refine prompts before submitting large volumes. The broker’s sample would cover renewal documents from several insurers.

  1. Sample set testing: Take 5-10 representative documents covering the range of formats, edge cases, and document types in your batch
  2. Iterate on the sample: Refine your extraction prompts, add few-shot examples, adjust schema design until the sample set achieves high accuracy
  3. Submit the full batch: With refined prompts, your first-pass success rate will be significantly higher
  4. Handle failures: Resubmit only the failed documents with targeted modifications

Every failed document costs a retry, so this workflow cuts total cost. A 90% first-pass success rate on 1,000 documents means only 100 retries. A 60% rate means 400 retries. That is four times the resubmission cost, plus the batch processing cost for those retries.

The matching rule is the single most tested concept in this task statement. In Question 11 of the sample questions, a manager proposes to switch everything to batch to get the saving. The correct answer keeps blocking workflows synchronous and batches only latency-tolerant ones.

The exam tests the tool limitation directly. A batch workflow that calls external tools during processing needs the synchronous API for that step. A scheduling question may work backwards from an SLA to a submission frequency.

Six exam-style multiple-choice questions on Batch Processing Strategies. Pick an answer, then open the explanation.

Your manager proposes switching both your blocking pre-merge code review and your overnight technical debt report to the Message Batches API for 50% cost savings. How should you evaluate this proposal?

  • A. Switch both to batch processing with status polling to check for completion
  • B. Keep real-time calls for both workflows to avoid batch result ordering issues
  • C. Use batch processing for the technical debt reports only, keeping real-time calls for pre-merge checks
  • D. Switch both to batch processing with a timeout fallback to real-time if the batch takes too long
Answer & explanation

Correct: C

  • A is wrong because status polling does not change the fundamental constraint: the batch API has no guaranteed latency SLA. Pre-merge checks cannot depend on a 24-hour processing window.
  • B is wrong because batch results are correlated using custom_id fields, so ordering is not an issue. The real concern is latency requirements.
  • C is correct because pre-merge checks are blocking workflows where developers wait. The 24-hour window is unacceptable. Technical debt reports are overnight and latency-tolerant — ideal for batch at 50% savings.
  • D is wrong because this adds unnecessary complexity. Match each workflow to the appropriate API based on latency requirements.

Your organisation requires a weekly code audit report delivered by Monday 09:00. You want to use the Message Batches API. When is the latest you can submit the batch?

  • A. Sunday 09:00 (24 hours before the deadline)
  • B. Friday 09:00 (72 hours before, to be safe)
  • C. Monday 06:00 (3 hours before the deadline, since batches usually complete quickly)
  • D. Sunday 03:00, which is 30 hours before the deadline and inside the batch window
Answer & explanation

Correct: D

  • A is wrong because submitting exactly 24 hours before leaves zero buffer. If the batch takes the full 24 hours, you hit the deadline with no margin.
  • B is wrong because 72 hours is unnecessarily conservative. A 6-hour buffer beyond the 24-hour maximum is adequate.
  • C is wrong because “usually completes quickly” is not a guarantee. The batch API has no latency SLA. Designing around best-case timing is unreliable.
  • D is correct because you must account for the worst case: full 24-hour processing. 30 hours before the deadline provides a 6-hour buffer for any issues.

A batch of 500 documents completes with 450 successes and 50 failures. What is the correct failure handling approach?

  • A. Identify the 50 failures by custom_id, apply targeted modifications, and resubmit only those 50
  • B. Resubmit the entire batch of 500 documents to ensure consistency
  • C. Discard the failures and report only the 450 successful extractions
  • D. Reduce the batch size to 50 documents and reprocess everything in smaller batches
Answer & explanation

Correct: A

  • A is correct because custom_id fields identify which documents failed. Targeted modifications (chunking, simplified prompts, format-specific examples) address the specific failure causes.
  • B is wrong because resubmitting all 500 wastes cost on the 450 already-successful documents and doubles processing expense.
  • C is wrong because discarding 10% of results reduces data completeness. Many failures are fixable with targeted retry.
  • D is wrong because batch size is not the issue. Targeted retry of failures is more efficient than reprocessing everything.

Your extraction workflow requires Claude to call a tool mid-request, use the tool results, and continue processing within the same request. Which API should you use?

  • A. Message Batches API with tool definitions included in the batch request
  • B. Synchronous Messages API, because it supports multi-turn tool calling
  • C. Message Batches API with a webhook to handle tool calls asynchronously
  • D. Either API, since tool calling works the same in both
Answer & explanation

Correct: B

  • A is wrong because the batch API does not support multi-turn tool calling within a single request, even with tool definitions.
  • B is correct because the batch API cannot execute tools mid-request and use results to continue. Multi-turn tool calling requires the synchronous API.
  • C is wrong because webhooks cannot inject tool results back into an in-progress batch request.
  • D is wrong because tool calling behaviour differs between the APIs. The batch API lacks multi-turn tool calling.

Before submitting a batch of 1,000 documents, your colleague suggests testing prompts on a 5-document sample first. Why is this the correct approach?

  • A. To refine prompts and maximise first-pass success rate, reducing total cost from resubmissions
  • B. To verify the API connection and authentication before a large submission
  • C. To estimate processing time so you can plan the batch submission schedule
  • D. To check that the model supports the document format before committing to full processing
Answer & explanation

Correct: A

  • A is correct because prompt refinement on a representative sample maximises first-pass success. A 90% success rate means 100 retries. A 60% rate means 400 retries — four times the resubmission cost.
  • B is wrong because API connection testing is a basic operational check, not the primary purpose of sample testing.
  • C is wrong because batch processing has no guaranteed latency SLA, so you cannot estimate processing time from a sample.
  • D is wrong because format support is a secondary concern. The primary benefit is prompt quality improvement before large-scale submission.

Which of the following is NOT a valid use case for the Message Batches API?

  • A. Nightly generation of test cases from code documentation
  • B. Weekly audit summary of code review findings
  • C. Pre-merge check a developer waits on before merging
  • D. Overnight extraction of financial data from 2,000 invoices
Answer & explanation

Correct: C

  • A is wrong because nightly test generation is latency-tolerant — results consumed next business day. Valid batch use case.
  • B is wrong because weekly audit summaries have no real-time dependency. Valid batch use case.
  • C is correct because pre-merge checks are blocking workflows. Developers wait for the result. The 24-hour batch processing window is unacceptable for blocking workflows.
  • D is wrong because overnight extraction is consumed the following morning. Valid batch use case.