Multi-level batching architecture
AI.AGG abstracts the immense engineering complexity of chunking and parallel execution. When you execute an AI.AGG query, the BigQuery engine automatically orchestrates a sequence natively within the database:
Row-level validation: The engine helps verify no single row exceeds the maximum context window (dropping or returning an error on payloads >10 MiB).
Context-safe batching: The engine dynamically groups input rows into smaller batches that safely fit within the token limits of your chosen Gemini endpoint.
Parallel inference: Your natural-language SQL prompt is applied to each batch in parallel, generating intermediate semantic summaries via Vertex AI.
Recursive synthesis: Finally, the system batches these intermediate outputs together and passes them back to the LLM with the exact same instruction. This reduction repeats recursively until a single, final aggregated string is produced per SQL GROUP BY clause.
Because AI.AGG requires deep reasoning to synthesize concepts across disparate batches, it relies entirely on full-weight LLMs for every stage of its pipeline — differentiating it from row-level functions like AI.CLASSIFY, which can leverage distilled CPU proxy models.
Built for the multimodal enterprise
AI.AGG natively supports both text and multimodal inputs. By leveraging BigQuery Object Tables and the OBJ.GET_ACCESS_URL function, analytics teams can aggregate visual insights across thousands of images stored directly in Cloud Storage with no data movement or transcoding required.
Data engineering pro-tip: The NULL exclusivity rule
When evaluating multiple columns, users often pass a STRUCT into the function. However, if even a single field within that composite structure evaluates to NULL, the entire row is treated as NULL and silently discarded. Always explicitly wrap nullable fields with IFNULL() (e.g., IFNULL(description, ‘No data provided’)) to help verify your data safely enters the pipeline.
Transformative enterprise use cases
The true analytical power of AI.AGG emerges when combined with standard SQL GROUP BY operations.
1. Diagnostic analysis of high-volume system logs
System logs generated by globally distributed microservices often hide subtle degradations (like memory thrashing or systemic clock drift) that never throw explicit fatal errors. Using AI.AGG, DevOps teams can group millions of logs by system component and generate targeted, human-readable diagnostic summaries.
2. Building the “agentic data cloud”
AI.AGG isn’t just an analytical reporting tool; it’s a dynamic orchestration engine. Imagine acquiring a massive repository of legacy support tickets with no clear categorization. You can deploy AI.AGG to read the raw text, synthesize a definitive list of categories, and output them as a strict JSON array.
By unpacking this array via JSON_VALUE_ARRAY and feeding those dynamic labels directly into row-level classifiers like AI.CLASSIFY, you can orchestrate an end-to-end, autonomous data labeling pipeline natively in SQL — reducing the need for external Python scripts.
Prompt engineering is data engineering
When integrating generative models into highly deterministic SQL pipelines, standard conversational prompts will often fail. Prompting for programmatic SQL outputs is a strict engineering discipline:
Explicit output contracts: Treat the prompt as an API contract. Append commands like: “Return exactly one valid JSON array of strings. Do not include markdown code blocks or conversational text.”
Contextual delimiters: Use string concatenation with explicit labels (e.g., ‘- Description: ‘) to anchor the LLM and prevent it from conflating fields across dense batches.
Failure state design: Explicitly instruct the model to return strict null values for missing information rather than guessing, preventing schema hallucinations downstream.
Few-shot anchoring: Provide a literal example of your expected structure within the prompt to heavily ground the model’s attention mechanism.
Governance, cost management, and guardrails
Transitioning semantic workloads to production requires operational oversight. Because of the recursive batching architecture, the total tokens sent to Gemini will be greater than the token count of your source data. To mitigate costs, mandate strict WHERE and LIMIT pre-filtering.
Furthermore, for workflows cross-referencing massive, static datasets, BigQuery supports Gemini Context Caching, which can reduce input token costs and execution latency. Use the AI.COUNT_TOKENS utility to estimate query costs prior to execution.
Administrators can operate knowing AI.AGG works strictly within BigQuery’s governance framework, supporting VPC Service Controls (VPC-SC) to prevent data exfiltration. Furthermore, if a transient error occurs during a batch process, the function degrades gracefully — skipping the rejected rows rather than crashing the entire query.
Preview guardrails to keep in mind:
The final output string per SQL group is strictly capped at 10,000 tokens per group. Ask the model for concise output
Individual payload rows must not exceed 10 MiB.
When processing multimodal arrays, limit image assets to fewer than 10 images per row to avoid silent skipping.
For optimal stability, we recommend processing a maximum of 20 million rows and outputting a maximum of 1,000 distinct SQL groups per query.
Putting AI.AGG into practice
To help you get started, here are examples of how AI.AGG is used to synthesize insights alongside traditional SQL metrics.
Macro-level topic discovery
Summarize what specific top authors talk about, turning thousands of their comments into a concise narrative