Tutorial: Run a LangGraph agent over Snowflake data in a sandbox¶
This tutorial shows how to run a LangGraph StateGraph agent inside a Cortex Sandbox. The agent queries live Snowflake data using your injected session identity, calls SNOWFLAKE.CORTEX.COMPLETE for AI reasoning, and is contained by the platform’s egress baseline, so it cannot call an external AI provider or any other host outside that set.
When you finish, you will have a working pattern for deploying any LangGraph agent against Snowflake data with governance enforced and credentials never leaving the platform.
Why run LangGraph inside a sandbox¶
LangGraph agents are typically run in the same environment as the developer, which gives the agent access to the developer’s credentials, environment variables, and network. Running the agent inside a Cortex Sandbox changes three things:
- Governance is enforced automatically. Every query the agent runs goes through your session identity. Row masking, column masking, and row access policies apply to what the agent can read, exactly as they would for a query you run directly. The agent cannot see more data than your role permits.
- Egress is locked down. The sandbox’s outbound network access is controlled by an
Egresspolicy. The defaultEgress()gives the container the platform baseline (Snowflake, its cloud-storage stages, and roughly 43 package-manager hosts) and nothing beyond that — so even if the agent tried to call an external LLM endpoint, the egress proxy would block it. OnlySNOWFLAKE.CORTEX.COMPLETE(which runs as SQL inside Snowflake) is reachable. - No credentials enter the agent. The platform injects a session token at
/snowflake/session/token. The agent script connects to Snowflake using only that token — no PAT, no environment variable, no connection string.
Time to complete: about 15 minutes.
Prerequisites¶
- The
snowflake-sandbox-pythonSDK installed and a Snowflake connection configured. See Cortex Sandboxes quickstart. - Your role must have access to
SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORYandUSAGEon the CortexCOMPLETEfunction.
How the agent works¶
The agent is a LangGraph StateGraph with three nodes and one conditional edge:
query_noderuns SQL againstWAREHOUSE_METERING_HISTORY, fetching the top five warehouses by credits used in the last seven days.summarize_nodepasses the rows toSNOWFLAKE.CORTEX.COMPLETEand asks for a plain-English analysis.fallback_nodehandles the case where no rows are returned (for example, no warehouse activity in the window).
Both query_node and summarize_node share a single Snowflake connection opened once at startup. The credential the platform provides is valid for the sandbox’s lifetime; opening multiple connections from it is not reliable.
Step 1: Write the agent script¶
Save the following as agent.py. This is the code that runs inside the sandbox.
Replace llama3-8b with any Cortex model your role has access to. See COMPLETE (SNOWFLAKE.CORTEX) for the full model list.
Step 2: Write the driver¶
The driver starts the sandbox, sends agent.py into it, installs LangGraph inside it, then runs the agent. Save this as main.py in the same directory as agent.py.
Dependencies are installed into each sandbox rather than baked into a reusable
image. pip reaches pypi.org and files.pythonhosted.org through the
platform’s package-manager baseline, so the install needs no egress
configuration and no external access integration.
Step 3: Run the example¶
Save agent.py and main.py in the same directory and run:
The whole run takes roughly ten seconds: a few seconds to start the sandbox, a few more to install LangGraph, then the agent’s own query and Cortex call.
Expected output:
The exact output depends on your account’s warehouse activity.
What this demonstrates¶
- No credentials in the agent.
agent.pyconnects using only the token the platform binds at/snowflake/session/token. Your PAT is not passed to the container. - One connection for the graph. The agent opens a single
snowflake.connectorconnection and shares it across all graph nodes via closures. The platform session token is reliable for the sandbox’s lifetime but not for multiple simultaneous connections. - Governance is enforced. The agent’s queries run under your session identity. If your role has column masking or row access policies on
WAREHOUSE_METERING_HISTORY, they apply — the agent cannot read past them. - External LLM endpoints are blocked. The platform baseline
Egress()does not include external AI provider endpoints. The agent cannot call OpenAI, Anthropic, or any other API even if LangGraph is configured to do so. Cortex AI is available through SQL, not HTTP, so it is unaffected. - LangGraph runs unmodified. The
agent.pyscript uses the standard LangGraph API —StateGraph,add_node,add_conditional_edges. No sandbox-specific changes to the agent code are needed. - Dependencies install from inside the sandbox.
pypi.organdfiles.pythonhosted.orgare in the platform’s package-manager egress baseline, so installing LangGraph needs noegressentry and no external access integration.
Going further¶
- Add more graph nodes. The
StateGraphcan include nodes that compare against a historical baseline, check for anomalies across multiple time windows, or generate a formatted report. - Fan out across questions. Use
ThreadPoolExecutorto launch multiple sandboxes in parallel, each answering a different question or analyzing a different table — the same pattern as Tutorial: Run Cortex batch inference over a Snowflake table. - Add a Snowflake secret for an external LLM. To use an external model instead of Cortex, mount a
Secretand add the model’s host toEgress. The credential’s value is not placed in the container. See Tutorial: Give a sandbox secure access to an external API. - Use a warm sandbox for interactive sessions. Rather than creating a sandbox per question, keep one alive and call
sb.send(question)in a loop to keep the agent warm across multiple questions without restarting.