Module 25: Callbacks and Guardrails - Agent Safety and Monitoring

Theoryβ
Beyond Standard Execution: The Need for Controlβ
Production-grade agents require more than just a simple request-response loop. They need safety checks, monitoring, and the ability to dynamically control their own behavior. Callbacks are the ADK's primary mechanism for injecting this custom logic into the agent's execution lifecycle.
The Callback Lifecycle in ADK 2.0β
Figure: Six-stage callback lifecycle hooks matrix intercepting agent nodes, model calls, and tool executions.
Callbacks are registered directly on an Agent node. They allow you to intercept specific stages of that node's execution.
| Callback | Trigger | Arguments | Return Type |
|---|---|---|---|
before_agent_callback | Start of node execution. | (callback_context: CallbackContext) | Optional[types.Content] |
after_agent_callback | End of node execution. | (callback_context: CallbackContext) | Optional[types.Content] |
before_model_callback | Before LLM call. | (callback_context, llm_request: LlmRequest) | Optional[LlmResponse] |
after_model_callback | After LLM response. | (callback_context, llm_response: LlmResponse) | Optional[LlmResponse] |
before_tool_callback | Before tool execution. | (tool: BaseTool, args, tool_context: ToolContext) | Optional[dict] |
after_tool_callback | After tool execution. | (tool: BaseTool, args, tool_context, tool_response) | Optional[dict] |
Control via Return Valuesβ
The power of callbacks lies in their ability to override the default framework behavior:
- Return
None: Tells the ADK to "Continue normally." This is ideal for logging or side-effects. - Return an Object: Tells the ADK to "Stop what you're doing and use THIS instead."
- Example: Returning a
types.Contentfrombefore_agent_callbackwill skip the LLM entirely and return that content as the final result (perfect for Caching). - Example: Returning an error dictionary from
before_tool_callbackwill block the tool from running.
- Example: Returning a
Callbacks vs. Plugins: Which one to use?β
AgentCallbacks (Node-Scoped Control): Registered directly on a singleAgent(before_*=..., after_*=...). Use callbacks when behavior is specific to one agent node (such as per-agent response caching, custom prompt shaping, or single-node tool argument rules).AppPlugins (Fleet-Wide Governance & Telemetry): Registered once onApp(plugins=[...])by subclassingBasePlugin(from google.adk.plugins import BasePlugin). Plugins wrap every agent, sub-agent, and workflow node in the application and execute before node-level callbacksβmaking them the standard architecture for organization-wide RAI/PII guardrails (Module 25.5) and OpenTelemetry/AgentOps observability (Module 26).
Signature Bridge: Agent Function Callbacks vs. BasePlugin Methodsβ
Notice one subtle difference you will use immediately in Modules 25.5 and 26:
- Standalone
Agentcallbacks can be plaindef(orasync def) functions taking positional argumentsβe.g.,def before_model_callback(callback_context, llm_request):. BasePluginlifecycle methods areasync defmethods that receive keyword-only arguments (*)βso include*, ...and**kwargsfor clean forward compatibility:
from google.adk.apps import App
from google.adk.plugins import BasePlugin
class MyPlugin(BasePlugin):
def __init__(self, name: str = "my_plugin"):
super().__init__(name)
# Note the keyword-only `*` signature used by BasePlugin hooks in Modules 25.5 & 26!
async def before_model_callback(self, *, callback_context, llm_request, **kwargs):
print(f"[Plugin] Intercepting LLM request for agent: {callback_context.agent_name}")
return None # Return None to continue, or an LlmResponse to short-circuit
app = App(name="my_app", root_agent=root_agent, plugins=[MyPlugin()])
Built-in Example: Self-Healing Pluginsβ
ADK ships two ready-made plugins, built entirely from the callbacks above, that automatically retry on failure instead of surfacing an error to the user:
ReflectAndRetryModelPlugin(before_model_callback/after_model_callback): catches malformed model responses (like a broken function call), injects reflection guidance, and retries the model call automatically.ReflectAndRetryToolPlugin(before_tool_callback/after_tool_callback): does the same for tools that return an error response (not a Python exception) β e.g. a tool that replies with{"status": "error"}.
from google.adk.apps.app import App
from google.adk.plugins import ReflectAndRetryModelPlugin, ReflectAndRetryToolPlugin
app = App(
name="my_app",
root_agent=root_agent,
plugins=[
ReflectAndRetryModelPlugin(max_retries=3),
ReflectAndRetryToolPlugin(max_retries=3),
],
)
Note these operate one level below the RetryConfig you'll see in Module 38, which retries a failed Workflow node on a raised Python exception. Together, the three cover a different layer each: node β tool response β model response.
These two plugins recover from errors. If you only need to be notified of an unrecoverable one β e.g. to page on-call before the app crashes β BasePlugin also exposes on_agent_error_callback and on_run_error_callback, notification-only hooks that always re-raise the original exception.
In ADK 2.0, what happens when a before_tool_callback returns a dictionary with an error payload instead of None?
Key Takeawaysβ
- Callbacks provide programmatic "hooks" into an agent's execution lifecycle (
before_agent,after_agent,before_model,after_model,before_tool,after_tool). - Node-level scope: Callbacks are registered directly on an
Agentobject for single-node customization and caching. - Overriding: You can bypass expensive LLM calls or risky tool executions by returning a replacement object instead of
Nonefrom a "before" callback. - Bridge to
BasePlugin: When you need these exact lifecycle hooks applied across every agent in anApp, package them into aBasePluginβwhich is exactly what we'll build next in Module 25.5 (Responsible AI & Safety Plugins) for fail-closed enterprise security and Module 26 (Observability & AgentOps) for fleet-wide telemetry!