guide
guide8 min read

Snowflake Query Tagging for Cost Tracking: A Step-by-Step Tutorial

Tag every query, join tags to metering data, and let an agent act on the result

Snowflake query tagging for cost tracking works by setting the QUERY_TAG session parameter on every query, then grouping credits by that tag in the ACCOUNT_USAGE views. Per the Snowflake documentation on QUERY_TAG, the tag is a string of up to 2,000 characters that is recorded against each query, and QUERY_ATTRIBUTION_HISTORY reports the compute credits attributed to each query. Join the two and you get credits per team, job or dashboard.

Key Takeaways

  • •QUERY_TAG is a session parameter, so it can be set per session, per user, or per account, and it lands on every query in QUERY_HISTORY.
  • •Credits per query come from the QUERY_ATTRIBUTION_HISTORY view in SNOWFLAKE.ACCOUNT_USAGE; join it to QUERY_HISTORY on query_id to group by tag.
  • •A JSON-shaped tag (team, job, env) is easier to query and enforce than a free-text string.
  • •Tags only help if coverage is high, so measure the share of untagged credits and drive it down.
  • •Reporting is the easy half. Acting on it (resizing a warehouse, pausing a job, chasing an owner) is where an agent saves time.

This tutorial assumes you have ACCOUNT_USAGE access (the IMPORTED PRIVILEGES grant on the SNOWFLAKE database, or a role granted the relevant database roles). Two caveats from Snowflake's own docs: ACCOUNT_USAGE views have latency (QUERY_HISTORY can lag by up to about 45 minutes), and query attribution does not include warehouse idle time. We cover both in step four.

Step 1: Decide your Snowflake query tagging for cost tracking schema

Before you set a single tag, decide what questions finance and engineering will ask. Most teams need three dimensions: who pays (team or cost center), what ran (pipeline, dashboard or ad hoc), and where (environment). Keep the tag short and machine-parseable. We recommend a JSON object, because Snowflake's PARSE_JSON and the colon path syntax make it trivial to query later. This is our opinion from working with these setups, not a Snowflake requirement.

  • •team: the owning group, taken from a fixed list (analytics, growth, finance-eng).
  • •job: a stable identifier such as the dbt project, Airflow DAG id, or BI dashboard name.
  • •env: prod, staging or dev.
  • •Avoid timestamps, run ids or user emails in the tag. They explode cardinality and make group-by useless.

Example tag: {"team":"growth","job":"daily_orders","env":"prod"}. Write the schema down in the repo next to your dbt project so the allowed values are reviewable in a pull request.

Step 2: Set QUERY_TAG at the session, user, or account level

There are three places to set the tag, and they stack from most to least specific. A session-level ALTER SESSION overrides a user-level default, which overrides the account default.

  • •Per session: ALTER SESSION SET QUERY_TAG = '{"team":"growth","job":"daily_orders","env":"prod"}';
  • •Per service user: ALTER USER svc_airflow SET QUERY_TAG = '{"team":"data-eng","job":"airflow","env":"prod"}'; this gives you a floor of attribution for every orchestrator login.
  • •Per account: ALTER ACCOUNT SET QUERY_TAG = '{"team":"unattributed"}'; a catch-all default that makes untagged spend visible instead of blank.

For dbt, the dbt-snowflake adapter supports a query_tag setting in the profile, and you can also set it per model with a config. Check the dbt Snowflake configuration docs for the current syntax and for how the adapter resets the tag after each model. For Airflow or Python jobs, run the ALTER SESSION statement as the first statement on the connection, or pass the tag as a connection parameter in your driver.

Step 3: Join QUERY_ATTRIBUTION_HISTORY to QUERY_HISTORY

QUERY_ATTRIBUTION_HISTORY carries the credits; QUERY_HISTORY carries the tag. Join them on query_id and group by a field pulled from the tag. A starting query for the last seven days:

  • •SELECT TRY_PARSE_JSON(q.query_tag):team::string AS team, TRY_PARSE_JSON(q.query_tag):job::string AS job, SUM(a.credits_attributed_compute) AS credits
  • •FROM snowflake.account_usage.query_attribution_history a
  • •JOIN snowflake.account_usage.query_history q ON q.query_id = a.query_id
  • •WHERE a.start_time >= DATEADD('day', -7, CURRENT_TIMESTAMP())
  • •GROUP BY 1, 2 ORDER BY credits DESC;

TRY_PARSE_JSON returns NULL on malformed tags rather than failing the whole query, which matters because free-text tags from legacy tools will be in the same column. Multiply credits by your contracted price per credit to get dollars. Per Snowflake's documentation, attributed credits cover compute only; cloud services credits are reported in a separate column, so include credits_used_cloud_services if you want the full picture.

Step 4: Account for idle time and untagged spend

Two gaps make tag-based totals lower than your invoice. First, Snowflake notes that attribution excludes warehouse idle time, so the sum of attributed credits will be below WAREHOUSE_METERING_HISTORY for the same period. Second, any query without a tag lands in a NULL bucket. Handle both explicitly.

  • •Idle gap: compare SUM(credits_used_compute) from WAREHOUSE_METERING_HISTORY against attributed credits per warehouse. The difference is idle cost. Allocate it to warehouse owners proportionally, or report it as its own line.
  • •Untagged share: report the percentage of attributed credits where the team field is NULL. Set a target (we suggest under 5%) and review it weekly.
  • •Short-lived warehouses: a warehouse that auto-suspends at 60 seconds still bills a 60-second minimum on each resume, so many tiny queries can cost more than their attributed credits suggest.

If you also need storage and serverless costs, object tags (TAG on warehouses, databases and tables) cover resources, while query tags cover work. They answer different questions, so most teams end up using both.

Step 5: Enforce tags and automate the follow-up

A tagging scheme decays unless something checks it. Two guardrails help. A scheduled task can write the untagged-credit percentage to a table and alert when it crosses your target. A resource monitor can cap credits on a warehouse, but note that it limits spend rather than explaining it. Neither tells you why the growth team's credits doubled on Tuesday.

That follow-up is investigative work: find the top queries behind the jump, check whether a warehouse was resized, look for a job that started rerunning, and find an owner. Our Cost Agent runs this loop over your tags. It detects waste, idle warehouses and sizing inefficiencies, and it is an MCP server, so you can ask it from Claude Code or Cursor in plain language: "How much are we spending on Snowflake this month?" or "What's causing the warehouse queue to spike?" It chains with our observability and pipelines agents, so a cost spike can be traced to the pipeline run that caused it.

The OSS edition is read-only by default, so it can report and recommend without touching your account. Writes such as resizing a warehouse or changing auto-suspend sit behind approval workflows in Pro. We wrote about how the Model Context Protocol makes each agent callable from your editor in our MCP posts, and our Connectors Agent handles the Snowflake connection, including the ACCOUNT_USAGE grants described above.

Common mistakes in Snowflake query tagging for cost tracking

  • •Tagging only dbt. BI tools, notebooks and reverse-ETL jobs often run the most expensive queries; set a user-level default for each of their service users.
  • •High-cardinality tags. A tag containing a run id makes every row unique and the report useless.
  • •Trusting the tag blindly. Any user who can run ALTER SESSION can set any tag, so treat tags as attribution, not security, and audit them against role and user.
  • •Ignoring latency. ACCOUNT_USAGE views lag, so do not build a real-time alert on them; the INFORMATION_SCHEMA table functions are fresher but cover a shorter window.
  • •Forgetting that the tag resets. A pooled connection can carry a stale tag from a previous job if you do not set it at the start of each unit of work.

Frequently Asked Questions

What is a Snowflake query tag? A query tag is a string stored in the QUERY_TAG session parameter and recorded in QUERY_HISTORY for every statement run in that session. Snowflake does not interpret it, which is why teams use it to carry team, job and environment labels.

How long can a Snowflake query tag be? Snowflake's parameter documentation lists a maximum of 2,000 characters. Keep tags far shorter in practice so they stay readable in the UI and cheap to parse.

Can I get cost per query in Snowflake? Yes, in the form of attributed compute credits. QUERY_ATTRIBUTION_HISTORY reports credits per query, excluding warehouse idle time, so per-query totals will not sum to your warehouse bill.

Should I use object tags or query tags for cost allocation? Use object tags to label resources such as warehouses and databases, and query tags to label the work that runs on them. For compute cost by team, query tags give finer attribution on shared warehouses.

Can an AI agent act on Snowflake cost tags? Yes. An MCP-native agent can read the tag-based report, explain a spike and propose a fix from your editor. In our setup the OSS edition only reads, and any change to a warehouse needs an approval step in Pro.