BigQuery and Snowflake
Cost per dbt model on BigQuery and Snowflake
Neither warehouse knows which dbt model a query came from. dbt tells it anyway: by default it adds a comment to every query it runs, /* {"app": "dbt", ..., "node_id": "model.my_project.orders"} */, and node_id names the model. Pull that out of the query text and the warehouse's own cost columns do the rest.
BigQuery
The comment is in the query column of JOBS_BY_PROJECT. If your profile sets query-comment with job-label: true, dbt-bigquery sends the same fields as job labels instead (app: dbt, node_id: model_my_project_orders — BigQuery lowercases labels and turns the dots into underscores). The query reads either.
Statements inside a script are counted on their own and the script's parent job is not, so nothing is counted twice. A dbt query with no node_id — dbt's own metadata queries, for instance — lands in the NULL row.
-- Cost per dbt model, the last 30 days, from dbt's query comment or its job labels.
WITH dbt_jobs AS (
SELECT
COALESCE(
REGEXP_EXTRACT(query, r'"node_id": *"([^"]+)"'),
(SELECT value FROM UNNEST(labels) WHERE key = 'node_id')
) AS dbt_node,
total_bytes_billed,
total_slot_ms
FROM `region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
AND job_type = 'QUERY' AND state = 'DONE'
AND IFNULL(statement_type, '') != 'SCRIPT'
AND (IFNULL(total_bytes_billed, 0) > 0 OR IFNULL(total_slot_ms, 0) > 0)
AND (
REGEXP_CONTAINS(query, r'"app": *"dbt"')
OR EXISTS (SELECT 1 FROM UNNEST(labels) WHERE key = 'app' AND value = 'dbt')
)
)
SELECT
dbt_node,
COUNT(*) AS runs,
ROUND(SUM(total_bytes_billed) / POW(1024, 4), 3) AS tib_billed,
ROUND(SUM(total_bytes_billed) / POW(1024, 4) * 6.25, 2) AS on_demand_usd,
ROUND(SUM(total_slot_ms) / 3600000, 1) AS slot_hours
FROM dbt_jobs
GROUP BY dbt_node
ORDER BY tib_billed DESC, slot_hours DESC6.25 is BigQuery's on-demand list price per TiB and 3 is the list price per Snowflake credit that dwhat assumes by default. Put the rate on your contract in their place. Change region-us to the region your jobs run in.
See a sample audit: a made-up BigQuery project's cost split by dbt model, with the queries dbt did not run shown as such.
Documented in: dbt: query-comment · BigQuery: INFORMATION_SCHEMA.JOBS view
Snowflake
Snowflake removes comments from the start of a query, so dbt-snowflake puts its comment at the end. QUERY_HISTORY keeps it in query_text; QUERY_ATTRIBUTION_HISTORY has the credits.
-- Cost per dbt model, the last 30 days, from dbt's query comment. SELECT REGEXP_SUBSTR(q.query_text, '"node_id": *"([^"]+)"', 1, 1, 'e', 1) AS dbt_node, COUNT(*) AS runs, ROUND(SUM(a.credits_attributed_compute), 2) AS credits, ROUND(SUM(a.credits_attributed_compute) * 3, 2) AS list_usd FROM snowflake.account_usage.query_attribution_history a JOIN snowflake.account_usage.query_history q ON q.query_id = a.query_id AND q.start_time >= DATEADD(day, -31, CURRENT_TIMESTAMP()) WHERE a.start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP()) AND REGEXP_INSTR(q.query_text, '"app": *"dbt"') > 0 GROUP BY dbt_node ORDER BY credits DESC
6.25 is BigQuery's on-demand list price per TiB and 3 is the list price per Snowflake credit that dwhat assumes by default. Put the rate on your contract in their place. Change region-us to the region your jobs run in.
Documented in: dbt: query-comment · Snowflake: QUERY_ATTRIBUTION_HISTORY view
What this misses
- A custom query-comment in dbt_project.yml that drops node_id leaves nothing to group by.
- It shows which models cost the most, not which one started costing more this week. That needs the same query per day, kept, and compared.
- On Snowflake, idle time on the dbt warehouse is not attributed to any model.
Run this every day instead
dwhat reads the same history with a read-only account, every day. It keeps the daily cost by user, dbt model and dashboard, emails you when a day costs well above its 14-day median and says what grew, and lists the 20 most expensive queries with the fixes above where the evidence supports them.