Add Amiril to a Google ADK agent
Check every tool call of a Google ADK agent against your rules, in Python or TypeScript. Amiril runs as the first plugin and blocks calls that break a rule.
On Google ADK, Amiril is a plugin. It sits first in your app's plugin list, so it sees every tool call before other plugins do. You add it to your App, and your agent and its tools stay as they are.
Install
pip install "amiril[adk]"Python needs 3.11 or later and google-adk 2.11 or later. TypeScript needs Node 24 or later and @google/adk 2.2.1 or later.
Save a rules file
Save this file as rules.json beside your agent. Its one rule blocks an issue_payment call for more than 10000. With the rule in a local file, you can try the SDK before you have an API key. The file is JSON because that is the form the SDK runs. Once you connect, you write rules as rule text in Amiril and the SDK fetches them in this form.
{
"schema": "amiril.rules/1",
"ruleset_version": "example-1",
"rules": [
{
"id": "payment-cap",
"version": "1",
"mode": "enforce",
"description": "A payment amount must be above 0 and at most 10000",
"target": { "tools": ["issue_payment"] },
"assert": { "and": [{ ">": [{ "arg": "amount" }, 0] }, { "<=": [{ "arg": "amount" }, 10000] }] },
"action": "block"
}
]
}Add Amiril to your app
Call init once, then hand your App to instrument.
import amiril
from google.adk.agents import LlmAgent
from google.adk.apps import App
def issue_payment(payee: str, amount: float) -> dict:
"""Send a payment to a payee."""
return {"payee": payee, "amount": amount, "status": "sent"}
amiril.init(rules_file="rules.json")
agent = LlmAgent(name="payments", model=model, tools=[issue_payment])
app = App(name="payments", root_agent=agent)
amiril.instrument(app) # puts Amiril's plugin first in app.pluginsCall instrument before you build the Runner, because a Runner copies the app's plugin list when it is built. model is the model your agent already uses. Rules see the agent under its ADK name, payments here.
Run the agent
Run the agent the way you already do. In this snippet a scripted model stands in for Gemini and asks for payments of 500 and 20000 in one turn.
import asyncio
import json
from google.adk.runners import InMemoryRunner
from google.genai import types
async def main() -> None:
runner = InMemoryRunner(app=app)
session = await runner.session_service.create_session(app_name=app.name, user_id="user")
message = types.Content(role="user", parts=[types.Part.from_text(text="Pay Acme Ltd 500 and 20000.")])
async for event in runner.run_async(user_id="user", session_id=session.id, new_message=message):
for part in event.content.parts if event.content else []:
if part.function_response:
print(part.function_response.id, json.dumps(part.function_response.response))
await runner.close()
asyncio.run(main())The payment of 500 runs, and the payment of 20000 is blocked before it reaches the tool. ADK hands the model Amiril's answer as the tool's result. That answer tells the model why the rule blocked the call.
c1 {"payee": "Acme Ltd", "amount": 500, "status": "sent"}
c2 {"status": "blocked", "blocked_by": "amiril", "message": "The call to issue_payment was not performed. Rule payment-cap blocked it: A payment amount must be above 0 and at most 10000 (amount 20000). Do not retry this call with different arguments or achieve the same outcome another way; tell the user it was not performed."}Connect to your workspace
So far the SDK runs the rules in your file. Connect it to your workspace and it runs the rules you publish in Amiril instead.
- Open the Data page and select Set up a new installation.
- Under API key, choose Create a new key. Name it and select Create key.
- Select Copy key. Amiril shows the key only once.
- Select Copy SDK endpoint.
Where your agent runs, set the key, the endpoint and a name for this service:
export AMIRIL_API_KEY="<your API key>"
export AMIRIL_ENDPOINT="<the SDK endpoint>"
export AMIRIL_SERVICE="<a name for this service>"Remove rules_file from init, or rulesFile in TypeScript, and restart the agent. The SDK now enforces the rules published to the key's environment. A rule blocks calls only once it is published and in Enforce, as Write your first rule shows.
When the agent starts, your service appears on the Installations page under the name in AMIRIL_SERVICE. Its Coverage card lists the agent and the tools Amiril checks. Set it for each service you run. Without it the SDK takes the name in your pyproject.toml or package.json, and two services in one project become one installation. SDK configuration has the order it reads names in.
Next steps
Write your first rule
Start from a rule Amiril drafted for an issue, or from a blank rule. Save it, publish it in Shadow, then switch it to Enforce in one environment.
How rules reach your agent
The SDK keeps your published rules in the agent's process and checks each tool call there. It keeps checking when Amiril is unreachable or the agent restarts.
SDK configuration
Every option of the Amiril SDK's init and every environment variable it reads, in Python and TypeScript, with its type, its default and what it does.
Send traces from Google ADK
Turn on Google ADK's own OpenTelemetry tracing in Python or TypeScript and send every agent invocation to Amiril, with each tool call's arguments and result.
Last updated on