Skip to main content

OpenAI-compatible-Batch Chat

For scenarios that do not require real-time responses, such as data annotation and content generation, real-time inference APIs suffer from high costs and limited concurrency. The Batch Chat API of Alibaba Cloud Model Studio retains the same synchronous invocation style as the real-time API—you simply send a request and wait for the final result to be returned. This feature currently enjoys a limited-time 50% discount on the official website, directly reducing your inference costs by 50%.

This interface only supports submitting a single request. To submit multiple requests at once, you can submit them via a file. For details, see OpenAI-compatible-Batch (file input).

How it works

  1. Request submission: The client sends a request and establishes a connection.
  2. Connection maintenance: The request enters a queue, and the client keeps the connection open while waiting.
  3. Synchronous return: Once the request has been processed, the server returns the complete result to the client in one pass over the previously held connection.
    If the maximum waiting time is exceeded, the connection is automatically closed and a timeout error is returned.

Scope

  • China (Beijing)
  • Text generation models: qwen4-max, qwen3.8-max, qwen3.8-flash, qwen3.7-max, qwen3.7-plus, qwen3.6-plus, qwen3.7-flash, qwen3.6-flash, qwen3.5-plus, qwen3.5-flash, qwen-plus, qwen-flash
  • Image and video understanding models: qwen4-max, qwen3.8-max, qwen3.8-flash, qwen3.7-plus, qwen3.6-plus, qwen3.7-flash, qwen3.6-flash, qwen3.5-plus, qwen3.5-flash, qwen3.5-omni-plus, qwen3.5-omni-flash, qwen3-vl-plus
  • In Batch scenarios, qwen3.8-max, qwen3.8-flash, qwen3.7-max, qwen3.7-plus, qwen3.6-plus, qwen3.7-flash, qwen3.6-flash, qwen3.5-plus, qwen3.5-flash, qwen3.5-omni-flash, and qwen3.5-omni-plus support a maximum of 256K context tokens per request; qwen3.5-omni-plus and qwen3.5-omni-flash do not support audio output.
  • Some models support the thinking mode; when enabled, it produces thinking tokens, which increases costs.
  • The qwen3.8, qwen3.7, qwen3.6, and qwen3.5 series models have the thinking mode enabled by default. When using hybrid-thinking models, we recommend explicitly setting the enable_thinking parameter (true to enable / false to disable).
  • In the JSONL request body, enable_thinking is a top-level parameter of body and must be passed at the same level as model; it cannot be placed inside extra_body.

How to use

Prerequisites

pip3 install -U openai

Step 1: Configure the API endpoint

Simply change the API endpoint (base_url) to easily switch your existing real-time inference requests to batch inference. Configure the correct API endpoint according to your invocation method. SDK configuration: Set base_url to https://batch.dashscope.aliyuncs.com/compatible-mode/v1 HTTP invocation: The request endpoint is POST https://batch.dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

Step 2: Make the call

This section uses a text conversation as an example to show how to call the Batch Chat API. The default waiting timeout for a request is 3600 seconds (1 hour); in most cases, no additional configuration is required.
The following example shows how to proactively set a custom timeout (60-3600 seconds) for reference.
  • Python
  • Java
  • Node.js
  • Go
  • C#(HTTP)
  • PHP(HTTP)
  • curl
Sample request
import os
from openai import OpenAI

client = OpenAI(
   # If the environment variable is unset, insert your Model Studio API key: api_key="sk-xxx". Avoid hardcoding API keys in production to reduce the risk of key exposure.
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://batch.dashscope.aliyuncs.com/compatible-mode/v1",  # Model Studio Batch chat API URL
).with_options(timeout=1800.0) # Set the wait time to 1800 seconds (30 minutes); maximum: 3600 seconds

completion = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "你是谁?"},
    ]
)
print(completion.choices[0].message.content)
Sample response
我是千问,阿里巴巴集团旗下的超大规模语言模型。我能够回答问题、创作文字,比如写故事、写公文、写邮件、写剧本、逻辑推理、编程等等,还能表达观点,玩游戏等。如果你有任何问题或需要帮助,欢迎随时告诉我!

Limits

  • Waiting time: After a request is submitted, the result is awaited synchronously, and the server keeps the connection open for up to 3600 seconds (1 hour). You can set a custom timeout based on your actual needs, with a valid range of 60-3600 seconds.
  • Concurrency limit: A single account can maintain up to 10,000 pending requests per model. New requests that exceed this limit are rejected with a corresponding error code until pending requests complete and free up available slots.
  • Request rate: A single account can submit requests at a maximum rate of 1000 QPS (10,000 requests / 10 seconds).
    This limit is a theoretical maximum set by the system. In actual usage, the API's available resources are dynamically affected by the overall system load. We recommend implementing retry logic in your code.

Billing Details

  • Billing unit price: You are billed for the input and output tokens of successful requests, at the same rates as real-time model invocations. For a limited time, the official website offers a 50% discount for Batch Chat calls, with the final fee being 50% of the real-time invocation price. For more information, see Models.
  • Billing scope: Only requests that are successfully executed in a task are billed. Any failed requests (including system errors or timeouts) are not billed.
Batch inference is a separate billing item and does not support upfront payment (savings plans), the new-user free quota, or features such as context caching.

Error codes

If a model invocation fails and returns an error message, see Error codes for troubleshooting.

FAQ

  1. Is there a difference in processing time between Batch Chat requests and real-time APIs? Yes. Batch Chat requests must queue for scheduling, so the end-to-end processing time is usually longer than that of real-time APIs. The maximum waiting time on the server side is 1 hour. If a request is not executed within this time, the connection is automatically closed and an error is returned.
  2. How do I choose between Batch Chat and Batch File? Choose Batch Chat when your business logic requires submitting a large number of independent conversation requests with high concurrency via synchronous API calls. Choose Batch File when you need to process a single large file containing many requests and can accept asynchronously retrieving the result file.
  3. Does Batch Chat guarantee that all requests are completed? No. Batch Chat uses Batch resources, and completion depends on system resource allocation. If system resources are busy, requests may wait in the queue. If a request is still not scheduled for execution after the maximum waiting time, the connection times out and closes. In this case, the request is not billed, and you can retry later.