Google ADK guardrails that stop a bad tool call before it runs
Google ADK guardrails for tool calls, from before_tool_callback to a rule drafted from your agent's own traces, tested in Shadow and enforced by an ADK plugin.
Google ADK guardrails for tool calls, from before_tool_callback to a rule drafted from your agent's own traces, tested in Shadow and enforced by an ADK plugin.
Google ADK guardrails for tool calls come in two forms, and both can stop a call before the tool runs. One is a before_tool_callback on a single LlmAgent, and the other is a plugin on the App, which sees the calls of every agent in it. The example agent answers a shop's support chat with an issue_refund tool, and the bad call is a refund of 900 from an agent whose refunds have never gone above 140.
Google's safety page for ADK lists several kinds of Agent Development Kit guardrails. You can design tools that only expose what the model should do, and lean on Gemini's own content filters. A cheap Gemini model can screen inputs and outputs from a callback, and callbacks and plugins can "validate model and tool calls before or after execution, checking parameters against agent state or external policies."
A before_tool_callback runs once the model has asked for a tool and before the tool's code starts. Return None and the tool runs. A returned dict skips the tool and goes to the model as its result:
from typing import Any
from google.adk.agents import LlmAgent
from google.adk.tools.base_tool import BaseTool
from google.adk.tools.tool_context import ToolContext
def refund_limit(tool: BaseTool, args: dict[str, Any], tool_context: ToolContext) -> dict | None:
if tool.name == "issue_refund" and args.get("amount", 0) > 200:
return {
"status": "error",
"message": "Not performed: refunds over 200 need a person. "
"Do not retry with a smaller amount. Tell the customer.",
}
return None
agent = LlmAgent(
name="support",
model=model,
tools=[lookup_order, issue_refund],
before_tool_callback=refund_limit,
)A refund of 900 comes back to the model as that error and the payment provider never hears about it. If you need a running total, tool_context.state holds values for the session, so the callback can add up what it has already refunded.
ADK's callback reference gives the exact signature to copy. Python passes callback arguments by keyword. So the parameters have to be named tool, args and tool_context, and a callback that calls one of them ctx fails with a TypeError when the agent runs. Only None lets the tool run. ADK tests the return value with is None rather than for truthiness, so a callback that ends in return {} skips every tool it touches and gives the model {}.
A callback belongs to one agent, and ADK's way to cover every agent at once is a plugin. It's a BasePlugin subclass with its own before_tool_callback, registered once on the App, and its hooks run before any agent's callbacks. The plugins page recommends them over callbacks for security guardrails. Plugins run in list order. Once one returns a value, later plugins and the agent's own callback never see that call.
For a check that is simply true, such as a refund that can't be negative or larger than the order, the callback is the right tool and you can stop reading here.
That 200 was a guess, like most numbers in hand-written Google ADK guardrails. If this agent's refunds run from a few dollars up to about 140, a limit of 200 lets through refunds well past anything it has made, while 50 refuses its normal ones between 50 and 140. Wherever the line goes, nothing tells you when it's wrong, and unless the callback reads it from config, moving it means a deploy.
ADK already records each invocation as OpenTelemetry spans, and after pip install "google-adk[otel]" the exporter is one call at startup:
from google.adk.telemetry.setup import maybe_set_otel_providers maybe_set_otel_providers()
The endpoint and key go in the standard OTEL_EXPORTER_OTLP_TRACES_* variables. Setting OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to SPAN_ONLY puts each tool call's arguments on its span, which is what a rule about amounts needs. The Google ADK traces guide has the full list of variables, and each invocation arrives as a run under the agent's name, support here.
From those runs Amiril learns what is normal for this one agent. A refund far outside that range shows on the run as a rarity factor:
args.amount was 900 on an issue_refund call, the largest of 1,180 observations
Runs that share a problem are grouped into an issue, and an AI model reads a sample of them and drafts a rule that would have stopped them. Amiril tests the draft on past runs before you see it, and keeps it only if it catches some of the issue's runs and fires on at most one in a hundred of the agent's other runs. What survives shows up as a rule suggestion on the issue's page, with a line such as "Would have blocked 3 of 4 runs". Selecting Create rule opens the editor with the draft in it:
@id("refund-limit")
@sentence("A refund of {amount} is over the 150 refund limit")
block (
agent == "support",
tool == "issue_refund"
)
when {
call.args.amount > 150
};The 150 sits just above the top of what this agent normally refunds, so if the shop's policy says 100 without a manager, type 100. support is the name you gave the LlmAgent, and the sentence is what the model reads when the rule fires. Some rarity factors carry Turn into a rule on the run's page, which opens the editor already filled in from the value, so a rule can start there too.
A rule can also read earlier calls. This one caps a conversation's total: sum adds the refunds that already went through to the one being asked for. On ADK the session is ADK's own session id, so a customer who gets 100 and then 140 is refused a third refund of 120:
@id("session-refund-total")
@scope("session")
@sentence("This refund brings the conversation's refunds to {session_total}, over 300")
block (
agent == "support",
tool == "issue_refund"
)
unless {
session_total = sum(issue_refund{}.args.amount, call.args.amount);
session_total <= 300
};The rule language tour covers the rest, such as a rule that requires an earlier lookup_order call for the same order.
A saved rule is a draft. Once you publish it, it starts in Shadow. It checks every call and records what it would have done, then lets the call through anyway.
Test on past calls replays any version of the rule, an unpublished draft included, over the last 24 hours, 7 days or 30 days. It uses the same engine the SDK runs and lists each call the rule would have fired on, with the agent, the tool and the sentence. Since it replays traces, you can run it before the SDK is installed. Here you'd find any past conversation the 300 total would have cut short, such as three refunds of 120, and decide whether the rule should allow it.
The block happens inside the agent, through the Amiril SDK:
pip install "amiril[adk]"
import amiril
from google.adk.agents import LlmAgent
from google.adk.apps import App
from google.adk.runners import InMemoryRunner
amiril.init()
agent = LlmAgent(
name="support",
model="gemini-flash-latest",
tools=[lookup_order, issue_refund],
before_tool_callback=refund_limit,
)
app = App(name="support", root_agent=agent)
amiril.instrument(app)
runner = InMemoryRunner(app=app)amiril.init() reads AMIRIL_API_KEY and AMIRIL_ENDPOINT from the environment, and instrument puts Amiril's plugin first in app.plugins. Call instrument before you build the runner, because a Runner copies the app's plugin list when it's built. The Google ADK guardrails you already wrote stay where they are, and refund_limit still runs on every call Amiril lets through. TypeScript works the same way with @amiril/sdk-adk, and the SDK guide has both languages.
Switch the rule to Enforce on its page. The next time the model asks for a 900 refund, ADK gives it this as the tool's result:
The call to issue_refund was not performed. Rule refund-limit blocked it: A refund of 900 is over the 150 refund limit. Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed.
It arrives in a dict, {"status": "blocked", "blocked_by": "amiril", "message": ...}, the same kind of answer your own callback gives. The rule's page counts what the SDK blocked, flagged and allowed.
The SDK keeps a copy of your published rules in the agent's process and checks each call against it, with no network request per check. It asks Amiril for changes about once a minute, so lowering 150 to 100 is an edit and a publish. If Amiril can't be reached, the SDK goes on enforcing the last rules it had, and it saves them to disk so a restart doesn't lose them.
On LangGraph, middleware takes the plugin's place, and the LangGraph tool call guardrails post walks through it. CrewAI, the OpenAI Agents SDK and the Claude Agent SDK are on the AI agent guardrails page. To try this on an ADK agent, start by sending its traces.