View Source Anthropic.Batches (anthropic_community v0.5.0)

The Batches resource (Message Batches API): submit up to 100k Messages.create/2-shaped requests for asynchronous, discounted bulk processing.

Example

{:ok, batch} =
  Anthropic.Batches.create(client, [
    %{custom_id: "request-1", params: [model: "claude-opus-4-8", max_tokens: 100, messages: [%{role: "user", content: "Hi"}]]},
    %{custom_id: "request-2", params: [model: "claude-opus-4-8", max_tokens: 100, messages: [%{role: "user", content: "Hello"}]]}
  ])

{:ok, batch} = Anthropic.Batches.retrieve(client, batch.id)

if batch.processing_status == "ended" do
  {:ok, results} = Anthropic.Batches.results(client, batch)
end

Summary

Functions

Cancels a batch that is still processing.

Creates a message batch from a list of %{custom_id:, params:} requests.

Deletes a batch's tracking data. The batch must first be in an ended state (cannot delete an in-progress batch).

Lists batches, most recently created first.

Like list/2, but returns a lazy Stream of individual batches that transparently fetches subsequent pages as it's consumed, instead of one page at a time.

Fetches and parses the JSONL results of an ended batch, matched by custom_id. Accepts either a batch id or a batch() map already carrying results_url (avoids an extra retrieve/2 round-trip).

Retrieves a batch by id.

Types

@type batch() :: %{
  id: String.t(),
  type: String.t(),
  processing_status: String.t(),
  request_counts: map(),
  results_url: String.t() | nil,
  created_at: String.t() | nil,
  ended_at: String.t() | nil,
  expires_at: String.t() | nil
}
@type batch_request() :: %{
  custom_id: String.t(),
  params: Anthropic.Messages.create_opts()
}
@type deleted_batch() :: %{id: String.t(), type: String.t()}
@type list_result() :: %{
  data: [batch()],
  has_more: boolean(),
  first_id: String.t() | nil,
  last_id: String.t() | nil
}
@type result() :: %{
  custom_id: String.t(),
  type: String.t(),
  message: Anthropic.Messages.Message.t() | nil,
  error: Anthropic.Error.t() | nil
}

Functions

Link to this function

cancel(client, batch_id)

View Source
@spec cancel(Anthropic.Client.t(), batch_id :: String.t()) ::
  {:ok, batch()} | {:error, Anthropic.Error.t()}

Cancels a batch that is still processing.

Link to this function

create(client, requests)

View Source
@spec create(Anthropic.Client.t(), [batch_request()]) ::
  {:ok, batch()} | {:error, Anthropic.Error.t()}

Creates a message batch from a list of %{custom_id:, params:} requests.

Link to this function

delete(client, batch_id)

View Source
@spec delete(Anthropic.Client.t(), batch_id :: String.t()) ::
  {:ok, deleted_batch()} | {:error, Anthropic.Error.t()}

Deletes a batch's tracking data. The batch must first be in an ended state (cannot delete an in-progress batch).

Link to this function

list(client, opts \\ [])

View Source
@spec list(Anthropic.Client.t(),
  before_id: String.t(),
  after_id: String.t(),
  limit: pos_integer()
) ::
  {:ok, list_result()} | {:error, Anthropic.Error.t()}

Lists batches, most recently created first.

Link to this function

list_all(client, opts \\ [])

View Source
@spec list_all(Anthropic.Client.t(),
  before_id: String.t(),
  after_id: String.t(),
  limit: pos_integer()
) ::
  Enumerable.t()

Like list/2, but returns a lazy Stream of individual batches that transparently fetches subsequent pages as it's consumed, instead of one page at a time.

Link to this function

results(client, batch_id)

View Source
@spec results(Anthropic.Client.t(), batch() | String.t()) ::
  {:ok, [result()]} | {:error, Anthropic.Error.t()}

Fetches and parses the JSONL results of an ended batch, matched by custom_id. Accepts either a batch id or a batch() map already carrying results_url (avoids an extra retrieve/2 round-trip).

Link to this function

retrieve(client, batch_id)

View Source
@spec retrieve(Anthropic.Client.t(), batch_id :: String.t()) ::
  {:ok, batch()} | {:error, Anthropic.Error.t()}

Retrieves a batch by id.