View Source Anthropix.Batch (Anthropix v0.5.0)

The message batches API is a powerful, cost-effective way to asynchronously process large volumes of messages requests. This approach is well-suited to tasks that do not require immediate responses, reducing costs by 50% while increasing throughput.

Summary

Functions

Cancels a message batch before processing ends. Depending on when cancellation is initiated, the number of cancelled messages requests will vary.

Send a batch of message creation requests. Used to process multiple message requests in a single batch. Processing begins immediately.

List all message batches. Most recently created batches are returned first.

Retrieve the status of the batch. Will respond with or stream a list of chat message completions.

Retrieve the status of the batch. Can be used to poll for message batch completion.

Functions

cancel(client, batch_id)

@spec cancel(Anthropix.client(), String.t()) :: any()

Cancels a message batch before processing ends. Depending on when cancellation is initiated, the number of cancelled messages requests will vary.

Examples

iex> Anthropix.Batch.cancel(client, "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R")
{:ok, %{
  "type" => "message_batch",
  "id" => "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R"
  "processing_status" => "canceling",
  ...
}}

create(client, params \\ [])

@spec create(Anthropix.client(), [keyword()]) :: any()

Send a batch of message creation requests. Used to process multiple message requests in a single batch. Processing begins immediately.

Options

  • :requests (list of map/0) - Required. List of requests for prompt completion.

Examples

iex> Anthropix.Batch.create(client, [
...>   %{custom_id: "foo", params: %{model: "claude-3-haiku-20240307", messages: [%{role: "user", content: "Why is the sky blue?"}]}},
...>   %{custom_id: "bar", params: %{model: "claude-3-haiku-20240307", messages: [%{role: "user", content: "Why is the sea blue?"}]}},
...> ])
{:ok, %{
  "type" => "message_batch",
  "id" => "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R"
  "processing_status" => "in_progress",
  ...
}}

list(client, params \\ [])

@spec list(
  Anthropix.client(),
  keyword()
) :: any()

List all message batches. Most recently created batches are returned first.

Options

  • :before_id (String.t/0) - Returns the page of results immediately before this object.
  • :after_id (String.t/0) - Returns the page of results immediately after this object.
  • :limit (integer/0) - Number of items to return per page. Between 1-100 (default 20).

Examples

iex> Anthropix.Batch.list(client)
{:ok, %{"data" => [...]}}

# With pagination options
iex> Anthropix.Batch.list(client, after_id: "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R", limit: 50)
{:ok, %{"data" => [...]}}

results(client, map_or_batch_id, params \\ [])

@spec results(Anthropix.client(), String.t() | map(), keyword()) :: any()

Retrieve the status of the batch. Will respond with or stream a list of chat message completions.

It is preferred to pass the result of show/2 directly, but also accepts a results_url or batch_id string.

Results are not guaranteed to be in the same order as requests. Use the custom_id field to match results to requests.

Options

  • :requests (list of map/0) - Required. List of requests for prompt completion.

Examples

iex> Anthropix.Batch.results(client, "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R")
{:ok, [
  %{"custom_id" => "foo", "result" => %{...}},
  %{"custom_id" => "bar", "result" => %{...}},
]}

# Passing true to the :stream option initiates an async streaming request.
iex> Anthropix.Batch.results(client, "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R", stream: true)
{:ok, #Function<52.53678557/2 in Stream.resource/3>}

show(client, batch_id)

@spec show(Anthropix.client(), String.t()) :: any()

Retrieve the status of the batch. Can be used to poll for message batch completion.

When the batch is ready, the "processing_status" will be "ended" and the response will include a "results_url" value.

Examples

iex> Anthropix.Batch.show(client, "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R")
{:ok, %{
  "type" => "message_batch",
  "id" => "msgbatch_01DJuZbTFXpGRhqTdqFH1P2R"
  "processing_status" => "ended",
  "results_url" => "https://api.anthropic.com/v1/messages/batches/msgbatch_01DJuZbTFXpGRhqTdqFH1P2R/results",
  ...
}}