Skip to main content
query() supports two input modes:
  • Single-message query mode: submit one user message; the SDK closes the session after the current reply completes. See the Quickstart.
  • Multi-message session mode: keep the session open and carry on a multi-turn conversation with the model.

Multi-message session

Define a sequence that yields user messages in order:
The model replies once for each user message it receives. Once the message sequence finishes, the session closes automatically. For message field definitions see SDKUserMessage.

Interrupting the current turn

When the user wants to stop the current response but continue the conversation later, call await q.interrupt(). After the call completes, the current generation or tool execution has stopped.
interrupt() only stops the active turn; it does not close the session. A multi-message session can continue with another turn. See Managing the session lifecycle for ways to end the entire session.

Managing the session lifecycle

The SDK closes the session automatically after a string input or finite message sequence ends. To end the session earlier, use an AbortController to define when it should end, or call q.close() directly.

Define when the session ends

To end the session when a custom business condition is met, create an AbortController and pass it through options.abortController when calling query(). Call abort() on the same controller when that condition is met. The condition can come from a user action, upstream request, task timeout, application shutdown, or other application logic:
Calling abort() closes the entire session and ends message iteration. It cannot accept more messages afterward.

Close a session explicitly

When the current code no longer needs the session, call await q.close() directly. This works for normal completion, early exit, and error cleanup; placing it in finally ensures related resources finish closing: