<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anurag Jain</title>
    <description>The latest articles on DEV Community by Anurag Jain (@anurag_jain_1c2e23aeee455).</description>
    <link>https://dev.to/anurag_jain_1c2e23aeee455</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2628004%2Ffd44867c-77fe-4c06-a7c3-36f0d735bc88.png</url>
      <title>DEV Community: Anurag Jain</title>
      <link>https://dev.to/anurag_jain_1c2e23aeee455</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anurag_jain_1c2e23aeee455"/>
    <language>en</language>
    <item>
      <title>3 guardrails that stop an AI agent from deleting your BigQuery data (or running up your bill)</title>
      <dc:creator>Anurag Jain</dc:creator>
      <pubDate>Tue, 10 Nov 2026 12:44:14 +0000</pubDate>
      <link>https://dev.to/anurag_jain_1c2e23aeee455/3-guardrails-that-stop-an-ai-agent-from-deleting-your-bigquery-data-or-running-up-your-bill-1ico</link>
      <guid>https://dev.to/anurag_jain_1c2e23aeee455/3-guardrails-that-stop-an-ai-agent-from-deleting-your-bigquery-data-or-running-up-your-bill-1ico</guid>
      <description>&lt;p&gt;Giving an AI agent access to your database feels a bit like handing your car keys to a very confident teenager. It will &lt;em&gt;probably&lt;/em&gt; be fine. But "probably" is not a plan.&lt;/p&gt;

&lt;p&gt;I'm a data architect and I work on Google Cloud every day. Lately a lot of people have been building "ask your data" agents: you type a question in plain English, a model like Gemini writes the SQL, runs it on BigQuery, and tells you the answer. It's a genuinely useful thing to build.&lt;/p&gt;

&lt;p&gt;But the demo version usually has three holes in it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Nothing stops the agent from running a query that &lt;strong&gt;changes or deletes data&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Nothing stops one query from &lt;strong&gt;scanning a huge table and costing real money&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Nothing stops the agent from &lt;strong&gt;looping&lt;/strong&gt;, calling the model and the database again and again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's how to close all three. None of it is hard, and the whole thing takes about 20 minutes.&lt;/p&gt;

&lt;p&gt;The rule of thumb behind all of it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Ask "what's the worst thing this agent could do?" Then make that thing &lt;em&gt;impossible&lt;/em&gt;, not just discouraged.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Guardrail 1: Make deleting data impossible, not just "against the rules"
&lt;/h2&gt;

&lt;p&gt;The first thing most people do is add a check in their tool code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startswith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"select"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Error: only SELECT queries are allowed."&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a good start. But be honest about what it is: &lt;strong&gt;a speed bump, not a wall.&lt;/strong&gt; Look at this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It starts with &lt;code&gt;SELECT&lt;/code&gt;, so it passes the check. And BigQuery can run multi-statement scripts. A model that has been confused (or tricked by text hidden in your own data) could send exactly that.&lt;/p&gt;

&lt;p&gt;You could keep adding rules to the string check, but you'd be playing whack-a-mole. The real fix is to take the permission away entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run the agent as a service account that is only allowed to read.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Create an identity just for the agent&lt;/span&gt;
gcloud iam service-accounts create ask-your-data-sa \
  --display-name=&lt;span class="s2"&gt;"Ask Your Data agent"&lt;/span&gt;

&lt;span class="c"&gt;# 2. Let it run query jobs (it needs this to run any query at all)&lt;/span&gt;
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member=&lt;span class="s2"&gt;"serviceAccount:ask-your-data-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com"&lt;/span&gt; \
  --role=&lt;span class="s2"&gt;"roles/bigquery.jobUser"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in BigQuery, give it read access to &lt;strong&gt;only the one dataset it needs&lt;/strong&gt;, not your whole project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="sb"&gt;`roles/bigquery.dataViewer`&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="sb"&gt;`YOUR_PROJECT_ID.shop`&lt;/span&gt;
&lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="s2"&gt;"serviceAccount:ask-your-data-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. Two roles, nothing more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BigQuery Job User&lt;/strong&gt; lets it &lt;em&gt;run&lt;/em&gt; queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BigQuery Data Viewer&lt;/strong&gt; on one dataset lets it &lt;em&gt;read&lt;/em&gt; those tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now if the agent ever sends a &lt;code&gt;DROP&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt;, BigQuery itself refuses with an access-denied error. The permission simply doesn't exist. Your string check is still there to give the model a friendly error message, but it's no longer the thing keeping your data safe.&lt;/p&gt;

&lt;p&gt;When you deploy to Cloud Run, attach this service account to the service (&lt;code&gt;--service-account&lt;/code&gt; flag), so the agent never runs with your own, much more powerful, account.&lt;/p&gt;




&lt;h2&gt;
  
  
  Guardrail 2: Cap what a single query can cost
&lt;/h2&gt;

&lt;p&gt;BigQuery's on-demand pricing charges by &lt;strong&gt;how much data a query scans&lt;/strong&gt;, not by how many rows it returns. This trips up a lot of people.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM events&lt;/code&gt; on a large table can scan many gigabytes even if you only look at the first ten rows. An agent doesn't know your table sizes, and it will happily write that query.&lt;/p&gt;

&lt;p&gt;The fix is one setting: &lt;code&gt;maximum_bytes_billed&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cloud&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;bigquery&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bigquery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;job_config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bigquery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QueryJobConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;maximum_bytes_billed&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1_000_000_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# 1 GB hard cap per query&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;job_config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;job_config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a query would scan more than 1 GB, BigQuery &lt;strong&gt;refuses to run it&lt;/strong&gt; and you aren't charged. The agent gets an error back, and you can tell it (in its instruction) to rewrite the query with a filter or a &lt;code&gt;LIMIT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two more things worth doing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Estimate before you run.&lt;/strong&gt; A dry run tells you how much a query &lt;em&gt;would&lt;/em&gt; scan, for free:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;dry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;job_config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;bigquery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;QueryJobConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dry_run&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;use_query_cache&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;gb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total_bytes_processed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1e9&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"This query would scan {gb:.2f} GB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can log this for every question. It's a great way to spot which questions are expensive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set a budget alert on day one.&lt;/strong&gt; In the Google Cloud console go to &lt;strong&gt;Billing → Budgets &amp;amp; alerts&lt;/strong&gt;, set a small monthly budget and turn on email alerts at 50% and 90%. It takes five minutes. Note that it &lt;em&gt;warns&lt;/em&gt; you, it doesn't stop spending. The byte cap is what actually stops a bad query.&lt;/p&gt;

&lt;p&gt;And if your real tables are big, &lt;strong&gt;partition them by date&lt;/strong&gt;. Then a "last month" question only scans last month's slice instead of the whole table. It's the single biggest cost saver in real projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Guardrail 3: Cap how many times the agent can try
&lt;/h2&gt;

&lt;p&gt;An agent works in a loop. It reads your question, calls a tool, looks at the result, and decides whether to call the tool again. Usually that's two or three steps.&lt;/p&gt;

&lt;p&gt;But a confused agent can get stuck: the query fails, it tweaks it, it fails again, it tries again… Every loop costs model tokens and maybe a BigQuery query.&lt;/p&gt;

&lt;p&gt;So put a ceiling on it. If you're using Google's Agent Development Kit (ADK), there's a run setting for the maximum number of model calls in one turn. At the time of writing it looks like this (check the ADK docs, because these libraries move fast):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;adk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run_config&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;RunConfig&lt;/span&gt;

&lt;span class="n"&gt;run_config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RunConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_llm_calls&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# pass run_config=run_config when you call the runner&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ten is plenty for a question-answering agent. If it can't answer in ten steps, it should give up and say so, not keep spending.&lt;/p&gt;

&lt;p&gt;While you're at it, add two lines to the agent's instruction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Add LIMIT 1000 to any query that could return many rows.
If a query fails twice, stop and tell the user what went wrong.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Then log every turn.&lt;/strong&gt; For each question, write down three things: the question, the SQL the agent wrote, and the answer. When someone says "it gave me a weird answer", you look at the SQL and see the problem immediately. Without that log you're guessing. On Cloud Run, anything you print shows up in Cloud Logging automatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;p&gt;Before you let anyone else use your data agent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] It runs as a service account with &lt;strong&gt;only&lt;/strong&gt; BigQuery Job User + Data Viewer on &lt;strong&gt;one&lt;/strong&gt; dataset&lt;/li&gt;
&lt;li&gt;[ ] Every query has &lt;code&gt;maximum_bytes_billed&lt;/code&gt; set&lt;/li&gt;
&lt;li&gt;[ ] A billing budget alert is switched on&lt;/li&gt;
&lt;li&gt;[ ] The agent has a maximum number of steps&lt;/li&gt;
&lt;li&gt;[ ] Every question, SQL query and answer is logged&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these make the agent smarter. They make it &lt;strong&gt;safe to be wrong&lt;/strong&gt;, which matters more. Your agent &lt;em&gt;will&lt;/em&gt; write a bad query one day. With these guardrails, a bad query is just an error message, not a deleted table or a scary bill.&lt;/p&gt;




&lt;h2&gt;
  
  
  Want to build the whole agent?
&lt;/h2&gt;

&lt;p&gt;This article is one section of a longer guide I wrote, &lt;strong&gt;Ask Your Data&lt;/strong&gt;. It takes you from an empty folder to a live agent on Cloud Run that answers plain-English questions about your BigQuery data. It uses Gemini, ADK, FastMCP and BigQuery, and it's written in plain language, with all the code, evals, troubleshooting and deployment included.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://anuragjain0.gumroad.com/l/brviaf" rel="noopener noreferrer"&gt;Get &lt;em&gt;Ask Your Data&lt;/em&gt; on Gumroad&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you just wanted the guardrails, I hope this saved you a bad day.&lt;/p&gt;

</description>
      <category>bigquery</category>
      <category>ai</category>
      <category>googlecloud</category>
      <category>python</category>
    </item>
  </channel>
</rss>
