Batch، درخواستهای انبوه با نصف قیمت
مدلهایی که شناسهشان با :batch تمام میشود (مثلاً openai/gpt-6-luna:batch) نسخهٔ دستهای همان مدلاند: حدود نصف قیمت، ولی پاسخ فوری نمیدهند. تا ۱٬۰۰۰ درخواست چت یا embeddings را با POST /v1/batches یکجا بفرستید، هر دقیقه با GET /v1/batches/{id} وضعیت را بپرسید، و وقتی status برابر completed شد، پاسخها را از آرایهٔ results بخوانید و با custom_id به درخواستهایتان وصل کنید. بیشتر دستهها در چند دقیقه تمام میشوند و سقف زمان ۲۴ ساعت است.
چه وقت Batch مناسب است؟
وقتی کار زیادی دارید و عجله ندارید: دستهبندی هزاران نظر، ترجمهٔ متنهای طولانی، خلاصهسازی اسناد، ساخت embeddings برای جستجو، یا ارزیابی مدل روی یک مجموعه داده. برای چت زنده با کاربر، همان /v1/chat/completions را با مدل عادی صدا بزنید.
مراحل
۱. یک مدل :batch از کاتالوگ انتخاب کنید. ۲. درخواستها را با endpoint (/v1/chat/completions یا /v1/embeddings)، model و آرایهٔ requests بفرستید؛ هر درخواست یک custom_id یکتا و یک body دارد که همان بدنهٔ درخواست عادی است (بدون stream). ۳. پاسخ 202 شناسهٔ batch_… را برمیگرداند. ۴. هر ۳۰ تا ۶۰ ثانیه GET /v1/batches/{id} بزنید: validating ← in_progress ← finalizing ← completed. ۵. در results هر مورد custom_id، status_code و body (همان پاسخ endpoint عادی) یا error دارد. ترتیب نتایج ثابت نیست.
هزینه
هنگام ارسال، برآورد هزینهٔ کل دسته از اعتبار بلوکه میشود و وقتی دسته تمام شد، فقط هزینهٔ واقعی دسته کسر میشود. درخواستهایی که خطا بخورند هزینه ندارند؛ اگر کل دسته خطا بخورد یا منقضی شود، کل مبلغ آزاد میشود. برای چت max_tokens را تعیین کنید تا مبلغ بلوکهشده کوچک بماند.
محدودیتها
حداکثر ۱٬۰۰۰ درخواست و ۵۰ مگابایت در هر دسته، یک مدل و یک endpoint برای کل دسته، و حداکثر ۱۰ دستهٔ در حال اجرا همزمان. دستهی در حال اجرا لغو نمیشود؛ بعد از پایان میتوانید با DELETE /v1/batches/{id} حذفش کنید. نتایج ۳۰ روز در دسترساند. فرستادن مدل :batch به /v1/chat/completions خطای 400 BATCH_MODEL_REQUIRES_BATCHES_API میدهد.
/v1/batchesاحراز هویت BearerCreate a batch
Submit up to 1,000 chat or embeddings requests for a :batch model.
Returns 202 at once with the batch id; results arrive within 24
hours (usually minutes).
Billing: the estimated cost of the whole batch is held from your
credit at submit, and you are charged the actual batch price when it
finishes. A batch that fails or expires releases the hold. Set
max_tokens on chat requests: without it each request is held at the
default completion limit.
A :batch model sent to /v1/chat/completions or /v1/embeddings
directly returns 400 BATCH_MODEL_REQUIRES_BATCHES_API.
پارامترها
| فیلد | in | نوع | توضیح |
|---|---|---|---|
Idempotency-Key | header | string | Optional deduplication token (any string up to 200 characters). How it works:
When to use: Add this header when you want to safely retry a request after a network timeout without risking a duplicate operation. |
بدنه درخواستالزامی
| فیلد | نوع | توضیح |
|---|---|---|
endpointالزامی | string | The endpoint every request in the batch runs against. مقادیر مجاز: |
modelالزامی | string | A نمونه: |
requestsالزامی | array of object | Up to 1,000 requests. Each |
custom_idالزامی | string | Your id for this request; results are matched by it. |
bodyالزامی | object | The same body you would send to |
نمونه
پاسخها
202Batch accepted.
400The request body or query parameters are invalid. Check the
error.code and error.message fields for details. Codes the
gateway emits at 400:
BAD_REQUEST: generic malformed JSON / missing required field.INVALID_TOOLS: thetoolsarray,pluginsfield, or model's:onlinesuffix uses an unsupported shape. The response names the offender and links to the supported form. See the dedicatedInvalidToolsresponse below for the three triggers and migration hints.UNSUPPORTED_TOOL: a built-in tool (file_search,code_interpreter,computer_use,mcp) whose pricing dimension is not yet wired into the gateway hold estimator.INVALID_AUDIO_FORMAT:input_audio.formatvalue is not in the supported set. The envelope lists every supported codec name. The gateway accepts both short names (wav,webm) and full MIME aliases (audio/wav,audio/webm); anything else fails fast at admission. See API guide §8b.AUDIO_NOT_SUPPORTED_ON_RESPONSES:input_audiocontent part sent toPOST /v1/responses. The Responses API has no audio content type today; resend toPOST /v1/chat/completionsinstead.
401Authentication failed. Either the Authorization header is missing,
the key format is invalid, or the key doesn't match any active account.
{
"error": {
"code": "UNAUTHORIZED",
"message": "Authentication required",
"status": 401,
"type": "authentication_error"
}
}402Not enough credit to hold the batch's estimated cost.
404The requested resource was not found. Either the ID is wrong or it belongs to a different account.
429Too many requests. Back off and retry after a short delay.
Check the Retry-After header for guidance.
503The upstream AI provider is temporarily
unavailable. Retry with exponential backoff (1s, 2s, 4s) using the
same Idempotency-Key: if the upstream actually completed
the work and only the response failed, the gateway replays the
cached success without a second charge. Holds are released
automatically on upstream failures.
/v1/batchesاحراز هویت BearerList batches
Your batches, newest first, without results.
پارامترها
| فیلد | in | نوع | توضیح |
|---|---|---|---|
limit | query | integer | |
after | query | string | The |
نمونه
پاسخها
200A page of batches.
401Authentication failed. Either the Authorization header is missing,
the key format is invalid, or the key doesn't match any active account.
{
"error": {
"code": "UNAUTHORIZED",
"message": "Authentication required",
"status": 401,
"type": "authentication_error"
}
}/v1/batches/{id}احراز هویت BearerGet a batch
Returns the batch's status. Once it has finished, results holds one
entry per request. Poll every 30-60 seconds; a batch takes minutes
to hours. Results stay available for 30 days.
پارامترها
| فیلد | in | نوع | توضیح |
|---|---|---|---|
idالزامی | path | string |
نمونه
پاسخها
200Current batch state.
401Authentication failed. Either the Authorization header is missing,
the key format is invalid, or the key doesn't match any active account.
{
"error": {
"code": "UNAUTHORIZED",
"message": "Authentication required",
"status": 401,
"type": "authentication_error"
}
}404The requested resource was not found. Either the ID is wrong or it belongs to a different account.
429Too many requests. Back off and retry after a short delay.
Check the Retry-After header for guidance.
503The upstream AI provider is temporarily
unavailable. Retry with exponential backoff (1s, 2s, 4s) using the
same Idempotency-Key: if the upstream actually completed
the work and only the response failed, the gateway replays the
cached success without a second charge. Holds are released
automatically on upstream failures.
/v1/batches/{id}احراز هویت BearerDelete a batch
Deletes a finished batch and its results. A batch that is still
running cannot be cancelled or deleted (409 BATCH_NOT_FINISHED).
پارامترها
| فیلد | in | نوع | توضیح |
|---|---|---|---|
idالزامی | path | string |
نمونه
پاسخها
200Deleted.
401Authentication failed. Either the Authorization header is missing,
the key format is invalid, or the key doesn't match any active account.
{
"error": {
"code": "UNAUTHORIZED",
"message": "Authentication required",
"status": 401,
"type": "authentication_error"
}
}404The requested resource was not found. Either the ID is wrong or it belongs to a different account.
409Idempotency key conflict: you sent the same Idempotency-Key with
a different request body.
503The upstream AI provider is temporarily
unavailable. Retry with exponential backoff (1s, 2s, 4s) using the
same Idempotency-Key: if the upstream actually completed
the work and only the response failed, the gateway replays the
cached success without a second charge. Holds are released
automatically on upstream failures.
سوالات متداول
- چرا مدل :batch در چت عادی کار نمیکند؟
نسخهٔ batch فقط به صورت دستهای اجرا میشود و پاسخ فوری ندارد. آن را از طریق
POST /v1/batchesبفرستید، یا برای پاسخ فوری همان مدل را بدون:batchصدا بزنید.- دسته چقدر طول میکشد؟
معمولاً چند دقیقه؛ سقف آن ۲۴ ساعت است. اگر دسته در این مدت تمام نشود، منقضی میشود و مبلغ بلوکهشده آزاد میگردد.
- اگر بعضی درخواستها خطا بخورند؟
دسته
completedمیشود وrequest_counts.failedتعداد خطاها را نشان میدهد. هر مورد خطادار درresultsیکerrorدارد و هزینهای برایش کسر نمیشود.