Tutorial: Analyze Snowflake data with Cortex AI in a sandbox¶
This tutorial shows how to combine a live Snowflake data query with a Cortex AI analysis, all running inside a single isolated sandbox. The sandbox uses your injected session identity to query data and call SNOWFLAKE.CORTEX.COMPLETE via SQL — no credentials are configured or forwarded.
The pattern scales to any data-plus-AI workflow: summarize operational metrics, classify query results, flag anomalies, or generate natural-language reports over Snowflake data.
Time to complete: about 15 minutes.
Prerequisites¶
- The
snowflake-sandbox-pythonSDK installed and a Snowflake connection configured on your local machine. See Cortex Sandboxes quickstart. - Run
main.pyfrom a local terminal, not from a Snowflake Workspace. When the driver runs inside a Workspace, the inner sandbox does not inherit the credentials needed forsnowflake.connectorto connect back to Snowflake. - Your role must have access to
SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORYandUSAGEon the CortexCOMPLETEfunction. If you do not haveACCOUNT_USAGEaccess, substitute any table with numeric data you do have access to.
Step 1: Write the analyst script¶
The following script runs inside the sandbox. It queries WAREHOUSE_METERING_HISTORY for the last seven days, formats the data, passes it to SNOWFLAKE.CORTEX.COMPLETE for analysis, and prints the result.
Replace llama3.1-8b with any Cortex model your role has access to. See COMPLETE (SNOWFLAKE.CORTEX) for the full model list.
Step 2: Write the driver and run it¶
The driver creates one sandbox, writes analyst.py into it, and runs it. Save this as main.py in the same directory as analyst.py.
How to run this example¶
Save analyst.py and main.py in the same directory and run:
Expected output:
The exact output depends on your account’s warehouse activity.
What this demonstrates¶
- No credentials are configured. The sandbox authenticates with the session token the platform provides, which
get_snowflake_connection()locates for you. Your PAT is not passed to the container. - Data and AI in the same isolated environment. The sandbox queries your live Snowflake data and calls Cortex AI in the same execution context, fully contained within your Snowflake account.
- Cortex runs in-warehouse.
SNOWFLAKE.CORTEX.COMPLETEis a Snowflake SQL function. The LLM inference runs on Snowflake compute and your data does not leave the platform. - Your governance policies apply. Because the sandbox runs as your session, row-level security and column masking policies apply to every query — including the queries the AI analysis is based on. See Tutorial: Governance enforcement in a sandbox.
Going further¶
This tutorial shows a single-sandbox, single-query analysis. For larger workloads:
- Fan out across multiple sandboxes to process different tables or time windows in parallel, as shown in Tutorial: Run Cortex batch inference over a Snowflake table.
- Pass the analyst’s output back to the driver and write it to a Snowflake table for downstream use.
- Extend the
analyst.pyscript with multiple queries, conditional logic, or a loop over Cortex calls to build a more complete agentic workflow.