Cost Attribution

Concepts covered: Pipeline Monitoring

A warehouse bill reports daily spend for each account and compute cluster. It cannot say that orders_daily cost $4,200 last month, because the warehouse has no idea what a pipeline is. Cost attribution supplies that idea: every query a pipeline sends carries a tag naming who sent it, and the warehouse's query log turns into a ledger you can group by pipeline and then break down by task or by run.

What goes in a query tag

{
  "pipeline": "orders_daily",
  "task": "build_fct_orders",
  "run_id": "orders_daily__2026-10-20T06:00",
  "owner": "payments-data",
  "env": "prod"
}

Each field has a job. pipeline and task are the grouping keys for the rollup. run_id ties a query's cost to 1 run and to that run's logs; it is safe here because the query log stores 1 row per query and mints no metric series. owner says whom to talk to. env keeps CI and development spend out of the production numbers.

Many warehouses let a client attach a tag or label to its session and record it on each query in the log. Set the tag in 1 place, the orchestrator's task wrapper or the shared database client, so every task inherits it. A hand-typed tag is missing from the first query someone writes in a hurry, which lands in the untagged bucket.

The rollup

SELECT
  coalesce(json_extract_scalar(query_tag, '$.pipeline'), 'untagged') AS pipeline,
  count(DISTINCT json_extract_scalar(query_tag, '$.run_id')) AS runs,
  sum(cost_usd) AS total_cost_usd
FROM query_history
WHERE start_time >= current_timestamp - INTERVAL '30' DAY
  AND coalesce(json_extract_scalar(query_tag, '$.env'), 'prod') = 'prod'
GROUP BY
  1
ORDER BY
  total_cost_usd DESC

Joined with lineage, the rollup gains its most useful column: how many distinct users and dashboards queried the pipeline's output tables in the same 30 days, from the query-log lineage of the previous section.

pipeline30-day costrunscost per runprior 30 days, cost per runreaders in 30 days
clickstream_sessions$11,800720$16.39$16.103
marketing_attribution$6,90030$230$2280
orders_daily$4,20030$140$609
untagged$3,0000n/an/an/a
inventory_snapshot$2,10030$70$695

Reading it for savings

Patterns worth a closer look

Rank these by expected saving, then by the risk of acting. Pausing an unread pipeline saves the most here and comes undone in a minute. The orders_daily jump is smaller in dollars, but it is a defect that keeps billing until someone finds it, and the run_id in every tag leads to the first expensive run. The steady pipelines can wait.

The team has 1 afternoon for orders_daily, whose cost per run rose from $60 to $140 starting with the first run after a deploy on 2026-09-24. Which next step uses the attribution data most directly?

About this lesson

This section is part of the Pipeline Operations: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.