- the callback pattern can protect model I/O through
before_model_callbackandafter_model_callback - tool guardrails through
before_tool_callbackandafter_tool_callback - this example uses server-side control execution
What It Demonstrates
- pre-LLM prompt injection blocking
- pre-tool restricted-city blocking
- post-tool output filtering for synthetic unsafe output
- fail-closed behavior when the Agent Control server cannot evaluate safely
Prerequisites
-
Start the Agent Control server from the repo root:
-
Install the example dependencies:
-
Set your Google API key:
-
Optional environment variables:
Setup
Create the demo controls and attach them to the example agent:adk-callbacks-block-prompt-injectionadk-callbacks-block-restricted-citiesadk-callbacks-block-internal-contact-output
Run
Run the ADK app from the example directory:Suggested Scenarios
Safe request:Testville is a deliberate demo trigger that makes the tool produce an internal contact note so the post-tool control can block it deterministically.
Files
setup_controls.py- registers the agent and creates the callback example controlsmy_agent/agent.py- ADK app with Agent Control callbacks.env.example- environment variables for local runs
Notes
- This example is server-only by design.
- The callback pattern itself can also support sdk-local evaluation when your callback wiring goes through Agent Control’s local-aware evaluation helpers instead of the server-only helper used here.
- Google ADK also supports
after_model_callback; this example keeps the manual flow smaller and wiresbefore_model_callbackplus the tool hooks. - If you want the recommended packaged integration, use /examples/google-adk-plugin.
- If you want the
@control()pattern or sdk-local execution, use /examples/google-adk-decorator.
Source Code
View the complete example with all scripts and setup instructions:Google ADK Callbacks Example