Overview
The MABQ BigQuery Agent uses Google ADK Tools to interact with BigQuery. Tools enable the agent to execute queries, validate SQL syntax, and retrieve schema information.Tools are functions that the LLM can invoke to perform actions beyond text generation. In this agent, the
BigQueryToolset provides query execution capabilities.BigQueryToolset
TheBigQueryToolset is a collection of tools for interacting with BigQuery datasets.
Initialization
Configured inagent.py:32-35:
Configuration Components
Included Tools
TheBigQueryToolset provides multiple tools to the agent:
| Tool | Purpose | Usage |
|---|---|---|
| Query Executor | Runs SQL queries and returns results | Primary tool for data retrieval |
| Schema Inspector | Lists tables and column definitions | Validates table/column existence |
| Query Validator | Checks SQL syntax without execution | Pre-execution validation |
| Metadata Reader | Retrieves dataset and table metadata | Understands data structure |
Tool Selection Process
Tool Selection Process
The agent automatically selects which tool to use based on the user’s request:The LLM determines tool selection based on the instruction prompt and user intent.
WriteMode.BLOCKED Security Setting
The most critical security feature of the agent is theWriteMode.BLOCKED configuration.
Configuration
Defined inagent.py:24-26:
How WriteMode.BLOCKED Works
Write Detection
Identifies prohibited keywords:
INSERT,UPDATE,DELETECREATE,ALTER,DROPTRUNCATE,MERGEGRANT,REVOKE
WriteMode Comparison
Error Handling with WriteMode.BLOCKED
When a write operation is attempted:Tool Execution in the Agent
The agent uses tools through a structured execution flow.Execution Flow
Mandatory Tool Usage
The instruction prompt requires tool usage for data queries:The agent cannot return SQL without first validating it through the BigQueryToolset. This ensures all generated queries are syntactically correct and executable.
Tool Usage Examples
Simple Query
Simple Query
User: “Show all assets”Agent Process:
- Generate SQL:
SELECT * FROM \datawarehouse-des.STG_ACTIVOS.assets“ - Execute via toolset to validate
- Receive confirmation query works
- Return SQL to user
Complex Aggregation
Complex Aggregation
User: “What is the average maintenance cost by asset type?”Agent Process:
- Generate SQL:
- Execute via toolset
- Verify query returns expected schema
- Return SQL to user
Write Attempt (Blocked)
Write Attempt (Blocked)
User: “Delete all old records”Agent Process:
- Generate SQL:
DELETE FROM \datawarehouse-des.STG_ACTIVOS.assets` WHERE date < ‘2020-01-01’` - Execute via toolset
- Tool raises WriteOperationBlockedError
- Agent receives error feedback
- Instruction prompt triggers rejection message
Integration with Instruction Prompt
The instruction prompt explicitly references and guides tool usage.Tool Reference
Fromagent.py:50-52:
Operational Rules
The prompt defines how tools should be used:Prompt-Tool Synergy
Tool Configuration Best Practices
Security Practices
Credential Management
Monitoring Tool Usage
Track tool execution for debugging and optimization:Enable ADK Logging
Enable ADK Logging
Query Metrics
Query Metrics
Monitor tool performance using BigQuery job history:
Adding Custom Tools
While the agent currently uses onlyBigQueryToolset, you can add additional tools.
Example: Adding a Custom Validation Tool
Custom SQL Validator
Custom SQL Validator
When adding custom tools, update the instruction prompt to inform the agent about the new capabilities.
Troubleshooting Tool Execution
Tool Not Being Used
Tool Not Being Used
Symptom: Agent returns SQL without executing itCauses:
- Instruction prompt doesn’t mandate tool usage
- Tool execution failed silently
- Agent doesn’t recognize tool availability
WriteMode Errors
WriteMode Errors
Symptom: Tool raises error even for SELECT queriesCause: Query might contain write operations in subqueries or CTEsSolution:
Credential Issues
Credential Issues
Symptom:
google.auth.exceptions.DefaultCredentialsErrorSolutions:Advanced Configuration
Query Timeout Settings
Configure BigQuery Job Timeout
Configure BigQuery Job Timeout
Result Size Limits
Control Query Result Size
Control Query Result Size
Summary
The MABQ BigQuery Agent’s tool configuration provides:- Secure Query Execution:
WriteMode.BLOCKEDprevents data modification - Automatic Validation: Mandatory tool usage ensures SQL correctness
- Credential Management: Automatic authentication via
google.auth.default() - Schema Discovery: Built-in tools for exploring database structure
- Error Handling: Graceful failure with security-aware messaging
The combination of
BigQueryToolset, WriteMode.BLOCKED, and security-focused instruction prompts creates a robust, production-ready analytics agent.