Multi-turn sessions and streaming input¶
This topic describes the main ways to send prompts to the Cortex Code Agent SDK: single-prompt queries
when you want query() to manage the session lifecycle for you, streamed input for incremental prompt
delivery, and multi-turn sessions for interactive conversations that maintain context across exchanges.
Input patterns¶
The SDK provides three input patterns:
Mode |
When to use |
API |
|---|---|---|
Single prompt input |
Send one prompt with |
|
Streamed input |
Send user messages incrementally from an async iterable instead of a single prompt string |
|
Multi-turn session |
Interactive conversations: send multiple prompts, maintain context, control the session lifecycle |
|
Single-prompt queries¶
The query() function is the simplest way to use the SDK. It can send either a single prompt string or an async
iterable of SDK user messages, and it streams back events until the agent produces a ResultMessage. In this mode,
“single prompt” refers to the input pattern: output still streams normally.
The query() function manages the full session lifecycle for you: it creates a session, sends the prompt,
yields events, and closes the session when the result arrives or the iterator is exhausted.
This is different from maxTurns / max_turns. A single-prompt query still allows the agent to take as many
internal turns as needed, subject to your configured turn limit. Setting maxTurns: 1 or max_turns=1 is a
separate constraint that limits the agent to one internal turn and can produce an error_max_turns result.
Multi-turn sessions¶
For conversations that require multiple exchanges, use a session. The agent retains context between turns, so later prompts can reference files read, analysis performed, and topics discussed in earlier turns.
Session lifecycle¶
Call
createCortexCodeSession(options)to start a session. This spawns the CLI process.Call
session.send(prompt)to send a user message.Iterate
session.stream()to receive responses. Stop iterating when you see aresultevent.Repeat steps 2–3 for additional turns.
Call
session.close()to end the session and clean up the process.
Create a
CortexCodeSDKClientand callconnect()(or useasync with).Call
client.query(prompt)to send a user message.Iterate
client.receive_response()to receive messages up to and including aResultMessage.Repeat steps 2–3 for additional turns.
Call
client.disconnect()(or let theasync withblock exit).
Continue a previous session¶
You can resume a conversation from a previous session. The agent loads the prior conversation history and continues where it left off.
You can also resume a specific session by ID:
Fork a session¶
Forking creates a new session that starts with the full conversation history of an existing session. The original session is not modified. This is useful for exploring alternative approaches without losing the original conversation.
Interrupt a turn¶
Your application can request an interrupt while the agent is processing a turn. This has the same effect as pressing Esc in the CLI. The session stays alive for further prompts.
Direct interrupt call¶
Call the interrupt() method to send an interrupt request directly:
Abort controller / abort event¶
You can also pass an abort signal at session creation time. When the signal fires, the SDK automatically sends the same interrupt request.
Note
In TypeScript, pass an AbortController whose abort() method triggers the interrupt request. In Python, pass
an asyncio.Event whose set() method triggers the interrupt request. In both cases, the session stays alive
after interruption.
Legal notices¶
Where your configuration of Cortex Code uses a model provided on the Model and Service Pass-Through Terms, your use of that model is further subject to the terms for that model on that page.
The data classification of inputs and outputs are as set forth in the following table.
Input data classification |
Output data classification |
Designation |
|---|---|---|
Usage Data |
Customer Data |
Covered AI Features [1] |
For additional information, refer to Snowflake AI and ML.