Batchrouter Docs

Submit a batch

Create a Batchrouter batch with POST /v1/batches — inline items or an uploaded JSONL manifest, a quote_id, an Idempotency-Key, routing, and privacy.

Not open yet

Sign-up is not open yet. The API routes for batches, chat completions and the OpenAI-compatible endpoints are not yet available.

Creating a batch submits your work for asynchronous processing and reserves credits against a quote. You send POST /v1/batches with either inline items or a reference to an uploaded file, accept a quote_id, and pass an Idempotency-Key so retries are safe.

This is the deep version of step 3 of the quickstart. It assumes you already have an API key (see Authentication) and a quote (see Quotes and pricing).

Before you start

You need two things in hand:

  • An API key, sent as Authorization: Bearer br_live_…. Keep it server-side.
  • A quote_id (qlock_…) from POST /v1/quotes/model. The quote is free, pins your price estimate, and is required to reserve credits before dispatch.

Quotes are free; credits are charged at batch creation. If your workspace lacks credits you'll get a 402 — check GET /v1/auth/account and top up credits in the dashboard at https://app.batchrouter.com/billing.

Choose your input: inline items or a file

A batch's work comes from exactly one of two sources — they are mutually exclusive:

  • items — an inline array of batch items. Best for small-to-medium batches you assemble in memory.
  • input_file_id — a file_… id from a JSONL file you uploaded with POST /v1/files and X-BatchRouter-Purpose: batch_input. Best for large batches: inline items are capped at 5,000 per request, a file is not. The file is either Batchrouter Sectioned v1 or standard OpenAI Batch lines; see JSONL format for both shapes and the file-based example below. A file uploaded without that purpose is rejected here with 400 invalid_input_file.

Send one or the other. Sending both — or neither — fails preflight with 400 and error.details.preflight. Which one to send is a size question; see Large batches.

The canonical item used across these docs:

{"customer_item_id":"item-1","operation": "chat.completions","model":"gpt-5.4-mini","input":{"messages":[{"role":"user","content":"Summarize: Batchrouter routes batch-AI workloads across providers."}]}}

gpt-5.4-mini is a model id from the live catalog. List the models available to you now, with per-provider pricing and capacity, via GET /v1/catalog/models.

Large batches

The rule for how to send a job depends only on its size:

Your jobSend it asWhy
Up to 5,000 items and a request body up to 20 MiBInline items in one POST /v1/batchesOne request, one JSON body, nothing to upload.
More than thatUpload the JSONL to POST /v1/files with purpose batch_input, then create with input_file_idThe file is checked and streamed to storage, and its requests are inserted in pages.

There is no item cap on an uploaded manifest; the 5,000-item cap applies to inline items only. The browser dashboard follows the same rule, so an API customer is never at a disadvantage, and an official SDK that makes the inline-versus-upload choice for you is being built.

The one ceiling every request meets is the per-request body size that Cloudflare enforces at the edge, before Batchrouter sees the request. A very large single upload can be refused there with a 413 that is not a Batchrouter error (no error.code in the body). The limit depends on the zone plan, so it is not printed here; for very large files, contact us.

A job above that size today: split the JSONL into several files and run one batch per file.

  1. Split the file at line boundaries. Mixed-model files are fine, and splitting by file does not change routing or price: each batch is quoted and routed on its own items, at the same rates.
  2. Upload each file to POST /v1/files with purpose batch_input and keep its file_id. To split a Sectioned v1 file, give each part its own header line, and when a cut falls inside a section, repeat that section's line at the top of the next part.
  3. Quote each file with POST /v1/quotes/model (one quote per batch), then create one batch per file with the same options, the batch's own quote_id, a fresh Idempotency-Key, and a shared metadata key, for example "metadata": { "run_id": "nightly-2026-10-16" }, so you can join the results afterwards.
  4. Poll each batch.id and merge the results by customer_item_id; keep those unique across the whole job, not just within a file.

The Idempotency-Key header

POST /v1/batches requires an Idempotency-Key header — a unique string of 8–128 characters per logical submission. If a network error leaves you unsure whether the batch was created, retry with the same key: Batchrouter returns the original batch instead of creating a duplicate. Use a fresh key for each new batch (a UUID works well).

Request options

Beyond the input and quote_id, the body accepts these optional fields:

FieldValuesDefaultWhat it does
sla_tierpriority · standard · relaxed · flex · economystandardHow fast the batch must finish: 4h, 24h, 48h, 72h or 7 days. Slower tiers cost less. See SLA tiers.
provider_preferencesobject—Provider controls: order, only, ignore, allow_fallbacks, and fallback ({ regions, providers }, who comes after the first choice, in what order). Also the filters data_collection, zdr, max_total_cost, the tiebreak sort and the preferred_* limits: see Provider preferences.
webhook{ url, secret }—Per-batch completion callback; secret is 8+ chars and signs X-BatchRouter-Signature.
metadataobject—Arbitrary key/value pairs echoed back on the batch.
input_item_countinteger—File-based only. The number of items the manifest should contain; a manifest that parses to a different count is rejected with 400 input_manifest_count_mismatch.
input_manifest_formatbatchrouter_sectioned_v1 · openai_batch_jsonl · batchrouter_items_jsonl (deprecated) · autoautoFile-based only. The shape of the file. The upload already detects it from the first line and stores it, so you rarely need this. See How the shape is detected.

Set these consistently with the quote_id you're accepting. If you quoted with a given max_price, create with the same intent so the reserved estimate matches the routing decision.

Create a batch (inline items)

  1. Assemble your items and accept the quote.

  2. Send the request with an Idempotency-Key.

    curl -X POST https://api.batchrouter.com/v1/batches \
      -H "Authorization: Bearer $BATCHROUTER_API_KEY" \
      -H "Content-Type: application/json" \
      -H "Idempotency-Key: $(uuidgen)" \
      -d '{
        "quote_id": "qlock_abc123",
        "sla_tier": "standard",
        "items": [
          {
            "customer_item_id": "item-1",
            "operation": "chat.completions",
            "model": "gpt-5.4-mini",
            "input": {
              "messages": [
                { "role": "user", "content": "Summarize: Batchrouter routes batch-AI workloads across providers." }
              ]
            }
          }
        ],
        "webhook": {
          "url": "https://example.com/hooks/batchrouter",
          "secret": "a-strong-shared-secret"
        },
        "metadata": { "project": "docs-demo" }
      }'
  3. Store batch.id (batch_…) and poll for completion. See Results.

Create a batch (file-based)

For a large batch, upload the requests as JSONL first, then quote and create with the returned id as input_file_id instead of items. Everything else about the create request is the same.

  1. Upload the JSONL to POST /v1/files with X-BatchRouter-Purpose: batch_input: raw body, Content-Type: application/x-ndjson, Content-Length required. The file is Batchrouter Sectioned v1 or standard OpenAI Batch lines; see JSONL format for the headers and both shapes.

    curl -X POST https://api.batchrouter.com/v1/files \
      -H "Authorization: Bearer $BATCHROUTER_API_KEY" \
      -H "X-BatchRouter-Purpose: batch_input" \
      -H "Content-Type: application/x-ndjson" \
      -H "Content-Length: $(wc -c < requests.jsonl)" \
      --data-binary @requests.jsonl
    # => { "file_id": "file_xyz789", "file": { "kind": "input_manifest", "item_count": 10000, ... } }

    POST /v1/files with X-BatchRouter-Purpose: batch_input stores a request file (kind: "input_manifest") for input_file_id. A file without that purpose cannot be used as input_file_id; POST /v1/batches rejects it with 400 invalid_input_file. The older POST /v1/batches/input-manifests is a deprecated alias of the batch_input upload.

  2. Quote with POST /v1/quotes/model, sending input_file_id and input_item_count where you would send items. The quote reads the file and prices every request.

  3. Create the batch with the same input_file_id, the quote_id, and an Idempotency-Key.

    curl -X POST https://api.batchrouter.com/v1/batches \
      -H "Authorization: Bearer $BATCHROUTER_API_KEY" \
      -H "Content-Type: application/json" \
      -H "Idempotency-Key: $(uuidgen)" \
      -d '{
        "quote_id": "qlock_abc123",
        "input_file_id": "file_xyz789",
        "input_item_count": 10000,
        "sla_tier": "standard"
      }'

A bad line fails with 400 and the line number, before any credits are reserved. Today that check runs when you quote and create; it may move to upload time, so do not rely on the upload succeeding as proof that the file is valid.

The 202 response

A successful create returns 202 Accepted — the batch is queued for routing, not yet processed. The body wraps the batch summary (abridged here; the full batch object also carries sla_tier, pricing_estimate, file ids, and more):

{
  "batch": {
    "id": "batch_9f3c2a1b7e4d46c8a1b2c3d4e5f60718",
    "state": "priced",
    "counts": {
      "total": 1,
      "pending": 1,
      "running": 0,
      "completed": 0,
      "failed": 0,
      "canceled": 0
    },
    "created_at": "2026-06-18T10:00:00Z",
    "deadline_at": "2026-06-19T10:00:00Z"
  }
}
FieldMeaning
batch.idThe batch_… identifier you poll and fetch results with.
batch.stateThe lifecycle field. A new batch is priced, then advances queued → dispatching → running → finalizing → completed. See the status lifecycle.
batch.countsPer-item state counts: total, pending, running, completed, failed, and canceled. counts.total is the number of items accepted into the batch.
batch.created_atWhen the batch was created.
batch.deadline_atThe 24-hour SLA deadline by which the batch is expected to complete.

Replaying the same Idempotency-Key returns this same response rather than creating a second batch.

Errors

StatusCauseFix
400Preflight failed (bad JSONL, both/neither input, context window exceeded, unsafe webhook URL).Inspect error.details.preflight; see JSONL format.
400 invalid_input_fileinput_file_id names a file that is not a request file.Upload the requests with POST /v1/files and X-BatchRouter-Purpose: batch_input, and use that id.
400 input_manifest_count_mismatchThe file holds a different number of requests than input_item_count.Re-upload the file, or fix the count.
401Missing or invalid key.Check your Authorization header — see Authentication.
402Insufficient credits.Check GET /v1/auth/account; top up credits in the dashboard at https://app.batchrouter.com/billing.
409Conflict (e.g. reused key on a different payload, or a quote_id whose work no longer matches the request).Use a fresh Idempotency-Key for a new batch; request a new quote for changed work.
411 · 415File upload without Content-Length, or a batch_input upload with a non-JSONL Content-Type.Send Content-Length and Content-Type: application/x-ndjson to POST /v1/files.

Next steps