Many Claude requests and no rush? Send a batch at half the cost

The Message Batches API takes many requests at once and works on them in the background, at 50% lower cost. Most batches finish in under an hour, and results come back with each request's custom_id.

Published 3 min read#AI#Developer tools

Same request, same model, half the cost. The only condition is that you're willing to wait. You have a thousand product descriptions to write, or customer reviews to sort, and nobody is waiting for the answer right now. Instead of a thousand requests one after another, you send them as one batch and pay less. This article shows when a batch fits, how to send one, and how to read its results without losing a request.

What is the Message Batches API?

The Message Batches API is a way to send many Messages requests at once, and Claude works on them in the background instead of answering each one right away.

According to the batch processing guide in the Claude docs, most batches finish in less than an hour, at 50% lower cost. A single batch has a limit: 100,000 requests or 256 MB, whichever comes first. The guide says it suits work that doesn't need an immediate answer, such as processing large volumes of data or running large-scale evaluations.

When should you use a batch instead of a normal request?

A batch fits when you have many requests and no user is waiting for the answer on screen, because you trade time for cost.

Work like this:

  • Descriptions for every product in a store, or their translations.

  • Sorting the customer reviews or support messages that came in during the day.

  • Summarizing old articles or files in one go.

  • Trying a new prompt on a hundred examples before you adopt it.

Chat, and anything a user is waiting for, stays on the normal request.

How do you send a batch?

You send a batch as one request holding a requests array, where each item has your own custom_id and params just like a normal Messages request.

The create batch page in the API docs says a batch begins processing immediately once it's created, and can take up to 24 hours to complete. Here is how you send the product descriptions with the TypeScript SDK:

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

const batch = await client.messages.batches.create({
  requests: products.map((product) => ({
    custom_id: `product-${product.id}`,
    params: {
      model: 'claude-haiku-5-5',
      max_tokens: 400,
      messages: [
        { role: 'user', content: `Write a short product description for: ${product.name}` },
      ],
    },
  })),
});

console.log(batch.id);

Store batch.id, because you use it to ask about the batch and fetch its results.

How do you read the results without mixing them up?

Results come back as a JSONL file, one request's result per line, and you match each result to its request by custom_id, not by order.

The batch results page says results are not guaranteed to be in the same order as the requests, that the custom_id must be unique for each request within the batch, since it is what matches each result to its request, and that each result has one of four types: succeeded, errored, canceled or expired. So once the batch is done:

for await (const item of await client.messages.batches.results(batch.id)) {
  if (item.result.type === 'succeeded') {
    await saveDescription(item.custom_id, item.result.message);
  } else {
    await markForRetry(item.custom_id, item.result.type);
  }
}

You collect the requests that didn't succeed and send them in a new batch, and the custom_id tells you which product still has no description.

What should you check before sending a thousand requests?

Try the prompt on a few normal requests before the batch, because a mistake in the instructions repeats in every request and you pay for each one.

  1. Send five normal requests with the same params, and read the answers.

  2. Check stop_reason on each answer, because an answer cut off at max_tokens will come out cut off in every request of the batch.

  3. Give the custom_id a meaning (product-123, not 1), so the result alone tells you what it belongs to.

  4. Then send the full batch.

Where do you start today?

Look in your project for any loop that sends requests to Claude one after another with no user waiting: a nightly script, a sorting job, content generation. That's the first candidate for a batch.

If every request carries the same long instructions, see how to store them with prompt caching. Before you send, count the tokens to know the size of each request, and once results come back, check stop_reason to catch cut-off answers.

What's the first job in your project that you're sending one request at a time, with nobody waiting for it?

FAQ

Can I use a batch for a live chat with a user?

No. A batch runs in the background and can take up to 24 hours. Use a normal Messages request for chat, and keep batches for work nobody is waiting on.

What happens to a request that doesn't finish?

It comes back with a result type other than succeeded: errored, canceled or expired. Read the type and resend the ones that didn't succeed in a new batch.

Do results come back in the same order as the requests?

Not guaranteed. That's why each request has a custom_id that is unique in the batch, and you match every result to its request with it.

Share

More articles