---
title: "Batches"
subtitle: "Run many requests at the provider's batch price through one endpoint."
---

A batch is a job: create it with `POST https://gateway.respan.ai/api/v1/batches`, poll `GET https://gateway.respan.ai/api/v1/batches/{id}` until it ends, then read `GET https://gateway.respan.ai/api/v1/batches/{id}/results`. The requests go inline in the create body, in the same shape for every provider. You never upload a file: the gateway does the provider's own steps for you.

Batches run on Anthropic, OpenAI, Mistral and Together models. A provider ends a batch within 24 hours, and often much sooner.

<Note>
Mistral and Together batches are in early access. Contact us to turn them on for your project.
</Note>

## Quickstart

<Tabs>
<Tab title="cURL">
```bash
# 1. Create the batch
curl https://gateway.respan.ai/api/v1/batches \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-haiku-4.5",
    "endpoint": "/v1/messages",
    "requests": [
      {"custom_id": "row-1", "body": {"max_tokens": 64, "messages": [{"role": "user", "content": "Say hi"}]}},
      {"custom_id": "row-2", "body": {"max_tokens": 64, "messages": [{"role": "user", "content": "Say bye"}]}}
    ]
  }'

# 2. Poll until "status" is "completed", "failed", "expired" or "cancelled"
curl https://gateway.respan.ai/api/v1/batches/$BATCH_ID \
  -H "Authorization: Bearer $RESPAN_API_KEY"

# 3. Read the results, one JSON line per request
curl https://gateway.respan.ai/api/v1/batches/$BATCH_ID/results \
  -H "Authorization: Bearer $RESPAN_API_KEY"
```
</Tab>
<Tab title="Python">
```python
import json
import os
import time

import requests

BASE = "https://gateway.respan.ai/api/v1/batches"
HEADERS = {"Authorization": f"Bearer {os.environ['RESPAN_API_KEY']}"}

batch = requests.post(BASE, headers=HEADERS, json={
    "model": "gpt-6-luna",
    "endpoint": "/v1/chat/completions",
    "requests": [
        {"custom_id": "row-1", "body": {"messages": [{"role": "user", "content": "Say hi"}]}},
        {"custom_id": "row-2", "body": {"messages": [{"role": "user", "content": "Say bye"}]}},
    ],
}).json()

while batch["status"] in ("validating", "in_progress", "finalizing", "cancelling"):
    time.sleep(60)
    batch = requests.get(f"{BASE}/{batch['id']}", headers=HEADERS).json()

results = requests.get(f"{BASE}/{batch['id']}/results", headers=HEADERS)
for line in results.text.splitlines():
    result = json.loads(line)
    print(result["custom_id"], result["response"] or result["error"])
```
</Tab>
</Tabs>

## Create a batch

| Field | Description |
| --- | --- |
| `model` | The model every request of the batch runs on. Required. Do not repeat it in a request's `body`. |
| `endpoint` | The shape of each request's `body`: `/v1/messages` for an Anthropic model, `/v1/chat/completions` for an OpenAI, Mistral or Together model. Required. The other one is refused with `400`. |
| `requests` | The requests, 1 to 50,000 of them. Required. |
| `requests[].custom_id` | Your id for the request, 1 to 64 characters, used once in the batch. Results are matched to requests by it. |
| `requests[].body` | The request, as you would send it to `endpoint`, without `model` and without `stream`. |
| `metadata` | An object returned on the batch and written to its logs. Optional. |
| `respan_params` | [Respan parameters](/docs/ai-gateway/customers/respan-params) for the batch's logs, such as `customer_identifier`. Optional. |

A create body is at most 32 MiB; split a larger job into several batches. The call answers `202` with the batch:

```json
{
  "id": "batch_340268aed8e84861aeaa6f1cc64e7a02",
  "object": "batch",
  "status": "validating",
  "model": "claude-haiku-4.5",
  "endpoint": "/v1/messages",
  "provider": "anthropic",
  "request_counts": {"total": 2, "completed": 0, "failed": 0},
  "cost": 0.0,
  "created_at": 1796331876,
  "completed_at": null,
  "metadata": {},
  "error": null
}
```

## Endpoints

| Endpoint | What it does |
| --- | --- |
| `POST /api/v1/batches` | Creates a batch and returns it with `status: "validating"`. |
| `GET /api/v1/batches/{id}` | Returns the batch: its `status`, `request_counts` and `cost` so far. |
| `GET /api/v1/batches/{id}/results` | Returns the results as JSON lines. Answers `409` until the batch has ended. |
| `GET /api/v1/batches` | Lists your batches, newest first. Takes `limit` (default 20, at most 100), `status`, and `after` (the `last_id` of the previous page). |
| `POST /api/v1/batches/{id}/cancel` | Cancels a running batch. Requests that had already finished keep their results and are charged. |
| `DELETE /api/v1/batches/{id}` | Deletes an ended batch and its stored results. Answers `409` while it runs: cancel it first. |

A batch moves through `validating`, `in_progress` and `finalizing`, and ends as `completed`, `failed`, `expired` or `cancelled`. A cancel shows as `cancelling` until the provider confirms it.

## Results

Each line of the results is one request, in no particular order. Match them by `custom_id`.

```json
{"custom_id": "row-1", "response": {"status_code": 200, "body": {"type": "message", "role": "assistant", "content": [{"type": "text", "text": "Hi!"}], "usage": {"input_tokens": 9, "output_tokens": 14}}}, "error": null}
{"custom_id": "row-2", "response": null, "error": {"code": "invalid_request_error", "message": "temperature: range: 0..1"}}
```

`response.body` is the provider's normal response for the batch's `endpoint`. A request that failed has `response: null` and an `error`; it is not charged, and it does not fail the rest of the batch.

## Keys and cost

A batch runs on one provider key for its whole life.

- **Your own provider key.** When the project has a key for the model's provider, the batch runs on it. The provider bills you, and Respan charges no credits.
- **Respan credits.** Otherwise the batch runs on Respan's key and each finished request is charged at the model's batch price, about half its standard price. The worst-case cost of the batch is held on your credits when it is created and released when it ends, so a batch you cannot cover is refused at create.

On Respan credits, a plan below Team takes at most 1,000 requests per batch and 1,000 running at once; Team and above take 50,000 per batch and 100,000 running at once. Over the per-batch cap the create answers `403`; over the running cap it answers `429`. Batches on your own key have no Respan cap, only the provider's.

## Logs

Each create, cancel and delete writes one log of type `batch`. When the batch ends, each request gets its own log with its input, output, usage and cost. That log carries the request's `custom_id` as its custom identifier, the batch id in `metadata.batch_id`, and the `respan_params` you sent at create.

## Errors

| Status | When |
| --- | --- |
| `400` | The body is malformed, a `custom_id` repeats, `endpoint` does not match the model's provider, or the model's provider does not run batches through Respan. |
| `403` | The batch has more requests than your plan takes on Respan credits. |
| `404` | No batch has this id in your project, or the model is not available. |
| `409` | Results were read, or a delete was sent, while the batch is still running; or a cancel was sent after it ended. |
| `413` | The create body is over 32 MiB. |
| `429` | Too many batch requests are running on Respan credits, or the key's rate limit was reached. |
| `503` | The call could not be served right now. Retry it. |
