Data & Analytics 8 min read

Taking the BigQuery Data Engineering Agent to the Next Level: Planning, Optimization and Troubleshooting

How Plan-and-Act mode, multi-agent pipeline optimization, and closed-loop Dataform troubleshooting transform autonomous data engineering.

Read on Medium ↗
Lucia Subatin Cloud Architect & Technical Lead

If you’ve experimented with early generations of generative AI in data engineering, you know the familiar pattern: you ask for a SQL snippet, get back a syntactically plausible query, copy-paste it into your editor, hit run, and immediately hit a syntax or schema error. You paste the error back, iterate three or four times, and finally get working code.

While that text-to-SQL approach is convenient for simple ad-hoc lookups, it fails when building resilient, production-grade enterprise data pipelines.

With the latest generation of the BigQuery Data Engineering Agent inside BigQuery Studio and Dataform, we are moving far beyond single-turn text-to-SQL into an autonomous agentic collaborator. In this deep dive, we’ll explore three major advancements that take pipeline engineering to the next level: Planning Mode (“Plan-and-Act”), Intelligent Multi-Agent Optimization, and Closed-Loop Dataform Troubleshooting.


1. Planning Mode: The Power of “Plan and Act”

When data engineers build multi-table transformation layers—for example, joining clickstream telemetry events with customer CRM profiles to compute a Recency, Frequency, Monetary (RFM) segmentation model—they don’t just write a single monolithic query. They reason through data grains, deduplication strategies, intermediate staging steps, and downstream mart schemas.

Previously, requesting a complex transformation all at once frequently overwhelmed the LLM context, resulting in missing join conditions or unoptimized subqueries that required multiple manual prompt corrections.

The “Plan and Act” Paradigm Shift

The BigQuery Data Engineering Agent introduces a dedicated Planning Mode. Instead of blindly generating hundred-line SQL blocks immediately, the agent first decomposes complex requests into an observable, structured architectural plan:

  1. Schema Introspection: The agent queries metadata across the candidate datasets, validating column types, nullability, and primary key candidates.
  2. Step-by-Step Blueprint: It surfaces an explicit reasoning plan outlining staging tables, incremental filtering logic, and target mart structures.
  3. Execution & Confirmation: Once the plan is confirmed (or refined by the practitioner), the agent executes the generation step-by-step.
User: "Create a plan to build an RFM segmentation pipeline from raw e-commerce orders and user accounts."

Agent: 
[Architectural Plan Generated]
Step 1: Ingest raw_orders, filter cancellations, and deduplicate on order_id.
Step 2: Aggregate by customer_id over a 365-day rolling window to compute Recency (days since last order), Frequency (distinct order count), and Monetary (sum of net spend).
Step 3: Calculate quintile score buckets (1-5) using BigQuery NTILE() window functions.
Step 4: Output curated table clustered by rfm_score and partitioned by calculation_date.

Field Benchmark: In practice, utilizing Planning Mode reduces manual back-and-forth prompt correction turns by approximately 30%, because any architectural misunderstanding or grain mismatch is caught and corrected before a single line of SQL or Dataform JavaScript is written.

You can explicitly invoke this behavior at any time in the BigQuery Studio agent panel by prefixing your instruction with: "Create a plan to [build / refactor / migrate]..."


2. Intelligent Multi-Agent Pipeline Optimization

Generating SQL that simply runs is only half the battle. In an enterprise lakehouse processing petabyte-scale datasets, an unoptimized query can consume tens of thousands of BigQuery slots and run up cloud infrastructure costs unnecessarily.

The BigQuery Data Engineering Agent utilizes an internal specialized multi-sub-agent architecture where a primary orchestrator coordinates with domain-specific optimization agents (Partitioning Evaluator, Clustering Advisor, and Schema Modeler).

+--------------------------------------------------------------+
|             Primary Data Engineering Orchestrator             |
+------------------------------+-------------------------------+
                               |
         +---------------------+---------------------+
         |                                           |
         v                                           v
+-----------------------------+     +-------------------------------+
|    Partitioning Advisor     |     |      Data Modeling Agent      |
|  - Date/Timestamp filters   |     |  - STRUCT / ARRAY compaction  |
|  - Ingestion-time balance   |     |  - Incremental vs Table view  |
+-----------------------------+     +-------------------------------+

Automatic Architectural Best Practices

When synthesizing or reviewing pipeline definitions, the agent automatically evaluates:

  • Smart Partitioning: Recommends date/timestamp partitioning based on temporal filter patterns in your historical queries, avoiding full table scans.
  • Optimal Clustering Keys: Selects up to 4 high-cardinality columns (e.g., tenant_id, customer_status, product_category) that are frequently co-filtered or joined.
  • Incremental vs. Materialized Tables: Suggests converting standard views into Dataform incremental tables when upstream volume exceeds millions of daily records.
  • Denormalization via Nested & Repeated Records (STRUCT / ARRAY): Replaces expensive relational star-schema joins with native BigQuery nested arrays for one-to-many relationship structures.
-- Generated and optimized by the Agent for BigQuery Lakehouse performance
CREATE OR REPLACE TABLE `analytics_production.mart_customer_rfm`
PARTITION BY DATE(calculation_date)
CLUSTER BY rfm_segment, primary_region AS
SELECT
  customer_id,
  CURRENT_DATE() AS calculation_date,
  NTILE(5) OVER (ORDER BY days_since_last_order DESC) AS r_score,
  NTILE(5) OVER (ORDER BY order_count_365d ASC) AS f_score,
  NTILE(5) OVER (ORDER BY total_spend_usd ASC) AS m_score,
  ARRAY_AGG(STRUCT(order_id, order_timestamp, order_total) 
            ORDER BY order_timestamp DESC LIMIT 5) AS recent_orders
FROM
  `analytics_staging.stg_customer_metrics`
GROUP BY
  customer_id, days_since_last_order, order_count_365d, total_spend_usd;

3. Automated Troubleshooting & Closed-Loop Remediation in Dataform

Anyone who has developed pipelines in Dataform knows that pipeline compilation errors can be notoriously frustrating: missing upstream dependency declarations, circular DAG references, schema type drift between source and target, or syntax errors hidden inside SQLX code blocks.

Traditionally, when a Dataform compilation or dry-run fails, the developer must read the stack trace, locate the offending .sqlx file, look up the schema definition, apply an edit, and re-trigger compilation.

Closing the Remediation Loop

The BigQuery Data Engineering Agent integrates directly into the Dataform workspace environment to close this loop automatically:

  1. Context-Aware Error Ingestion: When compilation or dry-run execution fails, the agent automatically ingests the error code, the offending SQLX file, and the resolved dependency DAG.
  2. Diagnostic Root-Cause Report: It generates an immediate, human-readable breakdown explaining exactly why the failure occurred (e.g., “Column transaction_timestamp in source table events_v2 changed from DATETIME to TIMESTAMP, causing a type mismatch in join condition on line 42”).
  3. Automated Workspace Patching: By prompting:
    "Apply these fixes for me"
    the agent directly patches the SQLX source file in your Dataform development workspace, updates the table assertions, and triggers a re-compilation to verify that the DAG is green!
-- Before: Broken compilation due to type mismatch
config {
  type: "incremental",
  schema: "reporting",
  name: "daily_revenue_summary"
}

SELECT
  DATE(transaction_timestamp) AS event_date, -- Failed: incompatible type
  SUM(amount) AS total_revenue
FROM ${ref("stg_raw_transactions")}
GROUP BY 1

-- After: Agent automatically applies cast fix and validates schema
config {
  type: "incremental",
  schema: "reporting",
  name: "daily_revenue_summary",
  bigquery: {
    partitionBy: "event_date"
  }
}

SELECT
  DATE(TIMESTAMP(transaction_timestamp)) AS event_date,
  SUM(amount) AS total_revenue
FROM ${ref("stg_raw_transactions")}
${when(incremental(), `WHERE DATE(TIMESTAMP(transaction_timestamp)) > (SELECT MAX(event_date) FROM ${self()})`)}
GROUP BY 1

Summary: Autonomous Data Engineering in Practice

The evolution from reactive prompt-completion to proactive, agentic planning and closed-loop troubleshooting represents a foundational step forward for enterprise data platforms on Google Cloud.

By offloading boilerplate DAG scaffolding, schema validation, and partition optimization to the BigQuery Data Engineering Agent, data teams can shift their focus from firefighting pipeline breakages to designing high-impact business semantics and intelligent data products.

Give Planning Mode and Dataform automated remediation a spin in BigQuery Studio, and let me know your thoughts and field experiences!

Authored in Markdown

This technical article is maintained as a Markdown source file in src/content/blog/taking-the-bigquery-data-engineering-agent-to-the-next-level.md and statically compiled via Astro 5.

About the Author — Lucia Subatin

Lucia is a Cloud Architect and Developer Advocate specializing in BigQuery, autonomous data engineering agents, distributed cloud architectures, and generative AI platforms at Google Cloud.