Haijun Platform Docs
ID

Message Batches allow you to process large volumes of Messages requests asynchronously and cost-effectively. This cookbook demonstrates how to use the Message Batches API to handle bulk operations while reducing costs by 50%.

In this cookbook, we will demonstrate how to:

r batch processing status

Retrieve and handle batch results

Implement best practices for effective batching

Setup

MODEL_NAME = "haijun-sonnet-4-6"

Example 1: Basic Batch Processing

Batch ID: msgbatch_01GgqTz9XzriGNHzTSGZsJJ8 Status: in_progress Created at: 2024-10-08 00:46:30.694748+00:00 Monitoring Batch Progress Now let's monitor the batch processing status:

le="padding-left:12ch;text-indent:-12ch"> print(f"Status: {result.result.type}")

if result.result.type == "succeeded":

print(f"Content: {result.result.message.content[0].text[:200]}...")

elif result.result.type == "errored":

print("Request errored")

elif result.result.type == "canceled":

print("Request was canceled")

elif result.result.type == "expired":

print("Request expired")

Example usage:

batch_status = monitor_batch(response.id)

if batch_status.processing_status == "ended":

process_results(batch_status.id)

BetaMessageBatch(id='msgbatch_01GgqTz9XzriGNHzTSGZsJJ8', cancel_initiated_at=None, created_at=datetime.datetime(2024, 10, 8, 0, 46, 30, 694748, tzinfo=datetime.timezone.utc), ended_at=datetime.datetime(2024, 10, 8, 0, 46, 47, 283392, tzinfo=TzInfo(UTC)), expires_at=datetime.datetime(2024, 10, 9, 0, 46, 30, 694748, tzinfo=datetime.timezone.utc), processing_status='ended', request_counts=RequestCounts(canceled=0, errored=0, expired=0, processing=0, succeeded=4), results_url='https://haijun.my.id/v1/messages/batches/msgbatch_01GgqTz9XzriGNHzTSGZsJJ8/results', type='message_batch') Status: ended Batch msgbatch_01GgqTz9XzriGNHzTSGZsJJ8 Summary: Status: ended Created: 2024-10-08 00:46:30.694748+00:00 Ended: 2024-10-08 00:46:47.283392+00:00 Expires: 2024-10-09 00:46:30.694748+00:00 Individual Results: Result for question-0: Status: succeeded Content: Solar panels convert sunlight into electricity through a process called the photovoltaic effect. Here's a step-by-step explanation of how this works: 1. Solar panel composition: Solar panels are made... Result for question-1: Status: succeeded Content: Mutual funds and ETFs (Exchange-Traded Funds) are both popular investment vehicles that allow investors to diversify their portfolios, but they have several key differences: 1. Trading: - Mutual fund... Result for question-2: Status: succeeded Content: A pick and roll, also known as a screen and roll, is a fundamental offensive play in basketball involving two players. Here's how it works: 1. The ball handler (usually a guard) has possession of the... Result for question-3: Status: succeeded Content: Leaves change color in autumn due to a combination of factors, primarily related to changes in temperature, daylight, and the tree's biological processes. Here's a breakdown of why this happens: 1. C... Example 2: Advanced Batch Processing for Different Message Types This example demonstrates more advanced usage, including error handling and processing different types of requests in a single batch including a simple message, a message with a system prompt, a multi-turn message, and a message with an image.

On this page
SetupExample 1: Basic Batch Processing