Skip to main content
Tool calling

Code interpreter

Run Python in a sandbox

The code interpreter lets the model write and execute Python code in a secure sandbox during a conversation. When the model encounters a task that benefits from computation — such as math, data analysis, or chart generation — it automatically generates code, runs it, and incorporates the result into its response.

Getting started

The following examples show a complete code interpreter request with output.
  • Responses API
  • Chat Completions
  • DashScope
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.getenv("YOUR_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)

response = client.responses.create(
  model="qwen3-max-2026-01-23",
  input="What is 12 to the power of 3?",
  tools=[{"type": "code_interpreter"}],
  extra_body={"enable_thinking": True}
)

print(response.output_text)
print(response.usage)
Response example
12 to the power of 3 equals **1728**.

Calculation:
12^3 = 12 x 12 x 12 = 144 x 12 = 1728

How it works

Once enabled, the model processes each request in stages:
1

Thinking

The model analyzes the request and determines how to solve the problem.
2

Code execution

The model generates and executes Python code in a sandbox.
3

Result integration

The model processes the execution results and determines next steps.
4

Response

The model generates a natural-language response.
Steps 2 and 3 may repeat multiple times within a single request.

Supported models

FamilyModelsNotes
Qwen-Maxqwen3-max, qwen3-max-2026-01-23Requires thinking mode
Qwen-Plusqwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-02-15
Qwen-Flashqwen3.5-flash, qwen3.5-flash-2026-02-23
Open sourceqwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b

Retrieve executed code

The DashScope API returns the code that the interpreter executed in the tool_info field of each streaming chunk. Each entry contains the code string and a type of code_interpreter:
"tool_info": [
  {
    "code_interpreter": {"code": "123**21"},
    "type": "code_interpreter"
  }
]
When the model calls the interpreter multiple times in a single request, tool_info accumulates all calls. The total call count is available in usage.plugins:
"plugins": {"code_interpreter": {"count": 2}}
The Chat Completions API does not return the executed code. Use the Responses API or DashScope API if you need to inspect the generated code.

Limitations

  • The code interpreter and function calling are mutually exclusive and cannot be enabled at the same time.
    Enabling both results in an error.
  • When the code interpreter is enabled, a single request triggers multiple model inferences. The usage field aggregates token consumption across all inferences.

Billing

The code interpreter is temporarily free, but it increases token consumption.
Code interpreter | Qwen Cloud