# Batches

> Create a batch of chat requests, read its status and collect the results.

## Create a batch

`POST https://antarya.ai/v1/batches`

Send a list of chat requests, each with a `custom_id` you choose. Antarya runs them in the background, billed at the batch rate. The [guide](https://antarya.ai/docs/guides/batch.md) walks through the whole cycle.

### Headers

| Header | Value |
|---|---|
| `Authorization` | `Bearer $ANTARYA_API_KEY` |
| `Content-Type` | `application/json` |

```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."}]}
      }
    ]
  }'
```

```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."}]},
    },
])
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." }] },
    },
  ],
});
console.log(batch.id, batch.status);
```

Output:

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

- `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.

## The batch object

- `id` (text, required): The id of the batch, such as batch_01.
- `status` (text, required): "in_progress" while requests are running, "ended" when all have finished or the batch was cancelled.
- `request_counts` (object, required): How many requests the batch holds and how many have finished.
  - `request_counts.total` (number, required): Requests in the batch.
  - `request_counts.done` (number, required): Requests that have a reply or an error.

## Read, collect and cancel

| Request | Does |
|---|---|
| `GET /v1/batches/{id}` | Returns the batch object. |
| `GET /v1/batches/{id}/results` | Once the status is `ended`, returns one line of JSON per request, each with its `custom_id` and either a `reply` or an `error`. Read `error` first. The lines may come in any order. |
| `POST /v1/batches/{id}/cancel` | Stops the requests that have not run. Finished requests keep their replies. |

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

```python
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
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);
  }
}
```

Output:

```
{"custom_id": "contract-001", "reply": {"id": "req_09", "model": "antarya", "content": [{"type": "text", "text": "Either side can end the agreement with 30 days of written notice."}], "stop_reason": "end_turn", "usage": {"input_tokens": 24, "output_tokens": 17}}}
{"custom_id": "contract-002", "error": {"type": "overloaded", "message": "This request did not run within 24 hours."}}
```

## 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." A request cancelled before it ran comes back as an `error` of type `invalid_request`. The batch object has only the statuses `in_progress` and `ended`. 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.

| 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

- [Batch guide](https://antarya.ai/docs/guides/batch.md): Create a batch and collect its replies.
- [Rate limits](https://antarya.ai/docs/api/rate-limits.md): How many requests a key can make.
- [Chat](https://antarya.ai/docs/api/chat.md): The body each batch request takes.
