Cortex Code Agent SDK reference – TypeScript¶
This topic provides the complete API reference for the Cortex Code Agent SDK for TypeScript, including all functions, types, and interfaces.
Installation¶
Requires Node.js 18 or later. The package is ESM-only.
The SDK expects the Cortex Code CLI to be installed separately. If it is not on
your PATH, set CORTEX_CODE_CLI_PATH=/path/to/cortex or pass cliPath
in the session options.
Functions¶
query()¶
The primary function for interacting with Cortex Code. Creates an async generator that streams messages as they arrive.
Parameters
Parameter |
Type |
Description |
|---|---|---|
|
|
A single user prompt, or an async iterable of SDK user messages for streaming input |
|
|
Optional configuration object (see Options) |
Returns
A Query object that extends AsyncGenerator<CortexCodeEvent> with additional control methods.
Example
createCortexCodeSession()¶
Creates a persistent session for multi-turn conversations.
Example
Query object¶
Returned by query(). Extends AsyncGenerator<CortexCodeEvent>.
Method |
Description |
|---|---|
|
Send SIGINT to the underlying process |
|
Change the permission mode for later turns in the active query |
|
Change the model for subsequent turns |
|
Return initialize-handshake metadata from the CLI |
|
Return slash command metadata from the initialize response, when advertised by the CLI |
|
Return model metadata from the initialize response, when advertised by the CLI |
|
Return custom agent metadata from the initialize response, when advertised by the CLI |
|
Return account metadata from the initialize response, when advertised by the CLI |
|
Stream additional SDK user messages into the active query |
|
Cancel a running background task by ID |
|
Close the session and terminate the process |
CortexCodeSession interface¶
Returned by createCortexCodeSession(). Supports multi-turn conversations and implements AsyncDisposable
for use with await using (Node.js 24+).
Property / Method |
Description |
|---|---|
|
PID of the underlying CLI process |
|
Send a plain-text or structured SDK user message |
|
Stream events from the agent |
|
Return initialize-handshake metadata from the CLI |
|
Send interrupt signal |
|
Change the permission mode for later turns in the session |
|
Change model mid-session |
|
Cancel a running background task by ID |
|
End session and terminate process |
|
Calls |
Options¶
Configuration passed to query() or createCortexCodeSession().
Option |
Type |
Default |
Description |
|---|---|---|---|
|
|
Current process working directory |
Working directory for the session. If omitted, the SDK inherits the current process working directory. |
|
|
CLI default |
Model to use. Use |
|
|
CLI default |
Snowflake connection name from Snowflake CLI connection settings, typically
|
|
|
|
Profile name (loads from |
|
|
|
Session ID to resume a previous conversation |
|
|
|
Continue the most recent conversation. Resumes the last session without needing a session ID. |
|
|
|
Fork a resumed session into a new session ID instead of continuing in-place. Use with |
|
|
|
Explicit session ID for the conversation. If omitted, the CLI generates one automatically. |
|
|
|
|
|
|
|
Safety flag required when using |
|
|
|
Tools to auto-approve without prompting |
|
|
|
Tools to always deny |
|
|
Custom permission handler called before each tool execution. See canUseTool callback. |
|
|
|
|
MCP tool name used for permission prompts. When |
|
|
|
Maximum number of agentic turns before stopping. When reached, the session emits an
|
|
|
|
Thinking effort level for the model. One of |
|
|
|
Additional working directories to make available to the session |
|
|
|
Plugin directories to load. Accepts paths as strings or |
|
|
|
Environment variables merged into the spawned process environment. Set a key to |
|
|
|
Send an interrupt signal by calling |
|
|
|
Custom system prompt. Pass a string to replace the default entirely, or a SystemPromptPreset to extend it. |
|
|
|
Text appended to the default system prompt. Use this to add instructions without replacing the built-in prompt. |
|
|
|
Hook callbacks for intercepting tool execution and other agent events. See Hooks. |
|
|
|
Setting sources to load. Array of |
|
|
|
Include token-level streaming events |
|
|
|
External MCP server configurations. Keys are server names, values are server configs
(for example, |
|
|
|
Disable MCP servers |
|
|
|
Structured output – validates final response against JSON Schema |
|
|
|
Path to custom CLI binary. If omitted, the SDK first checks
|
|
|
|
Additional CLI arguments as key-value pairs |
canUseTool callback¶
A custom permission handler called before each tool execution. Return an allow or deny result to control whether the tool call proceeds.
For many ordinary tool permission checks, the callback input contains fields such as
{ action, resource }. The allow/deny result and optional deny message are used for these checks.
updatedInput is used for SDK-routed pseudo-tools such as AskUserQuestion and ExitPlanMode, which contain
tool-specific fields.
Example
SystemPromptPreset¶
Instead of replacing the system prompt entirely, use a preset object to extend the built-in prompt.
Example
Hooks¶
Hooks let you intercept agent events such as tool execution. Each hook event maps to an array of matchers, and each matcher contains an optional pattern and one or more callbacks.
Example
Message types¶
Events yielded by query() and session.stream():
SDKAssistantMessage¶
Emitted when the agent produces a response. Contains one or more content blocks.
SDKResultMessage¶
Emitted when the agent finishes a turn. Check subtype for success or error.
SDKUserMessage¶
Echoed back when a user message is processed.
SDKSystemMessage¶
System events such as session initialization.
Content blocks¶
Type |
Fields |
|---|---|
|
|
|
|
|
|
|
|
Streaming events¶
Type |
Description |
|---|---|
|
Partial text/thinking streaming (requires |
|
|
|
|
SDKPartialAssistantMessage is emitted for partial text and thinking blocks. Complete tool calls still arrive as
AssistantMessage blocks, and tool results still arrive as UserMessage blocks.
Structured output¶
Force the agent to return a response matching a JSON Schema:
For more information, see Structured output.
Error handling¶
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.