adk-plugin
Plugin system for ADK-Rust agents.
Stability: Stable — follows semantic versioning, so breaking changes wait for the next major release. See STABILITY.md.
Overview
adk-plugin extends agent behavior through callbacks at lifecycle points. It has two plugin models:
| Model | Types | Installed on |
|---|---|---|
| Run plugins | Plugin, PluginConfig, PluginBuilder, PluginManager |
Runner::builder().plugin_manager(...) — run, user-message, event, agent, model, and tool callbacks for every agent the run reaches |
| Enhanced plugins | EnhancedPlugin, EnhancedPluginManager, PluginContext, AdaptedPlugin |
LlmAgentBuilder::enhanced_plugin(...) (adk-agent's enhanced-plugins feature) — priority-ordered tool and model call interception with shared typed state |
Installation
[]
= "3.0.0"
Quick Start
use Arc;
use ;
use ;
use Runner;
use SessionService;
The runner calls the run, user-message, and event callbacks itself, and installs the manager on the run's RunConfig::invocation_hooks, so LlmAgent and CodeActAgent run its agent, model, and tool callbacks ahead of their own for every agent the run reaches. A plugin callback that returns a value short-circuits the agent's callbacks of that kind. For callbacks that can modify data, the first plugin to return a modification wins.
Callback Types
Run Lifecycle
| Callback | Description |
|---|---|
on_user_message |
Called when a user message is received; can modify it |
on_event |
Called for each event the run yields, including a before_run early exit, a cancelled run's answers to its calls in flight, and a budget stop event; can modify it |
before_run |
Called before the run starts; a returned content ends the run early |
after_run |
Called once the run ends, however it ends — including a consumer that drops the stream |
close_fn |
Called by PluginManager::close, which Runner::close runs (bounded by PluginManagerConfig::close_timeout) |
Agent Callbacks
| Callback | Description |
|---|---|
before_agent |
Called before an agent runs |
after_agent |
Called after an agent runs, including after a transfer, a turn-ending tool result, and a cancellation |
Model Callbacks
| Callback | Description |
|---|---|
before_model |
Called before a model call; can modify the request or skip the call |
after_model |
Called after a model call; can modify the response |
on_model_error |
Called when a model call or its stream fails; a returned response answers the turn |
Tool Callbacks
| Callback | Description |
|---|---|
before_tool |
Called before a tool executes; a returned content skips the tool |
after_tool |
Called after a tool executes |
on_tool_error |
Called when a tool fails; can provide a fallback result |
Example: Caching Plugin
use HashMap;
use Arc;
use ;
use ;
use RwLock;
Example: Metrics Plugin
use Arc;
use ;
use ;
Enhanced Plugins
EnhancedPlugin implements only the hooks it needs; priority() orders plugins (lower first, default 100). before_tool_call returns BeforeToolCallResult::Continue(args) (possibly rewritten) or ShortCircuit(result), and after_tool_call may rewrite the result; before_model_call and after_model_call do the same for model calls. PluginContext holds typed state shared across the hooks of one invocation; PluginContext::shared() holds state every invocation of the agent shares. AdaptedPlugin runs a run plugin's tool and model callbacks as an enhanced plugin; a before_tool that returns content short-circuits the call with it.
use Arc;
use ;
use ;
use async_trait;
use ;
;
Enhanced plugins run before the governed authorization path, so the tool policy, guardrails, and confirmation see the arguments a plugin rewrote.
Changes in 3.0
| Change | Migration |
|---|---|
A runner's PluginManager agent, model, and tool callbacks now run (through RunConfig::invocation_hooks) |
Callbacks that did nothing before now take effect |
on_model_error runs for a failed model call or stream, and its response answers the turn |
— |
after_run runs once, after the run's last event, however the run ends |
— |
An AdaptedPlugin's legacy before_tool refusal stops the tool |
— |
An EnhancedPluginManager gives each invocation its own PluginContext |
Keep state that spans invocations in plugin_ctx.shared() |
See the 2.x to 3.0 migration guide and the changelog for the full list.
Related Crates
- adk-rust — umbrella crate
- adk-core — core traits and types
- adk-agent — agent implementations
- adk-runner — execution runtime
- adk-retry-reflect — a retry-and-reflect plugin
License
Apache-2.0
Part of ADK-Rust
This crate is part of the ADK-Rust framework for building AI agents in Rust.