Plugins in Google ADK are custom code modules that can be executed at various stages of an agent workflow lifecycle using callback hooks. Unlike regular callbacks that are configured on individual agents or tools, plugins are registered once on the Runner and apply globally to every agent, tool, and
Sign in to save downloads to your library and vote.
Preview
π Tutorial 7: Plugins
π― What You'll Learn
- Plugin Fundamentals: Understanding what plugins are and how they work
- Global Callback Management: Using plugins for cross-cutting concerns
- Practical Plugin Examples: Logging, monitoring, and request modification
π‘ Core Concept: Plugins
Plugins in Google ADK are custom code modules that can be executed at various stages of an agent workflow lifecycle using callback hooks. Unlike regular callbacks that are configured on individual agents or tools, plugins are registered once on the Runner and apply globally to every agent, tool, and LLM call managed by that runner.
Plugin vs Callback Comparison
βββββββββββββββββββ βββββββββββββββββββ
β Regular β β Plugin β
β Callbacks β β Callbacks β
βββββββββββββββββββ€ βββββββββββββββββββ€
β β’ Per agent β β β’ Global scope β
β β’ Per tool β β β’ Runner level β
β β’ Specific task β β β’ Cross-cutting β
β β’ Local effect β β β’ Reusable β
βββββββββββββββββββ βββββββββββββββββββ
Plugin Lifecycle Hooks
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β User Message βββββΆβ Runner Start βββββΆβ Agent Execute β
β Callback β β Callback β β Callback β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Model Callback β β Tool Callback β β Event Callback β
β (before/after)β β (before/after)β β (modify event)β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Error Handling β β Runner End β β Cleanup Tasks β
β Callback β β Callback β β & Reporting β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
Why Use Plugins?
- Cross-cutting Concerns: Implement functionality that applies across your entire application
- Reusability: Package related callback functions together for reuse
- Global Monitoring: Track all agent, tool, and model interactions
- Policy Enforcement: Implement security guardrails and access controls
- Logging & Metrics: Centralized logging and performance monitoring
- Request/Response Modification: Dynamically modify inputs and outputs
π Tutorial Overview
This tutorial demonstrates how to create and use plugins in Google ADK through a practical example that combines multiple use cases:
Demo Plugin Features
- Request Logging: Log all user messages with timestamps
- Request Modification: Add timestamp context to user messages
- Agent Tracking: Count and monitor agent executions
- Tool Monitoring: Track tool usage and handle errors
- Final Reporting: Generate summary of plugin activity
π Project Structure
7_plugins/
βββ README.md # This file - concept explanation
βββ agent.py # Agent implementation with plugin
βββ app.py # Streamlit interface
βββ requirements.txt # Dependencies
βββ plugin_example.py # Standalone plugin demonstration
π― Learning Objectives
By the end of this tutorial, you'll understand:
- β Plugin Architecture: How plugins extend the BasePlugin class
- β Global Scope: How plugins apply across all agents and tools
- β Callback Hooks: Available plugin callback methods and their timing
- β Practical Applications: Real-world use cases for plugins
- β Error Handling: How to implement graceful error recovery
- β Simple Implementation: Easy-to-understand plugin structure
π Getting Started
Prerequisites
- Python 3.11+
- Google AI Studio API key
- Basic understanding of Google ADK (Tutorials 1-6)
Setup
- Get API Key: Visit Google AI Studio
- Create .env file: Create a file named
.envin this directory with:GOOGLE_API_KEY=your_google_ai_studio_api_key_here - Install dependencies:
pip install -r requirements.txt
Important:
- Make sure your
.envfile is in the same directory as the tutorial files - Replace
your_google_ai_studio_api_key_herewith your actual API key - The
.envfile should not be committed to version control
Run Tutorial
cd 7_plugins
streamlit run app.py
π§ Key Concepts
Plugin Class Structure
from google.adk.plugins.base_plugin import BasePlugin
class MyPlugin(BasePlugin):
def __init__(self):
super().__init__(name="my_plugin")
# Initialize plugin state
async def before_agent_callback(self, *, agent, callback_context):
# Called before each agent execution
pass
async def after_model_callback(self, *, callback_context, llm_response):
# Called after each model call
pass
Plugin Registration
from google.adk.runners import InMemoryRunner
runner = InMemoryRunner(
agent=my_agent,
app_name="my_app",
plugins=[MyPlugin()] # Register plugins here
)
Available Callback Hooks
on_user_message_callback: Modify user inputbefore_run_callback: Setup before executionbefore_agent_callback/after_agent_callback: Agent lifecyclebefore_model_callback/after_model_callback: Model interactionsbefore_tool_callback/after_tool_callback: Tool executionon_model_error_callback/on_tool_error_callback: Error handlingon_event_callback: Modify events before streamingafter_run_callback: Cleanup after execution
π― Use Cases
Common Plugin Applications
- Logging & Tracing: Detailed logs for debugging and analysis
- Policy Enforcement: Security guardrails and access controls
- Monitoring & Metrics: Performance tracking and analytics
- Response Caching: Cache expensive operations
- Request/Response Modification: Add context or standardize outputs
- Error Recovery: Graceful handling of failures
- Usage Analytics: Track patterns and usage statistics
π¨ Important Notes
- Plugin Precedence: Plugin callbacks run before agent-level callbacks
- Global Scope: Plugins affect all agents, tools, and models in the runner
- Web Interface: Plugins are not supported by the ADK web interface
- Error Handling: Plugin error callbacks can suppress exceptions and provide fallbacks
π Next Steps
After completing this tutorial, you can:
- Create custom plugins for your specific use cases
- Implement monitoring and analytics plugins
- Build security and policy enforcement plugins
- Explore advanced plugin patterns and combinations
π§ Troubleshooting
Common Issues
"Missing key inputs argument" Error
- Ensure you have created a
.envfile with your Google AI Studio API key - Verify the API key is valid and has the necessary permissions
- Check that the
.envfile is in the same directory as the tutorial files
Import Errors
- Make sure you have installed all dependencies:
pip install -r requirements.txt - Verify you're using Python 3.11 or higher
Plugin Not Working
- Remember that plugins are not supported by the ADK web interface
- Ensure you're running the agent through the Streamlit app or Python script
π Additional Resources
Ingestion metadata
- Source catalog
- awesome-llm-apps
- Repository
- Shubhamsaboo/awesome-llm-apps Β· main
- File path
- ai_agent_framework_crash_course/google_adk_crash_course/7_plugins/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (53m ago)
- Refresh schedule
- Daily Β· 03:00 UTC
- Dedupe status
- Unique Β· deduped by (source, url)