OauthAccessToken icon
Request icon
If icon
Fail icon
Query icon
AIAgent icon
Anthropic icon
KestraTask icon
JSONStructuredExtraction icon
Log icon
Pause icon
McpToolTrigger icon
Webhook icon

Gate BigQuery Queries on Cost with an AI Rewrite Verified by Dry Run

Dry-run BigQuery queries, cap bytes billed, and let an AI agent rewrite over-budget SQL that a second dry run verifies before a human approves.

Categories
AIDataInfrastructure

One SELECT * on a large table can cost more than a month of scheduled jobs, and AI agents now write much of our SQL. This flow prices every query before BigQuery runs it, for agents over MCP and for services over a Webhook.

How it works

  1. An MCP client (Claude Code, Cursor) calls the bigquery_query_with_budget tool, or a service posts to the Webhook, with the SQL and the reason for it.
  2. price_original dry-runs the query through the BigQuery REST API. It is free and returns the bytes it would scan and the statement type.
  3. Anything other than a single SELECT is blocked.
  4. Within budget, run_query runs it with maximumBytesBilled set just above the estimate.
  5. Over budget, an AIAgent rewrites the query. Its only tool is the same dry run, exposed with KestraTask, so it tests each candidate against BigQuery and reads BigQuery's errors.
  6. price_rewrite dry-runs the proposal again, so the gate uses BigQuery's number, not the agent's.
  7. A Pause shows both prices and what the rewrite narrows. The reviewer runs the rewrite under a byte cap, or rejects it.

What you get

  • No query runs without a price, and none runs over the budget in variables.
  • A byte cap on every run, so a wrong estimate fails instead of billing.
  • Cheaper SQL proposed by an agent and proven by BigQuery before a person sees it.
  • Agents can query BigQuery through one governed tool, labelled system.from: mcp.

Who it's for

  • Analytics and data platform teams paying on-demand BigQuery prices.
  • Teams giving AI agents warehouse access.

Why orchestrate this with Kestra

  • The budget lives in the flow, so a caller cannot raise its own limit.
  • Pause turns approval into a step, and every decision stays in the execution history.
  • The same flow serves agents over MCP and services over a Webhook.

Prerequisites

  • A GCP service account with roles/bigquery.jobUser on the billing project.
  • Kestra encryption configured, so the OAuth token stays encrypted in outputs.

Secrets

  • GCP_SERVICE_ACCOUNT: service account JSON key.
  • ANTHROPIC_API_KEY: the rewriting agent.

Quick start

  1. Set project and budget_gib in variables and add the secrets.
  2. Execute with a small query: it prices and runs straight away.
  3. Execute with SELECT * FROM bigquery-public-data.crypto_bitcoin.transactions WHERE fee > 1000000 and a reason such as "January 2024 high-fee transactions". Read review_summary, then resume with run_rewrite.
  4. Connect your agent to the Kestra MCP server.

How to extend

  • Add a dataset allowlist or column masking before the run.
  • Use Gemini or OpenAI instead of Anthropic.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.