Cortex Code Agent SDK

The Cortex Code Agent SDK lets you build agentic AI applications using Python and TypeScript. Your agents can read files, run commands, search codebases, execute SQL, and edit code, using the same tools and agent loop that power Cortex Code.

The SDK includes built-in tools for file operations, shell commands, and code editing, so your agent can start working immediately without you implementing tool execution.

import { query } from "cortex-code-agent-sdk";

for await (const message of query({
  prompt: "What does this codebase do? Give me a one-paragraph summary.",
  options: { cwd: process.cwd() },
})) {
  if (message.type === "assistant") {
    for (const block of message.content) {
      if (block.type === "text") process.stdout.write(block.text);
    }
  }
}

Get started

Prerequisites

Requirement

Details

Cortex Code CLI

Install with curl -LsS https://ai.snowflake.com/static/cc-scripts/install.sh | sh

Snowflake connection

Configured through Snowflake CLI connection settings, typically in ~/.snowflake/connections.toml. Existing setups in ~/.snowflake/config.toml are also supported. Pass the connection option or set default_connection_name in the TOML file. See Configuring connections.

Node.js (TypeScript)

Version 18.0.0 or later

Python (Python SDK)

Version 3.10 or later

1. Install the Cortex Code CLI

Install the CLI:

curl -LsS https://ai.snowflake.com/static/cc-scripts/install.sh | sh

2. Install the SDK

Install the SDK from npm or PyPI:

npm install cortex-code-agent-sdk

3. Configure your Snowflake connection

The SDK authenticates through your Snowflake CLI connection settings. Add a connection to ~/.snowflake/connections.toml or use an existing setup in ~/.snowflake/config.toml (see Configuring connections):

[my-connection]
account = "myorg-myaccount"
user = "myuser"
authenticator = "externalbrowser"

The SDK uses the CLI’s default connection unless you specify one explicitly through the connection option.

If the Cortex Code CLI is not on your PATH, point the SDK at it by setting CORTEX_CODE_CLI_PATH=/path/to/cortex or by passing cliPath (TypeScript) or cli_path (Python) in the SDK options.

4. Run your first agent

The following example creates an agent that explores your project and summarizes what it does:

import { query } from "cortex-code-agent-sdk";

for await (const message of query({
  prompt: "What does this codebase do? Give me a one-paragraph summary.",
  options: { cwd: process.cwd() },
})) {
  if (message.type === "assistant") {
    for (const block of message.content) {
      if (block.type === "text") process.stdout.write(block.text);
    }
  }
  if (message.type === "result") {
    console.log("\nDone:", message.subtype);
  }
}

For a more complete tutorial, see Quickstart.

Capabilities

Built-in tools

Your agent can read files, run commands, execute SQL, and search codebases without additional configuration. Available tools can vary by environment and runtime capabilities:

Tool

Description

Read

Read any file in the working directory

Write

Create new files

Edit

Make precise edits to existing files

Bash

Run terminal commands, scripts, and git operations

Glob

Find files by pattern (**/*.ts, src/**/*.py)

Grep

Search file contents with regex

SQL

Execute SQL queries against Snowflake

Multi-turn sessions

You can maintain context across multiple exchanges. The agent retains knowledge of files read, analysis performed, and conversation history:

import { createCortexCodeSession } from "cortex-code-agent-sdk";

const session = await createCortexCodeSession({ cwd: process.cwd() });

await session.send("Read the authentication module");
for await (const event of session.stream()) {
  if (event.type === "result") break;
}

// Second turn - "it" refers to the auth module from context
await session.send("Now find all places that call it");
for await (const event of session.stream()) {
  if (event.type === "result") break;
}

await session.close();

You can also continue a previous session or fork it into a new one:

// Continue the most recent conversation
const session = await createCortexCodeSession({
  cwd: process.cwd(),
  continue: true,
});

// Or fork a resumed session into a new session ID
const forked = await createCortexCodeSession({
  cwd: process.cwd(),
  resume: "previous-session-id",
  forkSession: true,
});

MCP servers

You can connect to external systems through the Model Context Protocol:

from cortex_code_agent_sdk import CortexCodeAgentOptions

options = CortexCodeAgentOptions(
    mcp_servers={
        "my-tools": {
            "command": "node",
            "args": ["my-mcp-server.js"],
        },
    },
)

Hooks

You can run custom code at key points in the agent lifecycle. Available hook events include PreToolUse, PostToolUse, Stop, UserPromptSubmit, and more. Hooks are supported in both the Python and TypeScript SDKs. See the Python SDK reference or the TypeScript SDK reference for details.

Structured output

You can force the agent to return a response matching a JSON Schema:

const result = query({
  prompt: "Analyze this codebase",
  options: {
    cwd: ".",
    outputFormat: {
      type: "json_schema",
      schema: {
        type: "object",
        properties: {
          languages: { type: "array", items: { type: "string" } },
          summary: { type: "string" },
        },
        required: ["languages", "summary"],
      },
    },
  },
});

For more information, see Structured output.

Session control

You can control agent behavior through session options:

Option

Description

maxTurns / max_turns

Limit the number of agentic turns before the agent stops

effort

Set model thinking effort ("minimal", "low", "medium", "high", "max")

abortController / abort_event

Interrupt the running agent mid-turn. The session stays alive for further prompts.

env

Pass environment variables to the agent process

additionalDirectories / add_dirs

Add extra directories the agent can access beyond cwd

plugins

Load plugin directories for custom extensions

systemPrompt / system_prompt

Replace or append to the default system prompt

settingSources / setting_sources

Control which setting files are loaded ("user", "project", "local")

Supported models

Set the model using the model option. Snowflake recommends "auto" for automatic selection of the highest quality available model.

Model

Identifier

Auto (recommended)

auto

Claude Opus 4.6

claude-opus-4-6

Claude Sonnet 4.6

claude-sonnet-4-6

Claude Opus 4.5

claude-opus-4-5

Claude Sonnet 4.5

claude-sonnet-4-5

Claude Sonnet 4.0

claude-4-sonnet

OpenAI GPT 5.2

openai-gpt-5.2

Cross-region inference

Model availability varies by region. An account administrator can enable cross-region inference to access models not available locally:

ALTER ACCOUNT SET CORTEX_ENABLED_CROSS_REGION = 'AWS_US';

For more information, see Cross-region inference.

Next steps