bq-cost-auditor
Skill0 starsChanged 3 months agoMITView on GitHub
Audit BigQuery SQL or a whole project for cost, then return a rewritten query and a projected byte reduction. Use when the user asks why a query is slow or expensive, mentions a BigQuery bill or slot contention, asks to optimize or tune SQL, asks about partitioning or clustering choices, or pastes a query and asks what is wrong with it. Also use before shipping any model that will run on a schedule.
In this repository10 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- gcp-data-engineeringSkill
- analytics-hub-publisherSkill
- bq-schema-contractSkill
- composer-dag-builderSkill
- dataform-modelerSkill
- de-pr-reviewerSkill
- pipeline-incident-triageSkill
- terraform-gcp-scaffolderSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 7 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 8
Read references/bigquery-cost-heuristics.md and references/execution-model.md before the first audit in a session.
- Very specific: line 13
If the BigQuery MCP server or an authenticated bq CLI is available, gather the evidence yourself.
- Very specific: line 22
Run the three INFORMATION_SCHEMA queries from the heuristics reference through execute_sql_readonly.
- Distinctive: line 45
Do this in order and report in this order.
- Distinctive: line 47
State the grain and the driving table: If you cannot tell what one row means, ask.
- Very specific: line 49
Find the partition column: If the query filters on a partitioned column through a function (DATE(ts), CAST, FORMAT_TIMESTAMP), that is …
- Distinctive: line 56
Rewrite the query in full: Not a diff, not a snippet.
- Very specific: line 64
Run the three queries in the heuristics reference: top spenders, repeated queries, large unpartitioned tables.
- Distinctive: line 77
No partition only when the table is under a few GB and stays that way.
- Very specific: line 108
Never call execute_sql. A cost audit is a read.
- Distinctive: line 111
Never claim a dollar amount without stating the price per TB and the edition.
- Distinctive: line 112
Never recommend a reservation without asking about the ad hoc query workload, because moving analysts onto the same slots is how pipelines …
- Distinctive: line 114
If the honest answer is that the query is already fine, say that in one line and stop.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
BigQuery cost auditor
Read references/bigquery-cost-heuristics.md and references/execution-model.md
before the first audit in a session.
Run the audit, do not narrate it
If the BigQuery MCP server or an authenticated bq CLI is available, gather the
evidence yourself. A cost audit built on real bytes is a different artifact from
one built on reading the SQL.
With the MCP server (execute_sql_readonly only, never execute_sql):
get_table_infoon every table the query touches. This gives you the partition column, the clustering columns, and the size. Do not ask the user for a schema you can read.- Run the three
INFORMATION_SCHEMAqueries from the heuristics reference throughexecute_sql_readonly. Real bytes, real run counts, real users. - Aggregate in SQL. Results are capped at 3,000 rows, so never
SELECTraw job rows and count them yourself.
With the bq CLI, add the one thing MCP cannot do, a dry run of the rewrite:
bq query --use_legacy_sql=false --dry_run < rewritten.sqlThat is the only way to get a before-and-after on a query that has never run. Without it, project from the historical bytes of the original and say that is what you did.
With neither, emit the commands and label every number as unverified.
State which of the three you used in your first line. "Read 14 days of job history through the MCP server" and "estimated from the SQL text" are different claims and the user deserves to know which one they are getting.
When the user pastes a single query
Do this in order and report in this order.
- State the grain and the driving table. If you cannot tell what one row means, ask. Everything else depends on it.
- Find the partition column. If the query filters on a partitioned column
through a function (
DATE(ts),CAST,FORMAT_TIMESTAMP), that is finding number one, because it can defeat pruning. - Count the columns actually consumed downstream. Any
SELECT *that feeds a narrower consumer is waste. - Look for the four expensive shapes: repeated CTEs, self joins used for
dedupe,
LEFT JOINplus a not-null filter, and unconstrained wildcard scans. - Rewrite the query in full. Not a diff, not a snippet. Copy-paste ready.
- Quantify. Give current bytes, projected bytes, percent saved, and the assumption. Prefer a real dry run of the rewrite. Second best is historical bytes for the original plus a stated projection method. Worst and last is an estimate from the SQL text, which you must label as such.
When the user points you at a project
Run the three queries in the heuristics reference: top spenders, repeated
queries, large unpartitioned tables. Then rank findings by
bytes saved x runs per month, not by how bad the SQL looks. A mildly sloppy
query that runs every ten minutes beats a horrifying one that runs quarterly.
Partitioning and clustering decisions
Pick the partition column by how people filter, not by what looks like a date.
- Event date column when queries ask about when the thing happened.
- Ingestion time when queries ask about when we loaded it and the event date is unreliable.
- Integer range when the natural filter is an id bucket, not a date.
- No partition only when the table is under a few GB and stays that way.
Cluster on up to four columns, most selective first, and only on columns that
appear in WHERE, JOIN, or GROUP BY. Clustering a column nobody filters on
costs write time and saves nothing.
Recommend require_partition_filter = true on any table over 100 GB and say
plainly that it will break existing unfiltered queries, which is the point.
Output format
FINDING 1: <one line, the problem>
Impact: <bytes or percent, with the assumption>
Cause: <one sentence>
Fix: <what changes>
FINDING 2: ...
REWRITTEN QUERY
<full SQL>
PROJECTED
Before: X TB per run, N runs per month
After: Y TB per run
Saved: Z percent
Assumes: <the assumption>Rules
- Never call
execute_sql. A cost audit is a read. If you find yourself wanting to materialize a test table to prove a rewrite is cheaper, emit the DDL and let the user run it. - Never claim a dollar amount without stating the price per TB and the edition.
- Never recommend a reservation without asking about the ad hoc query workload, because moving analysts onto the same slots is how pipelines start missing SLAs.
- If the honest answer is that the query is already fine, say that in one line and stop. A padded audit trains people to ignore audits.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

