# Useful but Little-Known Features of ADK 2.0
๐ What if you could hook into the entire agent execution lifecycle โ observing, intervening, and amending โ without touching agent code?
ADK 2.0's Plugin system lets you extend `BasePlugin` and register it on a Runner to apply lifecycle callbacks globally across all agents. Unlike per-agent callbacks, plugins operate at the runner level with cross-cutting scope.
๐ Title: Plugins
๐ URL:
๐งฉ Overview
Plugins extend `BasePlugin` and are registered on the Runner. Unlike agent-specific callbacks, they apply globally across all agents. Lifecycle hooks cover the full span: user message receipt, runner start, agent execution, model calls, tool execution, event processing, and runner end. Plugins operate in three modes: Observe (monitoring only), Intervene (modify or block processing), and Amend (modify results after the fact). Plugin callbacks run BEFORE agent callbacks in the execution order.
๐ How to use it
Extend `BasePlugin` and override the lifecycle hooks you need.
```python
from adk.plugins import BasePlugin
class LoggingPlugin(BasePlugin):
def __init__(self):
super().__init__(name="logging_plugin")
async def on_before_model_call(self, callback_context, llm_request):
print(f"Model call: {llm_request.model}")
return None # returning None continues normal processing
async def on_after_tool_call(self, tool_context, tool_response):
print(f"Tool executed: {tool_context.tool_name}")
return None
# Register on Runner
runner = Runner(
agent=my_agent,
plugins=[LoggingPlugin()]
)
```
In Intervene mode, return a value from the callback to replace the normal processing. In Amend mode, modify results after event processing.
๐ Building it into production
ใปImplement logging and metrics collection as Observe-mode plugins to keep agent code clean
ใปBuild guardrails and content filtering as Intervene-mode plugins to block inappropriate I/O
ใปAdd analytics data collection as Amend-mode post-processing
ใปLeverage prebuilt plugins (Reflect/Retry, BigQuery Analytics, Context Filtering, Global Instructions) to accelerate development
๐ก Use cases
๐ Log all model calls and tool executions across every agent to BigQuery
๐ก Centralize input guardrails in a plugin to block harmful requests system-wide
๐ Implement retry logic for failed model calls using the Reflect/Retry plugin
๐ Apply global instructions (compliance rules, etc.) to all agents via the Global Instructions plugin
โ ๏ธ Watch out
Plugin callbacks execute before agent callbacks โ if a plugin blocks processing, the agent callback won't fire. Returning incorrect values in Intervene mode can break agent behavior, so understand the expected return types and semantics before using it. Plugin execution order depends on registration order in the Runner.
โจ Plugins let you separate cross-cutting concerns (logging, security, analytics) from agent core logic, enabling highly maintainable systems.
#
ADK# #
AIAgent#