Introduction: Why Process Multiple Messages at Once?

Note: This is the end of the demonstration, intended to showcase how we can adapt the Anthropic Learn content to our platform.

Welcome back! So far, you have learned how to send a single message to Claude using Python, JavaScript, and cURL. This is a great start, but in real-world applications, you often need to send several messages at once. For example, you might want to process multiple user questions, check several documents, or run a batch of tests. Sending each message one by one can be slow and inefficient.

This is where the Message Batches API comes in. It allows you to send multiple messages in a single request, making your code faster and more efficient. In this lesson, you will learn how to use the Message Batches API with Python to process several messages at once.


Quick Reference: Single Message Requests

Before we dive into batch processing, let’s quickly remind ourselves how sending a single message works. In a previous lesson, you learned to use the Anthropic Python SDK to send a message like this:

In this code:

  • You import the anthropic library.
  • You create a client.
  • You send a single message to Claude and print the response.

Now, let’s see how you can do something similar, but with multiple messages at once.


What Is the Message Batches API?

The Message Batches API is a special feature of the Anthropic SDK. It lets you send several messages in one request, instead of making a separate request for each message. This is useful when you have a list of questions or prompts you want Claude to answer all at once.

When you use the Message Batches API, you create a list of requests. Each request has its own message and settings. The API processes all of them together and returns a batch of responses.

You might use the Message Batches API if:

  • You want to save time by sending many messages at once.
  • You need to process a group of similar tasks together.
  • You want to keep your code clean and efficient.

How to Build a Batch Request in Python

Let’s build a batch request step by step. We will use the Anthropic Python SDK, which should already be installed in your CodeSignal environment.

1. Import the Required Modules

First, you need to import the necessary parts of the SDK. Besides the main anthropic module, you will use some types to help build your batch request.

  • anthropic is the main SDK.
  • MessageCreateParamsNonStreaming helps you set up the message parameters.
  • Request is used to define each individual message in the batch.
2. Create the Client

Just like before, you need to create a client to interact with Claude.

3. Build the List of Requests

Now, you will create a list of requests. Each request has:

  • A custom_id (a name you choose to help track the request).
  • params (the message and its settings).

Here’s how you can create two requests:

  • Each Request has a unique custom_id so you can tell which response belongs to which request.
  • The params field is set up just like a single message, but now you can have as many as you want in the list.
4. Send the Batch Request

Now, you use the client to send all the requests at once:

  • The batches.create() method takes your list of requests and sends them together.

Example: Sending Multiple Messages at Once

Let’s put it all together. Here is the complete code for sending two messages in a batch:

When a batch is first created, the response will have a processing status of in_progress.

  • Each response is linked to its custom_id, so you know which answer goes with which message.
  • You can now process or display the results as needed.
Tracking your batch

The Message Batch’s processing_status field indicates the stage of processing the batch is in. It starts as in_progress, then updates to ended once all the requests in the batch have finished processing, and results are ready. You can monitor the state of your batch by visiting the Console, or using the retrieval endpoint:

Polling

You can poll this endpoint to know when processing has ended.


Summary and What’s Next

In this lesson, you learned how to use the Message Batches API to send multiple messages to Claude in a single request. You saw how to build a batch request, set up each message with a unique ID, and handle the responses. This approach is much more efficient when you need to process several messages at once.

Next, you will get a chance to practice building and sending your own batch requests. Try different messages and see how Claude responds to each one. This will help you get comfortable with batch processing and prepare you for more advanced tasks in the future.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal