Cortex Agent code execution tool

The Cortex Agent code execution tool is a built-in tool that enables an agent to execute code during a conversation. With access to a code execution tool enabled, your agents can execute scripts to process data, perform calculations, and produce visualizations. By default, the code execution tool runs in a sandboxed, isolated environment that can only access data in the current agent session.

On accounts where sandbox by default is on, every agent request gets a sandbox, and you don’t need to configure anything. On other accounts, you enable the code execution tool by configuring it in an agent specification. In both cases, the agent decides during orchestration when to generate and run code based on the user’s query. The code execution tool is also used when executing Python scripts as part of an agent skill.

There are two tool types that provide code execution capabilities:

  • code_execution: A single code execution tool for running Python in a sandbox. Use this when you want basic code execution alongside other agent tools (Cortex Analyst, Cortex Search, and others). This tool type is documented on this page.
  • code_toolset_all: The full Cortex Code sandbox toolset, which includes bash, file read/write/edit, grep, glob, web search, SQL execution, and skills. Use this when you want an autonomous coding agent with the full suite of development tools. See Coding Agent.

These two tool types are mutually exclusive — you cannot include both in a single request. Specifying both returns an error.

How the code execution tool works

The agent uses the code execution tool alongside other configured tools and skills. During orchestration, the agent evaluates the user’s query and determines whether code execution is the best approach. If so, the agent invokes the code execution tool.

The agent then generates code and executes it in a secure sandbox. By default, the code execution tool environment is isolated and can only access data passed into the session, plus the workspace stage mounted into the sandbox.

When the agent needs data from Snowflake, it runs the query with its SQL tools, outside the sandbox. See SQL in the sandbox.

Default access scope

The sandbox has read and write access to the workspace stage location mounted into it, which is your default user workspace. The agent sees that workspace as a regular directory. Stages mounted for skills are read-only. Beyond those mounts, the sandbox can’t reach data that wasn’t passed into the session.

Because the workspace is backed by a stage, files the agent writes there outlive the sandbox. The sandbox itself is scoped to a conversation thread and is reused across requests on that thread, but it isn’t shared between threads, and it suspends after a period of inactivity. Don’t rely on in-memory state, such as imported modules or variables, surviving between separate code executions: persist anything you need to reuse as a file in the workspace.

SQL in the sandbox

The sandbox connects to Snowflake through a child of the caller’s session, with a restricted session scope applied that grants no access to tables, views, or semantic views. Its only way to read data is RESULT_SCAN on a query the agent already ran with its SQL tools:

SELECT * FROM TABLE(RESULT_SCAN('<query_id>'));

Sandbox code can’t query tables, views, or semantic views, call Cortex AI functions, or run DDL or DML.

Sandbox by default

When the account parameter CORTEX_AGENT_SANDBOX_DEFAULT_ON is TRUE, every Cortex Agent request gets a sandbox, whether or not the agent specification includes the code_execution tool. This behavior change is described in Cortex Agents get a sandbox by default (Pending). While the parameter is FALSE, agents get a sandbox only when code_execution is in their specification, and sandbox_config is ignored.

The sandbox is the same one the code_execution tool uses, with the same tools, the same permission policy, and the same limits on SQL in the sandbox.

If the sandbox can’t start, the agent continues without code execution, and the response includes a warning.

User workspace

Whether the sandbox mounts the user’s default workspace depends on how the agent is called and whether the agent has the code_execution tool:

How the agent is calledAgent has code_executionAgent has no code_execution
CoWork (including Agent Studio and Slack)Mounted. The mount_user_default_workspace setting has no effect.Mounted. The mount_user_default_workspace setting has no effect.
Cortex Agents API (including MCP and SQL function calls)Mounted. Set mount_user_default_workspace: false to turn it off.Not mounted. Set mount_user_default_workspace: true to turn it on.

On the Cortex Agents API, mount_user_default_workspace takes precedence over the code_execution tool: true mounts the workspace and false doesn’t, whether or not the agent has code_execution. The workspace is mounted only if the API request also includes a thread ID.

When the workspace isn’t mounted, the sandbox still runs, but files the agent writes don’t persist, and the agent can’t present files to the user for download.

The sandbox_config section

Use the sandbox_config section of the agent specification to configure the sandbox. All keys are optional:

sandbox_config:
  mount_user_default_workspace: true
  artifact_repositories:
    - SNOWFLAKE.SNOWPARK.PYPI_SHARED_REPOSITORY
KeyTypeDescription
mount_user_default_workspaceBooleanWhether to mount the user’s default workspace. See User workspace for how this interacts with the way the agent is called.
artifact_repositoriesArray of stringsArtifact repositories the sandbox can install packages from. See Adding libraries through Artifact Repository. An empty list means no repository, and it overrides repositories set under code_execution.

Snowflake rejects an agent specification that contains sandbox_config when CORTEX_AGENT_SANDBOX_DEFAULT_ON is FALSE (error code 399511). Other keys in sandbox_config, values of the wrong type, and blank repository names are rejected too.

Precedence is per field: a key that’s set in sandbox_config overrides the same key under the code_execution tool. Settings that sandbox_config doesn’t have, such as external_access_integrations, permission_policy, and disabled_skills, stay under code_execution.

You can also pass sandbox_config in an agent run request. Keys in the request override the same keys in the agent specification. If the request contains sandbox_config while the parameter is FALSE, the run continues and the response includes a warning that sandbox_config wasn’t applied.

Enabling the code execution tool

On accounts where sandbox by default is off, the agent must have the code execution tool specified in its tools to get a sandbox. On accounts where it’s on, the tool is optional: add it when you need external_access_integrations, permission_policy, or disabled_skills, or to mount the user workspace for Cortex Agents API calls without setting mount_user_default_workspace.

Important

Calls to agents that use owner’s rights don’t support code execution. When an agent is invoked from an owner’s rights stored procedure, the code execution tools are removed from the request and no sandbox is created, even when the tool is enabled in the agent specification. The response includes a warning saying so. Invoke the agent with caller’s rights if it needs code execution.

Required Cortex Agent permissions

The following permissions on a Cortex Agent affect both your ability to configure and query the agent:

PrivilegeRequired for
USAGEAllows invoking the agent, including code execution tool use
MODIFYChanging an agent specification to enable or configure the code execution tool
OWNERSHIPFull control over agent configuration and use

Agent specification

You enable the code execution tool by adding the resources and configuration for it to an agent specification.

The tool definition to add in the tools section of your agent specification is:

tools:
  - tool_spec:
    type: code_execution
    name: code_execution

Enable the tool by adding a code_execution section to tool_resources in your agent specification:

tool_resources:
  code_execution:

In Agent Studio, what you see depends on CORTEX_AGENT_SANDBOX_DEFAULT_ON. While it is FALSE, the Code Execution tool toggle adds the required fields specified previously, and new agents created through the UI have the toggle switched on by default. While it is TRUE, Agent Studio shows a Sandbox Configuration section instead of the toggle, and you configure the sandbox through the sandbox_config section of the agent specification. See Sandbox by default.

For full information on the agent specification format and instructions on how to modify an existing agent’s specification, see Create and manage agents.

Sandbox permission policy

By default, tools that modify state require user approval before execution. You can control this behavior using the permission_policy field in tool_resources under the tool’s name. The policy applies to both the code_execution and code_toolset_all tool types.

Policy valueBehavior
always_askAlways prompt the user for approval before executing state-modifying tools (default)
always_allowSkip permission checks and execute all tools without user approval
{
  "tools": [
    {
      "tool_spec": {
        "type": "code_execution",
        "name": "code_execution"
      }
    }
  ],
  "tool_resources": {
    "code_execution": {
      "permission_policy": {
        "type": "always_ask"
      }
    }
  }
}

Important

Setting permission_policy to always_allow means the tool can execute code without user confirmation. Only use this setting in trusted, automated workflows where human-in-the-loop approval is not needed.

For the permission_policy as it applies to the full Cortex Code toolset, see Sandbox permission policy.

Coding Agent (full toolset)

The code_toolset_all tool type provides the full Cortex Code sandbox toolset — bash, file read/write/edit, grep, glob, web search, SQL execution, and skills — as a managed coding agent backed by the same runtime that powers Snowflake CoCo. Unlike code_execution, which provides a single Python execution tool, code_toolset_all routes the request through the Cortex Code orchestration path and automatically provisions the complete set of sandbox tools.

For the full toolset, workspace mounts, skill attachment (bundled, workspace, and stage-based), disabled skills, and orchestration instructions, see Coding Agent.

Default available libraries

The default execution environment for the code execution tool uses Python 3.12, with the Python standard library available. Common data-processing and plotting libraries are also preinstalled, including numpy, pandas, scipy, pyarrow, matplotlib, and plotly. To use additional packages, retrieve them from PyPI through the Artifact Repository, as described in the following section.

Adding libraries through Artifact Repository

You can use Snowflake’s default PyPI Artifact Repository to install PyPI packages in the code execution tool environment with support for package policies. Add the artifact_repositories key to the sandbox_config section of your agent specification, as a list containing an entry for SNOWFLAKE.SNOWPARK.PYPI_SHARED_REPOSITORY:

sandbox_config:
  artifact_repositories:
    - SNOWFLAKE.SNOWPARK.PYPI_SHARED_REPOSITORY

On accounts where sandbox by default is off, set it under the code_execution tool instead:

tool_resources:
  code_execution:
    artifact_repositories:
      - SNOWFLAKE.SNOWPARK.PYPI_SHARED_REPOSITORY

When both are set, sandbox_config is used. The sandbox uses one repository: if the list has more than one entry, the sandbox ignores the extra entries and the response includes a warning.

To access the PyPI repository, you must also assign the role SNOWFLAKE.PYPI_REPOSITORY_USER to the owner of the Cortex Agent.

Important

This gives the code execution tool access to retrieve any package published on PyPI. Use caution when granting this level of access.

Known limitations

Cortex Agent code execution tool is subject to the following known limitations:

  • Thread-scoped sandbox: A sandbox is scoped to a single conversation thread. It isn’t shared across threads, and in-memory state isn’t preserved between separate code executions. Files written to the mounted workspace do persist, because the workspace is backed by a stage.
  • No owner’s rights support: Calls to agents that use owner’s rights don’t support code execution. See Enabling the code execution tool.
  • No table access in the sandbox: Sandbox SQL is limited to RESULT_SCAN on queries the agent already ran. See SQL in the sandbox. The sandbox can still call Snowflake REST API endpoints that don’t operate on a securable object, such as the Cortex Agent run and thread APIs, but its session is granted no data access, so those calls can’t reach your data either.
  • Limited stage access: The sandbox can only read and write the workspace stage mounted into it, not arbitrary stages in your account.
  • sandbox_config and code_toolset_all: The sandbox_config section applies to the code_execution tool and to the sandbox that agents get by default. It doesn’t apply to agents that use the code_toolset_all tool type.