How to stop runaway queries in Snowflake: resource monitors, timeouts, and an agent that kills them
Statement timeouts, resource monitors and an agent that cancels what the first two miss
To stop runaway queries in Snowflake, set STATEMENT_TIMEOUT_IN_SECONDS on every warehouse and attach a resource monitor with a SUSPEND_IMMEDIATE trigger as the credit backstop. The Snowflake parameters documentation lists the timeout default as 172,800 seconds (two days), so a query that never finishes can run for 48 hours unless you lower it. Timeouts and monitors are static limits. They do not tell you which query is the problem, so a third layer is needed: something that watches running queries and cancels the bad ones.
Key Takeaways
- •The default statement timeout in Snowflake is two days. Set a much lower value per warehouse, for example one hour for ETL and a few minutes for BI.
- •Resource monitors cap credits, not queries. They suspend a warehouse after spend crosses a threshold. Suspension is not instantaneous, so credits can still overshoot the quota.
- •
SYSTEM$CANCEL_QUERYcancels a single query by ID. It is the manual kill switch that every automated approach ends up calling. - •The three layers do different jobs. Timeouts bound one query, monitors bound spend, and an agent decides which running queries are anomalous and acts on them.
- •Our Cost Agent is an MCP server, so you can run this detection loop from Claude Code or Cursor. The OSS edition is read-only, and cancelling queries needs the Pro tier's writes and approvals.
How to stop runaway queries in Snowflake: the three layers
A runaway query is one that consumes far more warehouse time than anyone expected. Typical causes are an accidental cross join, a missing filter on a large table, an unbounded recursive CTE, or a BI tool that fires the same expensive query on every dashboard refresh. Billing follows warehouse uptime, so one stuck query on a large warehouse keeps the whole warehouse running and billing. No single Snowflake setting covers every case, so we use three layers.
| Layer | What it bounds | When it acts | What it misses |
|---|---|---|---|
| Statement timeout | Runtime of one query | After a fixed number of seconds | Queries that are slow but legitimate, and many small queries that add up |
| Resource monitor | Credits per warehouse or account | After spend crosses a threshold | Which query caused the spend; credits can overshoot before suspension |
| Agent-driven cancellation | Anomalous queries by pattern | While the query is running | Needs write access and an approval policy before it can act |
Step 1: Set statement timeouts per warehouse
STATEMENT_TIMEOUT_IN_SECONDS can be set at account, user, session and warehouse level, and the most specific level wins. Warehouse level is the practical choice, because workloads map to warehouses. For example: ALTER WAREHOUSE etl_wh SET STATEMENT_TIMEOUT_IN_SECONDS = 3600; gives ETL one hour, and ALTER WAREHOUSE bi_wh SET STATEMENT_TIMEOUT_IN_SECONDS = 300; gives dashboards five minutes. Pick values from your own query history. A good starting point is a bit above the slowest legitimate query on that warehouse.
Also set STATEMENT_QUEUED_TIMEOUT_IN_SECONDS. It controls how long a query waits in the queue before Snowflake cancels it, and its default is no limit. Without it, a spike of queued queries can pile up behind a runaway and all start together once it clears.
Step 2: Attach a resource monitor as the credit backstop
A resource monitor tracks credit consumption and fires actions at percentage thresholds. The resource monitor documentation describes three actions: NOTIFY, SUSPEND (let running statements finish, then suspend) and SUSPEND_IMMEDIATE (cancel running statements and suspend). Creating one requires the ACCOUNTADMIN role. A typical monthly monitor for one warehouse:
CREATE RESOURCE MONITOR rm_etl WITH CREDIT_QUOTA = 500 FREQUENCY = MONTHLY START_TIMESTAMP = IMMEDIATELY TRIGGERS ON 80 PERCENT DO NOTIFY ON 100 PERCENT DO SUSPEND ON 110 PERCENT DO SUSPEND_IMMEDIATE; then ALTER WAREHOUSE etl_wh SET RESOURCE_MONITOR = rm_etl;. The 110 percent trigger exists because suspension is not instantaneous. The docs note that a warehouse can consume additional credits between the threshold being reached and the suspension taking effect.
The limitation is that a monthly quota is a blunt instrument. A runaway query on day three can burn the quota, and then the warehouse is suspended for the rest of the month while legitimate work waits. That is why we treat monitors as a backstop and not as the primary control. Use shorter frequencies (daily or weekly) on critical warehouses so a bad day does not cost a bad month.
Step 3: Find and cancel running queries manually
When an alert fires, you need the offending query. The INFORMATION_SCHEMA.QUERY_HISTORY table function returns recent queries with low latency, unlike the ACCOUNT_USAGE views, which the Snowflake documentation says can lag by up to 45 minutes. Filter on execution_status = 'RUNNING' and sort by total_elapsed_time descending to see the longest-running statements, their users and their warehouses.
Once you have the ID, SELECT SYSTEM$CANCEL_QUERY('<query_id>'); cancels that one statement. If a whole warehouse is the problem, ALTER WAREHOUSE etl_wh ABORT ALL QUERIES; cancels everything running on it. Prefer the single-query form. Aborting everything also kills the healthy jobs that share the warehouse.
Step 4: Automate detection with an agent
Steps 1 to 3 leave a gap. A timeout cannot tell a legitimate two-hour backfill from a cross join, and a monitor only reacts after money is spent. The missing piece is something that compares each running query with what that warehouse normally does, such as the same query text, user or dbt model, and flags the outliers. Doing that by hand means someone checking query history every few minutes.
Our Cost Agent does this job. It runs as an MCP server, so from Claude Code or Cursor you can ask questions such as "What's causing the warehouse queue to spike?" or "How much are we spending on Snowflake this month?" and get the running queries, who started them and the credits they are burning. The Model Context Protocol is what lets the same agent work in any compatible client. The Cost Agent reads through the Connectors Agent, so you do not wire up Snowflake credentials twice.
On the autonomy question, we recommend starting in recommend-only mode. The OSS edition (dw-claw, Apache 2.0) is read-only by default. It will tell you which query to cancel and why, and you run SYSTEM$CANCEL_QUERY yourself. The Pro tier adds writes and approval workflows, so the agent can propose a cancellation and wait for a human to approve. Our Autonomous Data Conductor adds a per-domain autonomy dial, so you can let the agent cancel unattended on a dev warehouse while production still requires approval. In each case, cancellations are written to the audit trail.
Step 5: Prevent the repeat
Cancelling a query fixes today's bill. To stop the next one, record why it ran away. The usual fixes are: add a partition or date filter to the model, right-size the warehouse so a bad query hits the timeout sooner, move heavy BI extracts to a separate warehouse with a short timeout, and review joins in pull requests before they ship. We covered related sizing waste in our other posts on Snowflake cost control, and the Cost Agent chains with our observability and usage agents so a repeat offender shows up with its owner attached.
Frequently Asked Questions
What is the default query timeout in Snowflake? The default for STATEMENT_TIMEOUT_IN_SECONDS is 172,800 seconds, which is 48 hours. Set a lower value at the warehouse level so a stuck query cannot run for two days.
Does a resource monitor kill a single runaway query? Not directly. A resource monitor watches total credits and can suspend a warehouse when a threshold is crossed. SUSPEND_IMMEDIATE cancels running statements as part of the suspension, but it also cancels the healthy ones. For a single query, use a statement timeout or SYSTEM$CANCEL_QUERY.
How do I cancel a running query in Snowflake? Find the query ID in INFORMATION_SCHEMA.QUERY_HISTORY or Snowsight's query history, then run SELECT SYSTEM$CANCEL_QUERY('<query_id>');. You need to own the query or hold a role with the privilege to operate on the warehouse.
Can an AI agent cancel Snowflake queries automatically? Yes, if it has write access. We recommend a staged approach: recommend-only first, then approval-gated cancellation, then unattended cancellation for low-risk warehouses once the false-positive rate is low. The Data Workers OSS edition is read-only, and writes with approvals come with Pro.
Will lowering the timeout break legitimate jobs? It can. Check the longest legitimate runtime per warehouse in query history first, then set the timeout above it. Give long backfills their own warehouse or a session-level override so they are not capped by the BI limit.