Skip to content

Connect BigQuery to AI Agents: Automate Jobs and Row Access

Uday Gajavalli Uday Gajavalli 9 min read AI & Agents
TrutoFor teams building AI agents

Give your AI agent BigQuery tools.

Connect BigQuery to AI agents framework-agnostically with Truto. Discover how to fetch tools via API, manage async jobs safely, handle BigQuery's API quirks, and implement resilient agent polling workflows.

In this guide

  1. 01Initialize the Agent and Tool Manager
  2. 02Fetch BigQuery Tools via Truto
  3. 03Bind Tools to the LLM Framework
  4. 04Implement Rate Limit Handling
  5. 05Execute the Agent Workflow

The guide

Learn how to securely connect BigQuery to AI agents using Truto's /tools endpoint. A complete guide on automating async jobs, running queries, and managing Row-Level Security.

Connect BigQuery to AI Agents: Automate Jobs and Row Access

You want to connect Google BigQuery to an AI agent so your system can autonomously execute SQL queries, manage asynchronous data load jobs, audit IAM policies, and enforce row-level security. Here is exactly how to do it using Truto's /tools endpoint and SDK, bypassing the need to build and maintain a custom BigQuery API integration from scratch.

Giving a Large Language Model (LLM) read and write access to a petabyte-scale data warehouse is an engineering challenge with severe cost and security implications. If your team uses ChatGPT, check out our guide on connecting BigQuery to ChatGPT, or if you are building on Anthropic's models, read our guide on connecting BigQuery to Claude. For developers building custom autonomous workflows, you need a programmatic way to fetch these tools and bind them to your agent framework.

This guide breaks down exactly how to fetch AI-ready tools for BigQuery, bind them natively to an LLM using frameworks like LangChain, LangGraph, CrewAI, or the Vercel AI SDK, and execute complex data warehouse administration tasks. For a deeper look at the architecture behind this approach, refer to our research on architecting AI agents and the SaaS integration bottleneck.

The Engineering Reality of the BigQuery API

Giving an AI agent access to external APIs sounds simple in a Jupyter notebook prototype. You write a fetch request, wrap it in a @tool decorator, and pass it to the LLM. In production against a massive infrastructure component like BigQuery, this naive approach collapses.

The BigQuery API introduces specific integration challenges that break standard REST assumptions. If you hardcode these interactions into your agent, you will spend your sprints writing defensive integration code instead of improving your model's reasoning capabilities.

The Asynchronous Job Polling Trap

BigQuery is designed to process terabytes of data. Standard CRUD operations do not apply to data analysis. When an agent wants to load data, export tables, or run a complex query, it cannot expect a synchronous HTTP 200 OK with the final result.

Instead, almost all heavy operations in BigQuery are handled asynchronously via the Jobs API. If your agent wants to run an INSERT statement, it must POST to /projects/{projectId}/jobs. The API immediately returns a jobReference.jobId. The agent must then enter a polling loop, repeatedly calling the jobs.get endpoint until the job's status.state is DONE. Only then can the agent retrieve the query results or confirm the data load. Forcing an LLM to manage this exact polling loop, handle timeout backoffs, and track job IDs across context windows is a massive source of hallucination.

The Cryptic Table Data Format

When an agent finally manages to read rows from a table using tables.getData, BigQuery does not return a clean array of flat JSON objects like {"id": 1, "name": "Alice"}.

To optimize data transfer, BigQuery returns rows in a tightly packed schema format: { "f": [ { "v": "1" }, { "v": "Alice" } ] }. The column names are stripped out and provided separately in the table schema metadata. If you give raw BigQuery endpoints to an LLM, the model must simultaneously hold the table schema in context, map the index of every v value to the corresponding column name, and reconstruct the row. LLMs are notoriously bad at index-based array mapping, leading to severely hallucinated data extraction.

Dual-Layer Identity and IAM Granularity

BigQuery permissions are fiercely granular. Executing a query requires the bigquery.jobs.create permission at the project level, but reading the underlying data requires bigquery.tables.getData on the specific dataset or table. If an agent tries to execute a simple SELECT * FROM sales, it might succeed in creating the job but fail during execution because it lacks data access.

Furthermore, BigQuery sandboxes (projects without active billing) outright reject DML statements (INSERT, UPDATE, DELETE) with a billingNotEnabled error. The agent must understand the difference between a billing-enabled project and a sandbox, and gracefully handle complex Google Cloud IAM policy arrays when administering row-level access.

Why a Unified Tool Layer Matters for Agent Safety

Direct API tools - mapping one tool directly to one raw Google Cloud API endpoint - push all of BigQuery's quirks into the LLM's context window. The model has to remember the f/v mapping format, understand the difference between queries.create and jobs.create, and manually track HTTP pagination cursors.

A unified tool layer provided by Truto collapses these complexities. Your agent sees stable, declarative function names with strict JSON schemas. That gives you concrete safety wins:

  1. Smaller attack surface for hallucination. The LLM chooses from validated function names. Invalid arguments are rejected before they hit BigQuery, so a broken tool call fails fast.
  2. Deterministic error boundaries. Truto handles the raw API mapping but crucially does not hide infrastructure realities like rate limits. When BigQuery throws an HTTP 429 Too Many Requests, Truto passes that error directly to your agent, alongside IETF-standardized headers (ratelimit-limit, ratelimit-remaining, ratelimit-reset). Your agent framework remains fully in control of the retry and backoff logic, ensuring you never accidentally DDoS your own data warehouse.

BigQuery Hero Tools for AI Agents

To effectively administer BigQuery, your agent needs tools that handle data execution, job management, and security governance. Here are the core tools Truto exposes to your LLM frameworks.

Create a BigQuery Job

create_a_big_query_job

This tool initiates a new asynchronous job. This is the primary method for executing heavy loads, extracts, or complex long-running queries. The agent submits the job configuration as JSON, and the tool returns a job ID that can be tracked.

"Execute an asynchronous load job to import the CSV file from gs://data-lake/q3_sales.csv into the 'financials' dataset, appending to the 'raw_transactions' table."

Get Single BigQuery Job by ID

get_single_big_query_job_by_id

Once a job is started, the agent uses this tool to poll the job's status. It returns the current state, statistics about bytes processed, and any errors encountered during execution.

"Check the status of BigQuery job 'bquxjob_39a8f2' to see if the data export has completed successfully or if it failed due to schema mismatches."

Create a BigQuery Query

create_a_big_query_query

For smaller, fast-executing SQL statements, this tool runs a query synchronously. It waits up to a specified timeout for results. If the query finishes within the timeout, it returns the data immediately. If not, it returns a job ID to fall back to the polling pattern.

"Run a synchronous query to find the top 50 active users by login count in the 'telemetry.events' table and return the results."

List All BigQuery Table Rows

list_all_big_query_table_rows

This tool allows the agent to read the contents of a table directly without executing a SQL job. It is ideal for sampling data, inspecting raw contents, and understanding table structures before writing complex queries.

"Fetch the first 100 rows from the 'hr.employee_directory' table so I can inspect the current schema and data formats."

Create a BigQuery Row Access Policy

create_a_big_query_row_access_policy

Data governance is critical in BigQuery. This tool allows the agent to enforce Row-Level Security (RLS) by creating a policy that restricts which users or groups can see specific rows based on a SQL filter.

"Create a row access policy on the 'finance.q3_revenue' table named 'eu_sales_only' that restricts access so that only the group 'eu-managers@company.com' can see rows where region = 'EU'."

BigQuery Table IAM Policies Bulk Update

big_query_table_iam_policies_bulk_update

This tool allows the agent to manage access control at the table level. It replaces the existing IAM policy on a resource, allowing the agent to grant or revoke roles like bigquery.dataViewer or bigquery.dataEditor.

"Update the IAM policy on the 'marketing.campaigns' table to grant the 'bigquery.dataViewer' role to the service account 'sa-reporting@my-project.iam.gserviceaccount.com'."

These tools represent just a fraction of the available operations. For the complete inventory, including dataset management, ML model administration, and routine deployments, view the full BigQuery integration page.

Workflows in Action

When you bind these tools to an autonomous agent, you move beyond simple chat interfaces into full infrastructure automation. Here are two real-world workflows demonstrating how an agent navigates the BigQuery API.

Scenario 1: Autonomous Data Governance and Security Enforcement

An IT security administrator needs to ensure that a newly created sensitive table is locked down with strict row-level security before it is queried by analysts.

"Audit the 'hr_data' dataset. If the 'salary_bands' table exists, apply a row access policy named 'restrict_exec_only' that only allows users in 'exec-team@company.com' to view rows where the 'band_level' is greater than 5."

Agent Execution Steps:

  1. The agent calls list_all_big_query_tables with the dataset ID hr_data to verify the salary_bands table exists.
  2. The agent calls list_all_big_query_row_access_policies to check if a policy already exists on the table.
  3. Finding no existing policy, the agent calls create_a_big_query_row_access_policy with the filter band_level > 5 and the grantee group:exec-team@company.com.
  4. The agent returns a confirmation message detailing the applied policy.

Outcome: The agent autonomously navigates the BigQuery resource hierarchy, checks existing state, and enforces a complex security control without manual Google Cloud Console intervention.

Scenario 2: Autonomous ELT Orchestration

A data engineer tasks the agent with orchestrating a nightly data load and verifying the results.

"Trigger a load job to pull data from gs://inbound-bucket/daily_events.json into the 'events_raw' table. Wait for the job to finish, then run a query to count the total number of inserted rows."

Agent Execution Steps:

  1. The agent calls create_a_big_query_job passing the JSON load configuration and Cloud Storage URI.
  2. The API returns a jobId. The agent enters a controlled loop, calling get_single_big_query_job_by_id every few seconds.
  3. Once the job status returns DONE, the agent inspects the job statistics to verify successful completion.
  4. The agent calls create_a_big_query_query with the SQL SELECT COUNT(*) FROM dataset.events_raw.
  5. The agent parses the returned row count and reports the final number back to the user.

Outcome: The agent successfully orchestrates an asynchronous infrastructure workflow, managing the state transitions and follow-up validation autonomously.

Building Multi-Step Workflows

To build these autonomous data warehouse workflows, you need to fetch the proxy APIs from Truto and bind them to your LLM. Because Truto standardizes the tools via the /tools endpoint, this approach is entirely framework-agnostic.

Here is how you architect this using TypeScript, the Truto Langchain.js SDK, and standard LangGraph loops.

flowchart TD
    A["Initialize Agent<br>(LangChain)"] -->|"Fetch via SDK"| B["GET /integrated-account/{id}/tools"]
    B -->|"Return Tool Schemas"| C["Agent.bindTools()"]
    C --> D{"Agent Reasoning Loop"}
    D -->|"Invoke Tool"| E["Truto Proxy API"]
    E -->|"HTTP 429 Rate Limit"| F["Agent Interceptor catches 429"]
    F -->|"Read ratelimit-reset header"| G["Agent Sleep / Backoff"]
    G --> D
    E -->|"HTTP 200 OK"| H["Agent parses JSON"]
    H --> D

Fetching Tools and Handling Rate Limits

A critical architectural requirement when integrating with BigQuery is handling API quotas. Truto intentionally does not absorb rate limits or silently retry requests. If you exceed BigQuery's concurrent API request quotas, Truto passes the HTTP 429 error directly to your agent.

Truto normalizes the upstream provider's rate limit information into standardized IETF headers:

  • ratelimit-limit: The total requests allowed in the current window.
  • ratelimit-remaining: The number of requests left.
  • ratelimit-reset: The time at which the rate limit window resets.

Your agent framework is responsible for catching this error, reading the headers, and applying backoff logic.

import { ChatOpenAI } from "@langchain/openai";
import { TrutoToolManager } from "truto-langchainjs-toolset";
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
 
async function runBigQueryAgent() {
  // 1. Initialize the LLM
  const llm = new ChatOpenAI({ 
    modelName: "gpt-4o",
    temperature: 0 
  });
 
  // 2. Fetch BigQuery tools for the specific connected account
  const toolManager = new TrutoToolManager({
    trutoApiKey: process.env.TRUTO_API_KEY,
  });
 
  const integratedAccountId = "your_bigquery_integrated_account_id";
  const tools = await toolManager.getTools(integratedAccountId);
 
  // 3. Bind tools to the LLM
  const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are an expert BigQuery administrator. You can run queries, manage jobs, and configure IAM policies. If a job is asynchronous, you must poll its status until it completes."],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"]
  ]);
 
  const agent = createToolCallingAgent({
    llm,
    tools,
    prompt,
  });
 
  const agentExecutor = new AgentExecutor({
    agent,
    tools,
    maxIterations: 15,
    // Implement standard retry logic in your executor or HTTP client 
    // to catch HTTP 429s and respect the 'ratelimit-reset' header.
  });
 
  // 4. Execute the workflow
  const result = await agentExecutor.invoke({
    input: "Trigger a load job to import gs://bucket/data.csv into 'finance.q4_data'. Wait for it to finish, then tell me the job statistics."
  });
 
  console.log(result.output);
}
 
runBigQueryAgent();

When building production agents, you must implement a robust HTTP interceptor or custom tool wrapper that intercepts HTTP 429 responses. By parsing the ratelimit-reset header returned by Truto, you can pause the agent's execution thread for the exact required duration, preventing infinite failure loops and ensuring your BigQuery quotas are respected.

Building AI agents that interact with enterprise data warehouses requires moving past prototype code. Direct integrations force your LLM to become an expert in Google Cloud's idiosyncratic asynchronous polling, nested JSON structures, and complex IAM configurations. By leveraging a unified tool layer, you abstract the architectural boilerplate, limit the attack surface for hallucinations, and maintain total control over critical execution logic like rate limiting and retries. Focus your engineering cycles on improving your agent's reasoning capabilities, not maintaining fragile integration code.

Two ways to put BigQuery to work

Truto

For product teams

Give your agent BigQuery tools

Your customers connect their own BigQuery accounts. Your product gets one API and MCP tools for BigQuery, through Truto.

FAQ

How do AI agents handle BigQuery's asynchronous jobs?
Agents must use a polling pattern. By calling `create_a_big_query_job` to initiate the task, the agent receives a job ID. It then enters a loop, calling `get_single_big_query_job_by_id` until the status returns as DONE.
Does Truto automatically retry when BigQuery hits a rate limit?
No. Truto intentionally does not absorb rate limits. When BigQuery returns an HTTP 429, Truto passes it to your agent with standardized `ratelimit-limit`, `ratelimit-remaining`, and `ratelimit-reset` headers, leaving backoff logic in your control.
Can I use Truto BigQuery tools with any agent framework?
Yes. Truto's `/tools` endpoint serves standard JSON tool schemas that can be ingested by LangChain, LangGraph, CrewAI, Vercel AI SDK, or any framework that supports LLM function calling.
BigQuery BigQueryAI agent tools Get a sandbox

More from our Blog