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 DESCJoined 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.
| pipeline | 30-day cost | runs | cost per run | prior 30 days, cost per run | readers in 30 days |
|---|---|---|---|---|---|
| clickstream_sessions | $11,800 | 720 | $16.39 | $16.10 | 3 |
| marketing_attribution | $6,900 | 30 | $230 | $228 | 0 |
| orders_daily | $4,200 | 30 | $140 | $60 | 9 |
| untagged | $3,000 | 0 | n/a | n/a | n/a |
| inventory_snapshot | $2,100 | 30 | $70 | $69 | 5 |
- $28,000
- 10.7%
Reading it for savings
Patterns worth a closer look
- No readers: It spends $6,900 a month on tables nobody queried in 30 days. Ask the owner what still depends on them before pausing the schedule.
- Cost per run jumped: Same cadence, cost per run up from $60 to $140. Something in the code changed, often a deploy that swapped an incremental load for a full rebuild.
- Cadence above need: It makes 720 hourly runs for 3 readers. If each run reprocesses the whole day so far and readers look once each morning, a daily run removes most of the repeated work.
- Untagged spend: Find who sends untagged queries first. The $3,000 here exceeds inventory_snapshot's whole month, enough to hide a pipeline that size; a working threshold, such as 5% of spend, says when to chase it.
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?
- Move orders_daily to a smaller compute cluster so each run costs less, and check the bill again next month.
- Run orders_daily every other day, which halves the monthly cost while the cause is investigated.
- Ask the 9 readers whether they still need the output, and pause the pipeline if fewer than half reply.
- Group query_history for orders_daily by task, before and after 2026-09-24, and read the deploy's diff for the task whose cost moved.
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.