همه صفحه‌ها

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 می‌دهد.

POST/v1/batchesاحراز هویت Bearer

Create 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-Keyheaderstring

Optional deduplication token (any string up to 200 characters).

How it works:

  • Same key + same request body → cached response replayed (safe retry)
  • Same key + different body → 409 CONFLICT error
  • Keys expire after 24 hours

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.

مقادیر مجاز: /v1/chat/completions · /v1/embeddings

modelالزامیstring

A :batch model id from the catalogue (for example openai/gpt-6-luna:batch). The bare id is accepted too. One model for the whole batch.

نمونه: openai/gpt-6-luna:batch

requestsالزامیarray of object

Up to 1,000 requests. Each custom_id must be unique.

custom_idالزامیstring

Your id for this request; results are matched by it.

bodyالزامیobject

The same body you would send to endpoint. model may be omitted (it must match the batch model if present); stream is not supported.

نمونه

پاسخ‌ها

202

Batch accepted.

400

The 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: the tools array, plugins field, or model's :online suffix uses an unsupported shape. The response names the offender and links to the supported form. See the dedicated InvalidTools response 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.format value 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_audio content part sent to POST /v1/responses. The Responses API has no audio content type today; resend to POST /v1/chat/completions instead.
401

Authentication 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"
  }
}
402

Not enough credit to hold the batch's estimated cost.

404

The requested resource was not found. Either the ID is wrong or it belongs to a different account.

429

Too many requests. Back off and retry after a short delay. Check the Retry-After header for guidance.

503

The 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.

GET/v1/batchesاحراز هویت Bearer

List batches

Your batches, newest first, without results.

پارامترها

فیلدinنوعتوضیح
limitqueryinteger
afterquerystring

The last_id of the previous page.

نمونه

پاسخ‌ها

200

A page of batches.

401

Authentication 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"
  }
}
GET/v1/batches/{id}احراز هویت Bearer

Get 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الزامیpathstring

نمونه

پاسخ‌ها

200

Current batch state.

401

Authentication 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"
  }
}
404

The requested resource was not found. Either the ID is wrong or it belongs to a different account.

429

Too many requests. Back off and retry after a short delay. Check the Retry-After header for guidance.

503

The 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.

DELETE/v1/batches/{id}احراز هویت Bearer

Delete 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الزامیpathstring

نمونه

پاسخ‌ها

200

Deleted.

401

Authentication 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"
  }
}
404

The requested resource was not found. Either the ID is wrong or it belongs to a different account.

409

Idempotency key conflict: you sent the same Idempotency-Key with a different request body.

503

The 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 دارد و هزینه‌ای برایش کسر نمی‌شود.

به‌روزرسانی: 2026-11-25