Skip to main content
The 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

prompt
string | AsyncIterable<SDKUserMessage>
required
The prompt to send to Qwen Code.
  • String: For single-turn queries (most common)
  • AsyncIterable: For multi-turn conversations
The transport remains open until the prompt is consumed.
Example (Single-turn):
Example (Multi-turn):

options

options
QueryOptions
Configuration options for the query session. See QueryOptions below.

QueryOptions

Basic Options

cwd
string
default:"process.cwd()"
The working directory for the query session. Determines the context in which file operations and commands are executed.
model
string
The AI model to use for the query session. Takes precedence over OPENAI_MODEL and QWEN_MODEL environment variables.
Common models:
  • gpt-4
  • gpt-3.5-turbo
  • qwen-max
  • qwen-plus
  • qwen-turbo
pathToQwenExecutable
string
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

permissionMode
'default' | 'plan' | 'auto-edit' | 'yolo'
default:"'default'"
Permission mode controlling tool execution approval.
  • default: Write tools denied unless approved via canUseTool or in allowedTools. 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.
See Permission Modes for details.
canUseTool
CanUseTool
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.
allowedTools
string[]
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)'
excludeTools
string[]
List of tools to exclude from the session. Excluded tools return a permission error immediately. Takes highest priority.
coreTools
string[]
List of core tools to enable. If specified, only these tools will be available to the AI.

MCP Integration

mcpServers
Record<string, McpServerConfig>
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

abortController
AbortController
Controller to cancel the query session. Call abortController.abort() to terminate.
See Aborting Queries for examples.
maxSessionTurns
number
default:"-1 (unlimited)"
Maximum number of conversation turns before the session automatically terminates. A turn consists of a user message and an assistant response.
sessionId
string
Specify a session ID for the new session. Ensures SDK and CLI use the same ID without resuming history.
resume
string
Resume a previous session by providing its session ID.

Advanced Options

env
Record<string, string>
Environment variables to pass to the Qwen CLI process. Merged with the current process environment.
debug
boolean
default:"false"
Enable debug mode for verbose logging from the CLI process.
logLevel
'debug' | 'info' | 'warn' | 'error'
default:"'error'"
Logging level for the SDK. Controls the verbosity of log messages.
stderr
(message: string) => void
Custom handler for stderr output from the Qwen CLI process.
authType
'openai' | 'qwen-oauth'
default:"'openai'"
Authentication type for the AI service.
Using 'qwen-oauth' in SDK is not recommended as credentials are stored in ~/.qwen and may need periodic refresh.
includePartialMessages
boolean
default:"false"
When true, the SDK emits incomplete messages as they are being generated, allowing real-time streaming.
See Message Types for handling partial messages.
agents
SubagentConfig[]
Configuration for subagents that can be invoked during the session.
timeout
object
Timeout configuration for various SDK operations. All values are in milliseconds.

Return Value

Returns a Query instance that implements AsyncIterable<SDKMessage>.
Query
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