# `Jibu.Batches`
[🔗](https://github.com/oliverandrich/jibu/blob/v0.2.1/lib/jibu/batches.ex#L1)

The Message Batches API: many Messages requests processed asynchronously at half price.

    {:ok, batch} =
      Jibu.Batches.create(client, %{
        requests: [%{custom_id: "a", params: %{model: "claude-opus-5-5", max_tokens: 1024, messages: messages}}]
      })

Poll `get/2` until `processing_status` is `"ended"`. Then read `results/2`.
Results arrive in any order. Match them by `custom_id`.
Results are the API's JSON with string keys. See the [batches guide](batches.md).

# `cancel`

```elixir
@spec cancel(Req.Request.t(), String.t()) :: {:ok, map()} | {:error, Jibu.Error.t()}
```

Cancels a batch. Requests already processed keep their results.

# `create`

```elixir
@spec create(Req.Request.t(), map()) :: {:ok, map()} | {:error, Jibu.Error.t()}
```

Creates a batch. The body is sent unchanged.

# `delete`

```elixir
@spec delete(Req.Request.t(), String.t()) :: {:ok, map()} | {:error, Jibu.Error.t()}
```

Deletes a batch that has ended.

# `get`

```elixir
@spec get(Req.Request.t(), String.t()) :: {:ok, map()} | {:error, Jibu.Error.t()}
```

Retrieves a batch, including its `processing_status` and `request_counts`.

# `list`

```elixir
@spec list(Req.Request.t(), keyword()) :: {:ok, map()} | {:error, Jibu.Error.t()}
```

Returns one page. `query` takes the API's parameters, such as `:limit` and `:after_id`.

# `results`

```elixir
@spec results(Req.Request.t(), String.t()) ::
  {:ok, Enumerable.t()} | {:error, Jibu.Error.t()}
```

Fetches the results of an ended batch and returns them as a lazily decoded stream of maps.

The whole JSONL body is held in memory while the stream is consumed.

# `stream`

```elixir
@spec stream(Req.Request.t(), keyword()) :: Enumerable.t()
```

Streams all batches across pages, forward only. Raises `Jibu.Error` when a page fails.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
