Batch API
Send many chat requests in one file and get the results within 24 hours, at half the price.
When to use batch
| Need | Batch | Synchronous API |
|---|---|---|
| Workload | Independent offline tasks, such as dataset enrichment | Interactive responses and multi-step conversations |
| Delivery | JSONL result files; a 24-hour processing window | A response to each call, optionally streamed |
| Price | 50% of standard token rates | Standard token rates |
Models and pricing
| Model | Input | Cached input | Output |
|---|---|---|---|
GLM 5.3 Flash glm5.3-flash | $0.075 | $0.0075 | $0.25 |
Qwen3.8 27B qwen3.8-27b | $0.10 | $0.025 | $1.25 |
Per million tokens: half the standard price, shown struck through. Each file holds up to 50,000 requests and 100 MB, on /v1/chat/completions. Rows not finished within 24 hours expire.
Batch is not zero data retention. Live requests to these models store nothing. A batch has to hold your requests until it runs, so we keep them until it finishes, then delete our copies. Your input and result files stay until you delete them, or for 31 days.
Quickstart
Classify two support tickets in a batch. Batch uses the OpenAI SDK you already know: point it at https://api.reka.ai/v1 with your Reka API key. Each step below continues the same script.
pip install openai
export REKA_API_KEY="your_reka_api_key"1. Write your requests
Save one request per line as requests.jsonl. Each line is a normal Chat Completions body plus a custom_id, which you use to match answers back to requests.
{"custom_id":"ticket-001","method":"POST","url":"/v1/chat/completions","body":{"model":"qwen3.8-27b","messages":[{"role":"user","content":"Reply with one word, billing or technical. Ticket: Update my invoice address."}],"max_tokens":64}}
{"custom_id":"ticket-002","method":"POST","url":"/v1/chat/completions","body":{"model":"qwen3.8-27b","messages":[{"role":"user","content":"Reply with one word, billing or technical. Ticket: My application cannot connect."}],"max_tokens":64}}
2. Upload the file
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.reka.ai/v1", api_key=os.environ["REKA_API_KEY"])
batch_file = client.files.create(file=open("requests.jsonl", "rb"), purpose="batch")
print(batch_file.id)3. Create the batch
batch = client.batches.create(
input_file_id=batch_file.id,
endpoint="/v1/chat/completions",
completion_window="24h",
)
print(batch.id, batch.status)The batch starts as validating, then in_progress. Results arrive within the 24-hour window.
4. Wait for it to finish
import time
while batch.status not in ("completed", "failed", "expired", "cancelled"):
time.sleep(30)
batch = client.batches.retrieve(batch.id)
print(batch.status, batch.request_counts)5. Read the results
import json
if batch.output_file_id:
for line in client.files.content(batch.output_file_id).text.splitlines():
row = json.loads(line)
answer = row["response"]["body"]["choices"][0]["message"]["content"]
print(row["custom_id"], answer)
if batch.error_file_id:
print(client.files.content(batch.error_file_id).text)ticket-001 billing
ticket-002 technicalGood to know
- Match by
custom_id. Result rows can come back in any order. glm5.3-flashthinks before it answers. For short labels set"reasoning_effort": "none", or givemax_tokensroom (1,024 or more); otherwise thinking can use the whole budget andcontentcomes back empty.- Check the error file. A
completedbatch can still have failed rows; they are inerror_file_id. See working with results. - Retry create safely with an
Idempotency-Keyheader: a retry returns the same batch instead of a second one. - Queue full? HTTP 429
batch_queue_fullmeans wait forRetry-Afterand create again with the same file; no need to re-upload. - Cancel with
batches.cancel(batch_id). Finished rows are kept. - Download results within 31 days. Files are deleted after that.
Running at scale
- Labelling a spreadsheet? Follow the CSV recipe.
- For long or unattended runs, a resumable program saves its state and can be rerun safely after a crash: batch.py or batch.mts. Run it with
python batch.py --input requests.jsonl --state-dir run1.