# Batch

> Send many requests at once and collect the replies later.

## What it is for

When you have many requests and do not need the answers straight away, send them as one batch. Antarya works through them in the background, and you collect the replies when the batch is done. Batch requests are billed at their own price.

## Create a batch

Each request in the batch has a `custom_id` you choose and the same body you would send to `/v1/chat`.

```bash
curl https://antarya.ai/v1/batches \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "requests": [
      {
        "custom_id": "contract-001",
        "params": {"messages": [{"role": "user", "content": "Summarise this contract'"'"'s termination clause."}]}
      },
      {
        "custom_id": "contract-002",
        "params": {"messages": [{"role": "user", "content": "Draft a reply that declines the meeting."}]}
      }
    ]
  }'
```

```python
from antarya import Antarya

client = Antarya()

batch = client.batches.create(requests=[
    {
        "custom_id": "contract-001",
        "params": {"messages": [{"role": "user", "content": "Summarise this contract's termination clause."}]},
    },
    {
        "custom_id": "contract-002",
        "params": {"messages": [{"role": "user", "content": "Draft a reply that declines the meeting."}]},
    },
])
print(batch.id, batch.status)
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const batch = await client.batches.create({
  requests: [
    {
      custom_id: "contract-001",
      params: { messages: [{ role: "user", content: "Summarise this contract's termination clause." }] },
    },
    {
      custom_id: "contract-002",
      params: { messages: [{ role: "user", content: "Draft a reply that declines the meeting." }] },
    },
  ],
});
console.log(batch.id, batch.status);
```

Output:

```
{
  "id": "batch_01",
  "status": "in_progress",
  "request_counts": {"total": 2, "done": 0}
}
```

## Collect the results

`GET /v1/batches/{id}` returns the status. When it is `ended`, `GET /v1/batches/{id}/results` returns one line of JSON per request, each with its `custom_id` and either a `reply` or an `error`. See whether there is an `error` before you read `reply`. The lines may come in any order, so match them on `custom_id`.

```bash
curl https://antarya.ai/v1/batches/batch_01 \
  -H "Authorization: Bearer $ANTARYA_API_KEY"

curl https://antarya.ai/v1/batches/batch_01/results \
  -H "Authorization: Bearer $ANTARYA_API_KEY"
```

```python
batch = client.batches.get("batch_01")
if batch.status == "ended":
    for line in client.batches.results("batch_01"):
        if line.error:
            print(line.custom_id, line.error.type, line.error.message)
        else:
            print(line.custom_id, line.reply.content[0].text)
```

```typescript
const batch = await client.batches.get("batch_01");
if (batch.status === "ended") {
  for await (const line of client.batches.results("batch_01")) {
    if (line.error) {
      console.log(line.custom_id, line.error.type, line.error.message);
    } else {
      console.log(line.custom_id, line.reply?.content[0]?.text);
    }
  }
}
```

To stop a batch, call `POST /v1/batches/{id}/cancel`. Requests already finished keep their replies.

## Parameters

- `requests` (array, required): The requests to run.
  - `requests.custom_id` (text, required): Your own label for the request. Each one in a batch must be different.
  - `requests.params` (object, required): The body of a chat request, with the same fields as POST /v1/chat except `stream` and `session_id`, which a batch request cannot use.

## Limits

A batch holds up to 1,000 requests and its results arrive within 24 hours. A request still unrun after 24 hours comes back as a result line with an `error` of type `overloaded` and the message "This request did not run within 24 hours." There is no `expired` status. Results are kept for 30 days after the batch ends. An account can have 5 batches in progress and 10,000 queued requests. Calls to the batch endpoints count against the management limit in [rate limits](https://antarya.ai/docs/api/rate-limits.md); the requests inside a batch do not count against the chat limits. `cancel` stops the requests that have not run and the batch ends.

| Limit | Value |
|---|---|
| Requests in one batch | 1,000 requests |
| Time to finish | Within 24 hours |
| Batches in progress | 5 an account |
| Queued requests | 10,000 an account |
| Batch price | To be announced |

## Next steps

- [Counting tokens](https://antarya.ai/docs/guides/counting-tokens.md): Estimate the size of a batch before you send it.
- [Rate limits](https://antarya.ai/docs/api.md): How many requests a key can make.
- [Python SDK](https://antarya.ai/docs/sdk/python.md): Create and read batches from Python.
