query() function is the primary entry point for interacting with Qwen Code. It creates a new query session and returns a Query instance that implements AsyncIterable<SDKMessage>.
Import
Signature
Parameters
prompt
The prompt to send to Qwen Code.
- String: For single-turn queries (most common)
- AsyncIterable: For multi-turn conversations
options
Configuration options for the query session. See QueryOptions below.
QueryOptions
Basic Options
The working directory for the query session. Determines the context in which file operations and commands are executed.
The AI model to use for the query session. Takes precedence over Common models:
OPENAI_MODEL and QWEN_MODEL environment variables.gpt-4gpt-3.5-turboqwen-maxqwen-plusqwen-turbo
Path to the Qwen Code executable. If not provided, the SDK uses the bundled CLI (v0.1.1+) or auto-detects from common locations.Supported formats:
- Command name:
'qwen'(executes from PATH) - JavaScript file:
'/path/to/cli.js'(uses Node.js or Bun) - TypeScript file:
'/path/to/index.ts'(uses tsx if available) - Native binary:
'/path/to/qwen'(executes directly)
Permission Options
Permission mode controlling tool execution approval.
default: Write tools denied unless approved viacanUseToolor inallowedTools. Read-only tools execute without confirmation.plan: Blocks all write tools, instructing AI to present a plan first.auto-edit: Auto-approve edit tools (edit, write_file) while other tools require confirmation.yolo: All tools execute automatically without confirmation.
Custom permission handler for tool execution approval. Invoked when a tool requires confirmation.Must respond within 60 seconds (configurable via
timeout.canUseTool) or the request will be auto-denied.See Custom Permission Handler for examples.List of tools allowed to run without confirmation. Matching tools bypass
canUseTool callback.Pattern matching:- Tool name:
'write_file','run_shell_command' - Tool class:
'WriteTool','ShellTool' - Shell command prefix:
'ShellTool(git status)'
List of tools to exclude from the session. Excluded tools return a permission error immediately. Takes highest priority.
List of core tools to enable. If specified, only these tools will be available to the AI.
MCP Integration
MCP (Model Context Protocol) servers to connect. Supports both external servers and SDK-embedded servers.External MCP server (stdio):SDK MCP server (in-process):See MCP Integration for details.
Session Control
Controller to cancel the query session. Call See Aborting Queries for examples.
abortController.abort() to terminate.Maximum number of conversation turns before the session automatically terminates. A turn consists of a user message and an assistant response.
Specify a session ID for the new session. Ensures SDK and CLI use the same ID without resuming history.
Resume a previous session by providing its session ID.
Advanced Options
Environment variables to pass to the Qwen CLI process. Merged with the current process environment.
Enable debug mode for verbose logging from the CLI process.
Logging level for the SDK. Controls the verbosity of log messages.
Custom handler for stderr output from the Qwen CLI process.
Authentication type for the AI service.
When See Message Types for handling partial messages.
true, the SDK emits incomplete messages as they are being generated, allowing real-time streaming.Configuration for subagents that can be invoked during the session.
Timeout configuration for various SDK operations. All values are in milliseconds.
Return Value
Returns aQuery instance that implements AsyncIterable<SDKMessage>.
An async iterable that yields messages from the query session.See Query Instance Methods for available methods.
Examples
Basic Query
With All Options
See Also
- Message Types - Understand different message types
- Query Instance Methods - Methods available on Query instances
- Permission Modes - Permission mode details
- Examples - Practical usage examples
