Query tags: put a name on every credit
A few lines in dbt_project.yml turn Snowflake's bill into a report you can group by team.
The bill went up 40% last month. Ismail wants to know which team caused it, and Snowflake's query history has the answer in front of him: thousands of rows of SQL text, warehouse names and run times, with nothing saying who any of it belongs to.
A query tag is the missing column. It's a label set on a session (one open connection, like the one a dbt run opens), and Snowflake stores it with every query that session runs. A run that was anonymous arrives already labelled:
That one extra field is the difference between an investigation and a GROUP BY. Each team gets its own line in a cost dashboard, each product its own curve, and the monthly cost review becomes a filter change instead of a meeting.
Set the tag once, in dbt_project.yml
You don't tag queries by hand. Set a small JSON tag in dbt_project.yml, carrying the two fields you'll group the bill by, team_name and product_name. The + marks a config rather than a folder name, as with +materialized in step 4:
# dbt_project. models: my_project: +query_tag: '{"team_yml name": "data", "product_ # every model marts: +query_tag: '{"team_name": "platform"}' name": "finance", "product_ # models/name": "revenue"}' marts/ wins
- Staging and intermediate carry the project default: the data team owns that plumbing.
- Seeds, snapshots and tests aren't under
models:. They take the same+query_tagunder their ownseeds:,snapshots:anddata_tests:keys. - A
query_tagon the profile target is the session default for everything dbt runs. A model's tag replaces it while that model builds.
Override it on the one model that needs it
A folder-level tag covers 95% of the project. For the odd model another team pays for, put a config block at the top of the model file:
-- models/marts/ {{ config( query_tag = '{"team_dim_ customer. sql name": "growth", "product_ ) }}name": "crm"}'
Under the hood, dbt-snowflake runs alter session set query_tag before building the model and puts the previous tag back afterwards. Model config wins over folder config, and folder over project: the usual dbt hierarchy.
Three rules keep tags useful:
- JSON, never free text, so Snowflake can parse it.
- Stable keys with few distinct values (team_name, product_name), so a
GROUP BYmeans something. - One convention everywhere. Most BI tools can send a query tag from their Snowflake connection settings. Use the same keys, and dashboard spend lands in the same buckets.
To check it worked, run a model, then list your own last queries in a worksheet, with no delay. query_history() is a table function (you query it like a table) found in every database, so pick any database first:
SELECT query_tag, query_text FROM TABLE(information_ ORDER BY start_time DESC LIMIT 20schema.query_ history ())
Read it back: one query, credits per team
First, what QUERY_HISTORY actually is
Snowflake logs every query run against your account: by hand, by dbt, by a BI tool. It keeps that log for a year in a view called SNOWFLAKE.ACCOUNT_ (rows show up within about 45 minutes). You query the view with plain SQL in a Snowsight worksheet. One row per query, and the columns you'll actually use:
query_idquery_textquery_tagwarehouse_namestart_time, total_elapsed_timeuser_name, role_nameIt holds what ran, where, when and who, but not the compute cost. Credits live in a second view:
QUERY_ATTRIBUTION_HISTORY: one row per query with the compute credits it used, plusquery_tag,warehouse_nameandstart_time. A cost-per-tag report needs nothing else.- A credit has a fixed price in your contract, a few dollars. Credits times price is money.
For the SQL text or the role too, join the two views:
SELECT a.query_id, a.credits_ FROM snowflake.attributed_ compute, q.role_name, q.query_text account_ JOIN snowflake.usage. query_ attribution_ history a account_ usage. query_ history q ON q.query_id = a.query_id
Three things to know about the credits view:
- Rows can take up to eight hours to appear.
- Queries under roughly 100 ms aren't listed.
- Idle time isn't attributed. A warehouse keeps billing until its auto-suspend timer fires, and those seconds belong to no query. So the total sits below your bill.
Who can read them. On a trial, ACCOUNTADMIN already can. USERLAND_ROLE can't: the views live in the shared SNOWFLAKE database. An admin opens both to a cost-reading role, once:
CREATE ROLE IF NOT EXISTS FINOPS_ROLE; GRANT DATABASE ROLE SNOWFLAKE.GOVERNANCE_ GRANT ROLE FINOPS_ROLE TO USER your_login; -- then USE ROLE FINOPS_ROLEVIEWER TO ROLE FINOPS_ROLE;
The query itself
Parse the JSON tag, sum the credits, group by week. That's the whole report Ismail can hand to anyone:
WITH tagged AS ( SELECT try_parse_ (query_tag) AS tag, -- NULL instead of an error if not JSON date_trunc(week, start_time) AS week, warehouse_json name, credits_attributed_ FROM snowflake.compute account_ WHERE start_time > dateadd(week, -8, current_date()) ) SELECT week, tag:usage. query_ attribution_ history team_ tag:name: : string AS team_name, -- tag:key reads a JSON field product_ warehouse_name: : string AS product_ name, name, sum(credits_ FROM tagged WHERE tag IS NOT NULL -- untagged or non-JSON tags drop out GROUP BY 1, 2, 3, 4 ORDER BY week, credits DESCattributed_ compute) AS credits
The whole loop: from dbt run to a cost dashboard
That last box is the FinOps deliverable (FinOps: managing cloud cost as a shared team habit). Plug the query above into your BI tool and you have the consumption dashboard: spend by team, by product, by warehouse, week over week. When a spike appears, the tag names the owner before anyone opens a ticket.
DEV_WH and production uses PROD_WH, so warehouse_name already keeps her dev runs out of finance's production bill.
