Query class returned by query() provides methods for controlling the query session, inspecting state, and modifying behavior mid-session.
Overview
AQuery instance implements AsyncIterable<SDKMessage>, allowing you to iterate over messages:
Methods
getSessionId()
Get the unique session identifier.Returns
The session ID (UUID format).
Example
Use Cases
- Resume sessions later with the
resumeoption - Track sessions across multiple queries
- Link sessions with external systems
- Debug and logging
isClosed()
Check if the query session has been closed.Returns
true if the session is closed, false otherwise.Example
Use Cases
- Check session state before calling methods
- Conditional logic based on session status
- Resource cleanup verification
interrupt()
Interrupt the current operation. The AI will stop what it’s doing and await further instructions.Returns
Promise that resolves when the interrupt is acknowledged.
Example
Notes
- Throws an error if the query is already closed
- The interrupt is sent as a control request to the CLI
- The AI will finish the current tool execution before stopping
setPermissionMode()
Change the permission mode during the session.Parameters
The new permission mode:
'default', 'plan', 'auto-edit', or 'yolo'.Returns
Promise that resolves when the mode change is acknowledged.
Example
Notes
- Changes take effect immediately for subsequent tool requests
- Does not affect ongoing tool executions
- Throws an error if the query is closed
setModel()
Change the AI model during the session.Parameters
The new model identifier (e.g.,
'gpt-4', 'qwen-max').Returns
Promise that resolves when the model change is acknowledged.
Example
Notes
- The new model is used for subsequent AI requests
- Does not affect the current ongoing request
- Throws an error if the query is closed
close()
Manually close the query session and cleanup resources.Returns
Promise that resolves when the session is fully closed.
Example
What Gets Cleaned Up
- CLI process terminated
- Pending control requests rejected
- MCP transports closed
- Input stream completed
- Event listeners removed
Notes
- Automatically called when iteration completes normally
- Safe to call multiple times (subsequent calls are no-ops)
- Pending operations will be rejected with “Query is closed” error
streamInput()
Stream user messages for multi-turn conversations.Parameters
Async iterable of user messages to send.
Returns
Promise that resolves when all messages have been sent.
Example
endInput()
Signal that no more user messages will be sent (for multi-turn mode).Example
supportedCommands()
Get the list of control commands supported by the CLI.Returns
Object containing supported command names and their metadata.
Example
mcpServerStatus()
Get the status of MCP servers connected to the session.Returns
Object containing MCP server status information.
Example
Complete Example
Here’s a comprehensive example using multiple methods:Error Handling
All methods throw errors if called on a closed query:See Also
- query() Function - Create query sessions
- Permission Modes - Understanding permission modes
- Multi-Turn Example - Multi-turn conversation patterns
