Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Batch APIs

Batch jobs are the other shape AI gateways now front: upload a file of requests, poll, download the results. The mock LLM serves the OpenAI-shaped files and batches API and the Anthropic-shaped message batches API, with the engine's deterministic answers, on a clock compressed from a day to seconds.

OpenAI shape
POST /ai/openai/v1/filesUpload an input file (multipart: purpose=batch, file as JSONL)
GET /ai/openai/v1/files, GET, DELETE /ai/openai/v1/files/{id}Your files
GET /ai/openai/v1/files/{id}/contentA file's content: your input, or a batch's output or error file
POST /ai/openai/v1/batchesStart a batch from an input file
GET /ai/openai/v1/batches, GET /ai/openai/v1/batches/{id}Your batches
POST /ai/openai/v1/batches/{id}/cancelCancel one
Anthropic shape
POST /ai/anthropic/v1/messages/batchesStart a batch (requests, each a custom_id and params)
GET /ai/anthropic/v1/messages/batches, GET .../batches/{id}Your batches
GET .../batches/{id}/resultsThe results as JSONL, once it ended
POST .../batches/{id}/cancelCancel one

A batch

The input file has one request a line:

{"custom_id": "greeting", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gpt-4o", "messages": [{"role": "user", "content": "hello"}]}}
{"custom_id": "echo", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "rustybin-echo", "messages": [{"role": "user", "content": "What did you receive?"}]}}
{"custom_id": "pii", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "rustybin-scripted", "messages": [{"role": "user", "content": "Show me the customer SSN"}]}}
POST {{base_url}}/ai/openai/v1/files
X-Rustybin-Session: docs-batch
[Multipart]
purpose: batch
file: file,batch-requests.jsonl;
HTTP 200
[Captures]
file_id: jsonpath "$.id"
[Asserts]
jsonpath "$.object" == "file"
jsonpath "$.purpose" == "batch"
jsonpath "$.filename" == "batch-requests.jsonl"
POST {{base_url}}/ai/openai/v1/batches
X-Rustybin-Session: docs-batch
X-Rustybin-Batch-Seconds: 1
X-Request-Id: docs-batch-1
{"input_file_id": "{{file_id}}", "endpoint": "/v1/chat/completions", "completion_window": "24h"}
HTTP 200
[Captures]
batch_id: jsonpath "$.id"
[Asserts]
jsonpath "$.object" == "batch"
jsonpath "$.status" == "validating"
jsonpath "$.request_counts.total" == 3
jsonpath "$.output_file_id" == null

A batch moves through the provider's states: validating, in_progress, finalizing, completed. Poll it:

GET {{base_url}}/ai/openai/v1/batches/{{batch_id}}
X-Rustybin-Session: docs-batch
[Options]
retry: 20
retry-interval: 200ms
HTTP 200
[Captures]
output_file: jsonpath "$.output_file_id"
[Asserts]
jsonpath "$.status" == "completed"
jsonpath "$.request_counts.completed" == 3
jsonpath "$.request_counts.failed" == 0
jsonpath "$.completed_at" isInteger
GET {{base_url}}/ai/openai/v1/files/{{output_file}}/content
X-Rustybin-Session: docs-batch
HTTP 200
[Asserts]
body contains "\"custom_id\":\"greeting\""
body contains "\"status_code\":200"
body contains "chat.completion"

Each line of the output file is {id, custom_id, response: {status_code, request_id, body}, error}, the body a chat completion from the engine: a model name or X-Rustybin-Mode on the batch request picks the mode for its lines.

The results are deterministic for the same input file, ids included: two batches of one file have the same output, byte for byte. A gateway that caches batch results can be shown to do so.

Only /v1/chat/completions lines are run; another endpoint is refused.

The clock

X-Rustybin-Batch-Seconds on the create request is the whole life of the batch (default 10, the ai.batch.seconds setting; at most 3600; 0 is done at once):

Share of the lifeStaterequest_counts
The first tenthvalidatingnothing done
Up to eight tenthsin_progresslines done evenly
The restfinalizingevery line done
After itcompletedoutput_file_id and error_file_id are set

The timestamps of a batch (in_progress_at, finalizing_at, completed_at) follow the same clock.

Lines that fail, and files that do not validate

X-Rustybin-Batch-Fail-Rate (a share from 0 to 1, or a percent) makes lines fail with the provider's error; X-Rustybin-Batch-Fail picks which (500 by default, 429, 503, 529, timeout). The lines are chosen by their custom_id, so the same file fails the same lines every time. Failed lines are in the error file:

POST {{base_url}}/ai/openai/v1/batches
X-Rustybin-Session: docs-batch
X-Rustybin-Batch-Seconds: 0
X-Rustybin-Batch-Fail-Rate: 1
X-Rustybin-Batch-Fail: 429
{"input_file_id": "{{file_id}}", "endpoint": "/v1/chat/completions", "completion_window": "24h"}
HTTP 200
[Captures]
error_file: jsonpath "$.error_file_id"
[Asserts]
jsonpath "$.status" == "completed"
jsonpath "$.request_counts.failed" == 3
jsonpath "$.output_file_id" == null

GET {{base_url}}/ai/openai/v1/files/{{error_file}}/content
X-Rustybin-Session: docs-batch
HTTP 200
[Asserts]
body contains "\"status_code\":429"
body contains "Rate limit reached"

A file with a line that is not JSON, has no custom_id (or one used twice), names another URL than the batch's endpoint or holds a request the chat API would refuse fails validation: the batch ends failed after its validating share, with the provider's errors list naming each line. An empty file fails the same way.

X-Rustybin-Batch-Outcome: expired lets a batch run out of time: in_progress until its life is over, then expired with half its lines done and in the output file.

Cancel

POST {{base_url}}/ai/openai/v1/batches
X-Rustybin-Session: docs-batch
X-Rustybin-Batch-Seconds: 2
{"input_file_id": "{{file_id}}", "endpoint": "/v1/chat/completions", "completion_window": "24h"}
HTTP 200
[Captures]
slow_batch: jsonpath "$.id"

POST {{base_url}}/ai/openai/v1/batches/{{slow_batch}}/cancel
X-Rustybin-Session: docs-batch
HTTP 200
[Asserts]
jsonpath "$.status" == "cancelling"

GET {{base_url}}/ai/openai/v1/batches/{{slow_batch}}
X-Rustybin-Session: docs-batch
[Options]
retry: 20
retry-interval: 100ms
HTTP 200
[Asserts]
jsonpath "$.status" == "cancelled"
jsonpath "$.cancelled_at" isInteger

A cancelled batch is cancelling for a tenth of its life, then cancelled; the lines done before the cancel are in its output file. A batch that already ended stays as it ended.

The Anthropic shape

POST {{base_url}}/ai/anthropic/v1/messages/batches
X-Rustybin-Session: docs-batch
X-Rustybin-Batch-Seconds: 0
{
  "requests": [
    {"custom_id": "first", "params": {"model": "claude-sonnet-4-5", "max_tokens": 64, "messages": [{"role": "user", "content": "hello"}]}},
    {"custom_id": "second", "params": {"model": "claude-sonnet-4-5", "messages": [{"role": "user", "content": "no max_tokens"}]}}
  ]
}
HTTP 200
[Captures]
message_batch: jsonpath "$.id"
[Asserts]
jsonpath "$.type" == "message_batch"
jsonpath "$.processing_status" == "ended"
jsonpath "$.request_counts.succeeded" == 1
jsonpath "$.request_counts.errored" == 1

GET {{base_url}}/ai/anthropic/v1/messages/batches/{{message_batch}}/results
X-Rustybin-Session: docs-batch
HTTP 200
[Asserts]
body contains "\"type\":\"succeeded\""
body contains "\"type\":\"errored\""
body contains "invalid_request_error"

A message batch is in_progress for its life, then ended (canceling in between when cancelled). Its request_counts are processing, succeeded, errored, canceled and expired. The results are one line a request: succeeded with the message, errored with the provider's error (a request whose params are wrong is its own errored result, as above; so is a line chosen by the fail rate), canceled or expired for what was not done. The batch headers above apply. Asking for the results before the batch ended is a 400.

What the mock saw

A batch is one record of the request log, kind: batch, under the create request's X-Request-Id: the tokens of all its lines as usage, and detail.lines with each line's custom_id, outcome, finish reason, tokens and the start of its text (at most 200 lines).

GET {{base_url}}/ai/requests/docs-batch-1?session=docs-batch
HTTP 200
[Asserts]
jsonpath "$.kind" == "batch"
jsonpath "$.detail.batch_id" == "{{batch_id}}"
jsonpath "$.detail.lines" count == 3
jsonpath "$.detail.lines[0].custom_id" == "greeting"
DELETE {{base_url}}/ai/openai/v1/files/{{file_id}}
X-Rustybin-Session: docs-batch
HTTP 200
[Asserts]
jsonpath "$.deleted" == true

Limits

Files and batches belong to their caller: the session (X-Rustybin-Session, else the client address), and the API key in shared mode. Another caller gets 404.

Files a caller20 (ai.batch.max_files), 8 MiB each and never more than the instance's body limit (RUSTYBIN_BODY_LIMIT, 1 MiB by default)
Batches a caller10 (ai.batch.max_batches); one more is refused with the provider's rate limit error
Requests a batch1000 (100 on a public instance)
KeptAn hour after the caller's last use
The instance256 callers and 256 MiB of files (64 and 32 MiB on a public instance)

On every surface

SurfaceWhere
APIThe routes above; a batch in GET /ai/requests and /ai/requests/{id}
ConsoleAI playground, Request log: a batch shows its lines
CLI and management MCPNothing to configure: this is the data plane. The caps are settings (rustybin settings set ai.batch.seconds 30, update_settings)
rustybin.yamlsettings: with ai.batch.max_files, ai.batch.max_batches and ai.batch.seconds