Qwen-Coder is a language model designed for code tasks. You can use the API to generate code, complete code, and call tools to interact with external systems.
It is recommended to use the latest general-purpose model instead of the Qwen-Coder model. See Text Generation to select a model that fits your scenario.
Quick Start
API prerequisites: You have obtained and configured an API Key and configured the API Key as an environment variable. If calling via SDK, you need to install the OpenAI or DashScope SDK.
The following examples will demonstrate how to callqwen-coder-plus-latestthe Model to write a Python function that finds prime numbers.
- OpenAI-compatible Chat Completions API
- DashScope
- Python
- Node.js
- curl
Request ExampleResponse
Core Capabilities
Built-in Tools
To enable the model to interact with the external environment (for example, reading and writing files, calling APIs, and operating databases), you can provide it with a series of tools. The model will decide whether and how to call these tools based on your instructions. For details, seeFunction Calling。
The complete tool calling process includes:
- Define tools and send a request:Define the tool list in the request, and present to the model the task that needs to be completed using tools.
- Execute tool:Parse the model's returned
tool_calls, and call the corresponding tool function you have implemented locally to execute the task. - Return execution result:Wrap the tool's execution result into a specific format and send it back to the model, letting it complete the final task based on the result.
write_file tool to save it to a local file.
- OpenAI-compatible Chat Completions API
- DashScope
- Python
- Node.js
- curl
Code generation
Qwen-Coder supports two Code Completion methods. Please choose based on your needs:
- Prefix Continuation (Partial Mode): Applicable to all Qwen-Coder Models and regions, supports prefix completion, simple to implement, recommended for use.
- Completions interface: Only supports North China 2 (Beijing)
qwen-coder-turbo-latestModel. Supports prefix completion and prefix-suffix completion.
Prefix Continuation (Partial Mode)
This feature is used to let the Model automatically complete the remaining part based on the code (prefix) you have written halfway.
By adding a message with role set to assistant in the messages list and setting partial: true, you can implement this. The assistant message's content is the code prefix you provide. For details, see Prefix Continuation.
- OpenAI-compatible
- DashScope
- Python
- Node.js
- curl
Request ExamplesResponse
Completions Interface
Supported Models:
qwen-coder-turbo-latest
Completions APIBy prompt using special fim (Fill-in-the-Middle) tags to guide the model to complete.
- Prefix-based completion
- Prefix and suffix-based completion
Prompt Template:
<|fim_prefix|>and<|fim_suffix|>are special Tokens, used to guide the model in text completion, no modification needed.{prefix_content}Need to be replaced with the passed-in prefix information, such as function name, Input Parameters, usage instructions, etc.
Production Usage
To optimize the efficiency and reduce the cost of using the Qwen-Coder model, refer to the following recommendations:
- Enable Stream: Setting
stream=Truecan return intermediate results in real time, reducing timeout risks and improving user experience. - Lower the temperature parameter: Code generation tasks usually require determinacy and accuracy in results. It is recommended to lower the
temperatureparameter to reduce the randomness of generated results. - Use a model that supports context caching: In scenarios containing a large number of repeated prefixes (such as code completion and code review), it is recommended to use a model that supportsContext Cache to effectively reduce costs.
- Control Tool Count: To ensure the efficiency and cost-effectiveness of model calls, it is recommended that the number of tools passed in a single call
toolsdoes not exceed 20. Passing a large number of tool descriptions will consume too many input Tokens, which not only increases costs and reduces response speed, but also makes it harder for the model to select the correct tool. For details, see Function Calling.
Billing and Rate Limiting
-
Basic Billing: Based on each request's input
Tokencount and outputTokencount for billing. Unit prices differ across models; for specific prices, refer to Models。 -
Special Billing Items:
- Tiered Pricing: The
qwen3-coderseries models adopt tiered pricing. When SingleAfter the number of input Tokens in a request reaches a specific tier, all input and output Tokens of that request are billed at the unit price of this tier. - Context Cache: For models that support context caching, when multiple requests contain large amounts of repeated input (such as code review), the caching mechanism can significantly reduce costs. HitImplicit Cache's input text is billed at 20% of the unit price, hitExplicit Cache's input text is billed at 10% of the unit price. For details, please refer to Context Cache.
- Built-in Tools (Function Calling): When using the built-in tools feature, you are in
toolsThe tool descriptions defined in the parameters are counted as input contentTokentotal volume and incur costs.
- Tiered Pricing: The
- Rate Limiting: API calls are subject to dual limits of requests per minute (RPM) and tokens per minute (TPM). For details, see Rate Limiting.
- Free Quota (Singapore only): The Validity is calculated from the date of activating Bailian or model application Pass, within the 90-day Validity, Qwen-Coder models each provide 1 million Token new user Free Quota.
API Reference
For the Input Parameters and output parameters of the Qwen Code model, see Text Generation.
FAQ
When using development tools like Qwen Code, Claude Code, etc., why are a large number of Tokens consumed?
When calling the Qwen-Coder Model through external development tools to handle problems, the tool may call the API multiple times, thereby consuming a large number of Tokens. For specific methods of monitoring and reducing Token consumption, please refer to Qwen Code and Claude CodeDocs. You can enable the Free Quota Only feature to avoid extra costs after the Free Quota is exhausted.
You can also purchase an AI coding plan, adopting a fixed monthly fee and providing monthly request quotas supported for Use in AI tools. For details, please see Coding Plan Overview.
How to view Model Usage?
After Model Calling occursper-minutecan be viewed in Monitoring charts. Log in to the Monitoring page (SingaporeOrBeijing), select in the left navigation barO&M Management > MonitoringEnter the monitoring overview page, set query conditions (for example, select time range, business space, etc.), then in the Models area find the target model and click View Details to view the calling statistics of that model. For details, see Monitoring Docs.
Monitoring data is updated per minute and is for Reference only, not as a billing basis.
How to make the model only output Code without any explanatory text?
You can Refer to the following methods:
- Prompt Constraints: Explicitly instruct in the prompt, for example: "Only return code, do not include any explanations, comments, or markdown tags."
- Settings
stopSequence: Usestop=["\n# Explanation:", "Note:", "Explanation:", "Note:"]and similar phrases to terminate early when the Model begins generating explanatory text. For details, see Qwen API Reference.