Skip to main content
Text generation

Deep thinking

Deep thinking models reason before responding, improving accuracy on complex tasks like logical reasoning and math.

These examples use the OpenAI-compatible Chat Completion API and DashScope API. For the Responses API, see Deep thinking .

Usage

Model Studio supports deep thinking in two modes:
  • Hybrid thinking mode: Use the enable_thinking parameter to switch between thinking and non-thinking on a per-request basis:
    • Set to true — the model reasons before responding.
    • Set to false — the model responds directly, skipping the reasoning step.

    OpenAI compatible

# Import dependencies and create a client...
completion = client.chat.completions.create(
    model="qwen3.8-max", # Select a model
    messages=[{"role": "user", "content": "Who are you"}],
    # Since enable_thinking is not a standard OpenAI parameter, pass it in extra_body.
    extra_body={"enable_thinking":True},
    # Enable streaming output.
    stream=True,
    # Configure the stream to include token consumption information in the last data packet.
    stream_options={
        "include_usage": True
    }
)

DashScope

The DashScope API for the Qwen3.5 series uses a multimodal interface. The following example returns a url error . For the correct usage, see Enable or disable thinking mode .
# Import dependencies...

response = MultiModalConversation.call(
    # If you have not set the environment variable, replace the next line with your Model Studio API key, for example: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # You can use other deep thinking models as needed.
    model="qwen3.8-max",
    messages=messages,
    enable_thinking=True,
    stream=True,
    incremental_output=True
)
  • Thinking-only mode: The model always reasons before responding — this behavior cannot be disabled. The request format is the same as hybrid thinking mode; no enable_thinking parameter is needed.
The API returns reasoning in the reasoning_content field and the answer in the content field. Because reasoning adds latency, all examples use streaming by default (recommended, so you can watch the reasoning in real time). Commercial thinking models also support non-streaming (synchronous) output; see the FAQ below for usage and caveats. Some models (such as the open-source qwen3-235b-a22b and qwen3-32b) support streaming only, and a non-streaming call returns an error.

Supported models

  • Qwen3.8
  • Qwen3.7
  • Qwen3.6
  • Qwen3.5
  • Qwen3
  • QwQ (based on Qwen2.5)
  • DeepSeek
  • GLM
  • Kimi
  • MiniMax
Qwen3.8 Max series (hybrid thinking mode, thinking enabled by default): qwen3.8-maxQwen3.8 Flash series (hybrid thinking mode, thinking enabled by default): qwen3.8-flash
Model names, context windows, pricing, and snapshot versions are in the Model list. Rate limits are described in Rate limiting.

Getting started

Obtain an API key and set it as an environment variable. If you use an SDK, install the OpenAI or DashScope SDK (DashScope Java SDK version 2.19.4 or later is required). The following example calls qwen3.8-max in thinking mode with streaming output.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • HTTP

Sample code

from openai import OpenAI
import os

# Initialize the OpenAI client.
client = OpenAI(
    # API keys vary by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If an environment variable is not configured, provide your Model Studio API key directly: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Configurations vary by region. Modify the base_url based on your region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

messages = [{"role": "user", "content": "Who are you"}]

completion = client.chat.completions.create(
    model="qwen3.8-max",  # You can replace this with other deep-thinking models as needed.
    messages=messages,
    extra_body={"enable_thinking": True},
    stream=True,
    stream_options={
        "include_usage": True
    },
)

reasoning_content = ""  # Full thinking process
answer_content = ""  # Full response
is_answering = False  # Tracks if the response phase has started
print("\n" + "=" * 20 + "Thinking process" + "=" * 20 + "\n")

for chunk in completion:
    if not chunk.choices:
        print("\nUsage:")
        print(chunk.usage)
        continue

    delta = chunk.choices[0].delta

    # Collect only the reasoning content.
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
        reasoning_content += delta.reasoning_content

    # When content is received, start responding.
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "Full response" + "=" * 20 + "\n")
            is_answering = True
        print(delta.content, end="", flush=True)
        answer_content += delta.content

Response

====================Thinking process====================

The user's query "Who are you?" requires an accurate and friendly response. The answer should first establish my identity as Qwen, developed by Tongyi Lab at Alibaba Cloud. It will then outline key capabilities such as question answering, text generation, and logical reasoning. The language must be simple and the tone approachable. To encourage interaction, I will invite the user to ask more questions. Finally, I'll check that all key details are present, including my name (Qwen) and developer, to provide a comprehensive answer.
====================Full response====================

Hello! I am Qwen, a large language model developed by Tongyi Lab at Alibaba Group. I can answer questions, generate text, perform logical reasoning, write code, and more, to provide you with high-quality information and services. You can call me Qwen. How can I help you?

Core capabilities

Toggle thinking and non-thinking modes

Thinking mode improves response quality but adds latency and cost. On hybrid thinking models, toggle it per request based on query complexity:
  • Set enable_thinking to false for simple queries — casual conversation, straightforward Q&A.
  • Set enable_thinking to true for complex reasoning — logic problems, code generation, or math.
  • OpenAI compatible
  • DashScope
enable_thinking is not a standard OpenAI parameter. In the OpenAI Python SDK, pass it via extra_body. In the Node.js SDK, pass it as a top-level parameter.
  • Python
  • Node.js
  • HTTP

Sample code

from openai import OpenAI
import os

# Initialize the OpenAI client.
client = OpenAI(
    # If the environment variable is not configured, replace the value with your Model Studio API key: api_key="sk-xxx"
    # API keys differ by region. To get an API key, see https://www.alibabacloud.com/help/en/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. When calling, replace WorkspaceId with your actual workspace ID. URLs vary by region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

messages = [{"role": "user", "content": "Who are you?"}]
completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=messages,
    # Set enable_thinking in extra_body to enable the reasoning process.
    extra_body={"enable_thinking": True},
    stream=True,
    stream_options={
        "include_usage": True
    },
)

reasoning_content = ""  # Full reasoning process
answer_content = ""  # Full response
is_answering = False  # Indicates if the response phase has started
print("\n" + "=" * 20 + "Reasoning process" + "=" * 20 + "\n")

for chunk in completion:
    if not chunk.choices:
        print("\n" + "=" * 20 + "Token usage" + "=" * 20 + "\n")
        print(chunk.usage)
        continue

    delta = chunk.choices[0].delta

    # Collect only the reasoning content.
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
        reasoning_content += delta.reasoning_content

    # When content is received, start the response.
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "Full response" + "=" * 20 + "\n")
            is_answering = True
        print(delta.content, end="", flush=True)
        answer_content += delta.content

Response

====================Reasoning process====================

The user is asking "Who are you?". I need to determine what they want to know. They might be interacting with me for the first time or want to confirm my identity. I should introduce myself as Qwen, developed by Tongyi Lab. Then, I should explain my capabilities, such as answering questions, creating text, and coding, so the user understands how I can assist them. I should also mention my multilingual support so international users know they can communicate in different languages. Finally, I should maintain a friendly tone and invite them to ask questions to encourage further interaction. The explanation must be clear and simple, avoiding technical jargon. The user likely wants a quick overview of my abilities, so I will focus on my functions and applications. I should also consider if any information is missing, such as mentioning Alibaba Group or more technical details. However, the user probably only needs basic information. I will ensure the response is friendly and professional, and encourages them to continue the conversation.
====================Full response====================

I am Qwen, a large-scale language model developed by Tongyi Lab. I can help you answer questions, create text, code, and express opinions. I support communication in multiple languages. How can I help you?
====================Token usage====================

CompletionUsage(completion_tokens=221, prompt_tokens=10, total_tokens=231, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=None, audio_tokens=None, reasoning_tokens=172, rejected_prediction_tokens=None), prompt_tokens_details=PromptTokensDetails(audio_tokens=None, cached_tokens=0))
For the Qwen3 open-source hybrid thinking models, along with the qwen-plus-2025-04-28 and models, you can also control thinking mode with prompt suffixes. When enable_thinking is true, append /no_think to a prompt to skip reasoning for that turn, or append /think to re-enable it. The model always follows the most recent /think or /no_think instruction.

Limit thinking length

Reasoning traces increase latency and token costs. Use thinking_budget to cap reasoning tokens. When the limit is reached, the model stops reasoning and responds immediately. This applies to Qwen3.8, Qwen3.7, Qwen3.6, Qwen3.5, Qwen3-VL, Qwen3, GLM and Kimi models.
thinking_budget defaults to the model's maximum chain-of-thought length. Check the default for each model on its console page.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • HTTP

Sample code

from openai import OpenAI
import os

# Initialize the OpenAI client.
client = OpenAI(
    # If the environment variable is not configured, replace "sk-xxx" with your Model Studio API key.
    # API keys are region-specific. To get an API key, visit https://www.alibabacloud.com/help/en/model-studio/get-api-key.
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Configurations vary by region. Modify the base_url according to your region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

messages = [{"role": "user", "content": "Who are you"}]

completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=messages,
    # The enable_thinking parameter enables the thinking process, and thinking_budget sets its token limit.
    extra_body={
        "enable_thinking": True,
        "thinking_budget": 50
        },
    stream=True,
    stream_options={
        "include_usage": True
    },
)

reasoning_content = ""  # Complete thinking process
answer_content = ""  # Complete response
is_answering = False  # Tracks if the response phase has started
print("\n" + "=" * 20 + "Thinking process" + "=" * 20 + "\n")

for chunk in completion:
    if not chunk.choices:
        print("\nUsage:")
        print(chunk.usage)
        continue

    delta = chunk.choices[0].delta

    # Collect only the thinking content.
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
        reasoning_content += delta.reasoning_content

    # When content is received, the response phase begins.
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "Complete response" + "=" * 20 + "\n")
            is_answering = True
        print(delta.content, end="", flush=True)
        answer_content += delta.content

Response

====================Thinking process====================

Okay, the user is asking, "Who are you?" I need to provide a clear and friendly response. First, I should state my identity as Qwen, developed by Tongyi Lab at Alibaba Group. Next, I need to explain my main functions, such as answering
====================Complete response====================

I am Qwen, a large-scale language model developed by Tongyi Lab at Alibaba Group. I can answer questions, create text, perform logical reasoning, and write code.

Pass the thinking process

By default, the model ignores reasoning_content in the messages history. Set preserve_thinking to true to pass prior reasoning to subsequent turns. The reasoning_content from earlier assistant messages is then appended to the model's input.
The preserve_thinking parameter is only supported for qwen3.8-max, qwen3.8-flash, qwen3.7-max, qwen3.7-max-us, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.7-max-preview, qwen3.7-max-2026-05-17, qwen3.7-plus, qwen3.7-plus-us, qwen3.7-plus-2026-05-26, qwen3.6-max-preview, qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.7-flash, qwen3.7-flash-2026-07-15, kimi-k2.7-code (deployed on Alibaba Cloud Model Studio), kimi-k2.6 (deployed on Alibaba Cloud Model Studio), kimi/kimi-k3 (deployed on Moonshot AI), kimi/kimi-k2.7-code-highspeed (deployed on Moonshot AI), kimi/kimi-k2.7-code (deployed on Moonshot AI), and kimi/kimi-k2.6 (deployed on Moonshot AI).
Enabling this parameter when history messages lack reasoning_content does not cause an error.
When enabled, reasoning_content from conversation history counts toward input tokens and billing.
  • OpenAI compatible
  • DashScope
preserve_thinking is not a standard OpenAI parameter. When you use the Python SDK, pass this parameter in extra_body.
  • Python
  • Node.js
  • HTTP

Sample code

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # Configurations vary by region. Modify this based on your actual region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

# First turn of the conversation
messages = [
    {"role": "user", "content": "I need to choose a message queue for an E-commerce system that handles tens of millions of messages per day. Please provide a recommendation."}
]

first_reasoning = ""
first_content = ""
is_answering = False

completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=messages,
    extra_body={"enable_thinking": True},
    stream=True,
    stream_options={"include_usage": True},
)

print("=" * 20 + "First-turn thought process" + "=" * 20)

for chunk in completion:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        first_reasoning += delta.reasoning_content
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "First-turn response" + "=" * 20)
            is_answering = True
        print(delta.content, end="", flush=True)
        first_content += delta.content

# Second turn: Pass the thought process and ask why the model excluded Kafka
messages = [
    {"role": "user", "content": "I need to choose a message queue for an E-commerce system that handles tens of millions of messages per day. Please provide a recommendation."},
    {
        "role": "assistant",
        "content": first_content,
        "reasoning_content": first_reasoning,
    },
    {"role": "user", "content": "Why did you exclude Kafka in your comparison?"},
]

reasoning_content = ""
answer_content = ""
is_answering = False

# Pass preserve_thinking through extra_body
completion = client.chat.completions.create(
    model="qwen3.8-max",
    messages=messages,
    extra_body={
        "enable_thinking": True,
        "preserve_thinking": True,
    },
    stream=True,
    stream_options={"include_usage": True},
)

print("\n" + "=" * 20 + "Second-turn thought process" + "=" * 20)

for chunk in completion:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
        if not is_answering:
            print(delta.reasoning_content, end="", flush=True)
        reasoning_content += delta.reasoning_content
    if hasattr(delta, "content") and delta.content:
        if not is_answering:
            print("\n" + "=" * 20 + "Second-turn response" + "=" * 20)
            is_answering = True
        print(delta.content, end="", flush=True)
        answer_content += delta.content

Response

====================First-turn thought process====================
The user needs a message queue for an E-commerce system with tens of millions of daily messages. I will compare mainstream solutions based on dimensions such as throughput, reliability, delayed messages, and transaction support...

RocketMQ: Validated in Alibaba's E-commerce scenarios, natively supports transactional and delayed messages, and provides strict partition-level ordering...
Kafka: Extremely high throughput, but lacks native support for transactional and delayed messages, requiring custom compensation mechanisms...
RabbitMQ: Low latency, but limited cluster scalability, with a peak TPS in the tens of thousands...
====================First-turn response====================
Considering the core needs of an E-commerce scenario (transactional messages, delayed messages, ordering, and peak handling), I recommend Apache RocketMQ. If your team already has a Kafka ecosystem or requires strong real-time analytics, Kafka is also a viable option.
====================Second-turn thought process====================
The user is asking why Kafka was excluded. Reviewing my historical thought process, I did not exclude Kafka; I gave it a 4-star rating. I will refer to my previous detailed comparison to explain...

In the last turn, I compared the differences between RocketMQ and Kafka regarding transactional messages, delayed messages, and ordering. Kafka's main disadvantage is that it requires additional architectural design to compensate for E-commerce-specific semantics...
====================Second-turn response====================
I did not exclude Kafka. Kafka excels in throughput and ecosystem. The reason RocketMQ received a slightly higher rating is its better out-of-the-box match for core E-commerce workflows. RocketMQ natively supports transactional and delayed messages, while Kafka requires self-implementation through architectural patterns like the Outbox Pattern. If your team already has a Kafka ecosystem, it is fully capable of handling a scenario with tens of millions of messages.

Other features

Billing

  • Thinking content is billed per output token.
  • Some hybrid thinking models price thinking and non-thinking modes differently.
    If a thinking-mode model produces no reasoning output, non-thinking pricing applies.

FAQ

This depends on the model type:
  • For hybrid thinking models, such as qwen-plus and deepseek-v3.2-exp, set enable_thinking to false.
  • For thinking-only models, such as qwen3-235b-a22b-thinking-2507 and deepseek-r1, the thinking mode cannot be disabled.
qwen3.7-plus is a hybrid thinking model with thinking mode enabled by default. The thinking process generates a large number of reasoning tokens — more than 60% of the total output tokens in measurements — so the total latency of a single call is much higher than in non-thinking mode. The token generation speed itself is normal, at about 52 to 54 tokens/s. The longer total latency comes from the number of tokens that the thinking process produces, not from a slower model or a network fault.To troubleshoot the latency, follow these steps:
  1. Check whether thinking mode is enabled. It is enabled by default for qwen3.7-plus. If the response returns the reasoning_content field, thinking mode is active.
  2. Review completion_tokens and reasoning_tokens in the usage statistics of the response. A high proportion of reasoning_tokens means that the long total latency is expected behavior of thinking mode.
  3. If you do not need the reasoning process, set enable_thinking to false in the request to disable thinking mode. This greatly reduces output tokens and lowers total latency by 60% to 75% in measurements.
  4. If you want to keep the reasoning capability, use streaming output. You receive the first token sooner and can watch the reasoning in real time instead of waiting for the full response.
Usage statistics show the total latency of a single call, including the time spent generating reasoning tokens. This is not the generation latency of an individual token.
The examples in this topic use streaming by default (recommended, so you can watch the reasoning in real time and avoid a long wait). Commercial thinking models (such as qwen-plus, qwen3-max, and qwen-flash) also support non-streaming (synchronous) output, returning the full reasoning and answer in a single response.
When you switch an example from streaming to non-streaming, also update the response-parsing code: a non-streaming call returns a complete response object (completion). Do not iterate it with for chunk in completion as in the streaming example (this raises 'tuple' object has no attribute 'choices'). Instead, read completion.choices[0].message.reasoning_content (reasoning) and completion.choices[0].message.content (answer) directly. Also, when stream=False, do not set the stream_options parameter.
The following example calls qwen3.8-max in thinking mode without streaming, using the OpenAI-compatible interface:
from openai import OpenAI
import os

client = OpenAI(
    # API keys differ by region. Get an API key: https://www.alibabacloud.com/help/en/model-studio/get-api-key
    # If the environment variable is not configured, replace the line below with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following is the configuration for the Singapore region. Replace WorkspaceId with your real workspace ID; configurations differ by region.
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen3.8-max",  # Replace with a thinking model that supports non-streaming output
    messages=[{"role": "user", "content": "Who are you?"}],
    extra_body={"enable_thinking": True},
    stream=False,  # Non-streaming (synchronous) output; do not set stream_options when stream=False
)

# A non-streaming call returns a complete response object. Read message directly; do not iterate.
message = completion.choices[0].message
print("=" * 20 + "Reasoning" + "=" * 20 + "\n")
print(getattr(message, "reasoning_content", "") or "")
print("\n" + "=" * 20 + "Answer" + "=" * 20 + "\n")
print(message.content)
Some models (such as the open-source qwen3-235b-a22b and qwen3-32b) support streaming only. A non-streaming call returns the error parameter.enable_thinking only support stream call; use streaming for these models.
Top up your account in the Expenses and Costs center. Your account must have no overdue payments to call models.
After the free quota runs out, calls are billed per minute. View spending in Bill Details.
These models accept text only. Qwen3-VL and QVQ support deep thinking on images .
One hour after you call a model, go to the Monitoring (Singapore or Beijing) page. Set the query conditions, such as the time range and workspace. Then, in the Models area, find the target model and click Monitor in the Actions column to view the model's call statistics. For more information, see the Monitoring document.
Data is updated hourly. During peak periods, there may be an hour-level latency.
image
If a call with a long prompt fails or times out, thinking mode (enable_thinking=true) is usually enabled. Thinking mode increases processing time, which can truncate the response or cause the request to time out when the prompt is long.Solutions:
  • Disable thinking mode: set enable_thinking to false. Processing time can drop from about 50 seconds to about 30 seconds.
  • Enable streaming output: set stream to true to avoid the timeout limit of non-streaming mode.
  • Increase the timeout: to keep thinking mode enabled, set the client timeout to 180 seconds or longer.

API reference

For input and response parameters, see Text Generation.

Error codes

If a call fails, see Error codes.
Token Plan
Model Playground
Statistics and Monitoring
Support