AbstractInitializes the plugin.
A unique identifier for this plugin instance.
Callback executed after an agent's primary logic has completed.
This callback can be used to inspect, log, or append an additional event after the agent's execution completes.
An optional Content object. If a value is returned, it will be
emitted as an additional event appended after the agent's execution.
Returning undefined emits no additional content event.
Callback executed after context compaction.
This callback provides an opportunity to inspect the context after it has been compacted.
The context for the entire invocation.
The trigger for the context compaction.
Callback executed after a response is received from the model.
This is the ideal place to log model responses, collect metrics on token
usage, or perform post-processing on the raw LlmResponse.
The context for the current agent call.
The response object received from the model.
An optional value. A non-undefined return may be used by the
framework to modify or replace the response. Returning undefined
allows the original response to be used.
Callback executed after a workflow node has run.
Not called for a node whose body was skipped by beforeNodeCallback, and
not called when the node throws.
The node that has just run.
The node's execution context.
The output the node produced.
An optional value replacing the node's output. Returning
undefined keeps the original.
Callback executed after successful completion, abort, or early iterator close.
This is the final callback in the ADK lifecycle, suitable for cleanup, final logging, or reporting tasks. It is skipped when execution fails; onRunErrorCallback is called instead. Receiving this callback does not imply success: recognized cancellation still propagates to the caller.
The context for the entire invocation.
undefined
Callback executed after a tool has been called.
This callback allows for inspecting, logging, or modifying the result returned by a tool.
An optional dictionary. If a dictionary is returned, it will
replace the original result from the tool. This allows for
post-processing or altering tool outputs. Returning undefined uses
the original, unmodified result.
Callback executed before an agent's primary logic is invoked.
This callback can be used for logging, setup, or to short-circuit the agent's execution by returning a value.
An optional Content object. If a value is returned, it will
bypass the agent's callbacks and its execution, and return this value
directly. Returning undefined allows the agent to proceed normally.
Callback executed before context compaction.
This callback provides an opportunity to inspect or modify the context before it is compacted.
The context for the entire invocation.
The trigger for the context compaction.
Callback executed before a request is sent to the model.
This provides an opportunity to inspect, log, or modify the LlmRequest
object. It can also be used to implement caching by returning a cached
LlmResponse, which would skip the actual model call.
The context for the current agent call.
The prepared request object to be sent to the model.
An optional LlmResponse. Returning a non-undefined value
triggers an early exit and returns that response immediately. Returning
undefined allows the LLM request to proceed normally.
Callback executed before a workflow node runs.
The input the node is about to receive.
The node that is about to run.
The node's execution context.
An optional value. If anything other than undefined is returned,
the node's body is skipped and the returned value becomes its output,
which is how a plugin implements a node-level cache or a stub.
Returning undefined lets the node run normally.
Callback executed before the ADK runner runs.
This is the first callback to be called in the lifecycle, ideal for global setup or initialization tasks.
The context for the entire invocation, containing session information, the root agent, etc.
An optional Content to be returned to the ADK. Returning a value
halts execution of the runner and ends the run with an event containing
that content. Return undefined to proceed normally.
Callback executed before a tool is called.
This callback is useful for logging tool usage, input validation, or modifying the arguments before they are passed to the tool.
An optional dictionary. If a dictionary is returned, it will stop
the tool execution and return this response immediately. Returning
undefined uses the original, unmodified arguments.
Callback executed before a tool is selected.
This callback provides an opportunity to inspect, log, or modify the available tools before they are selected.
An optional value. A non-undefined return may be used by the
framework to modify or replace the available tools. Returning
undefined allows the original tools to be used.
Observes an error escaping an agent's execution or lifecycle callbacks.
Notification-only: return values are ignored and callback failures are logged. The original execution error always propagates.
Callback executed after an event is yielded from runner.
This is the ideal place to make modification to the event before the event is handled by the underlying agent app.
The event raised by the runner.
The context for the entire invocation.
An optional value. A non-undefined return may be used by the
framework to modify or replace the response. Copy the incoming
event when constructing a replacement to preserve fields that are not
being modified, such as event actions. Returning undefined allows the
original response to be used.
Callback executed when a model call encounters an Error.
This callback provides an opportunity to handle model errors gracefully, potentially providing alternative responses or recovery mechanisms.
The context for the current agent call.
The Error instance thrown during model execution.
The request that was sent to the model when the error occurred.
An optional LlmResponse. If an LlmResponse is returned, it
will be used instead of the default error response. Returning
undefined causes an error response/event with errorCode and
errorMessage derived from error to be yielded.
Observes an error in the run lifecycle, including before/after-run callbacks.
Notification-only: return values are ignored and callback failures are logged. The original execution error always propagates. A completion-hook failure during early iterator close is reported but does not prevent closing.
Callback executed when a tool call encounters an Error or cannot be
resolved.
This callback provides an opportunity to handle tool errors gracefully, potentially providing alternative responses or recovery mechanisms.
An optional dictionary. If a dictionary is returned, it will be
used as the tool response instead of the default error response.
Returning undefined causes {error: error.message} to be returned as
the tool response (unlike adk-python, which re-raises). Non-Error
throwables bypass this callback and become {error: thrownValue}.
Callback executed when a user message is received before an invocation starts.
This callback helps logging and modifying the user message before the runner starts the invocation.
The context for the entire invocation.
The message content input by user.
An optional Content to be returned to the ADK. Returning a
value to replace the user message. Returning undefined to proceed
normally.
Base class for creating plugins.
Plugins provide a structured way to intercept and modify agent, tool, and LLM behaviors at critical execution points in a callback manner. While agent callbacks apply to a particular agent, plugins applies globally to all agents added in the runner. Plugins are best used for adding custom behaviors like logging, monitoring, caching, or modifying requests and responses at key stages.
A plugin can implement one or more methods of callbacks, but should not implement the same method of callback for multiple times.
Relation with Agent callbacks:
Execution Order Similar to Agent callbacks, Plugins are executed in the order they are registered. However, Plugin and Agent Callbacks are executed sequentially, with Plugins takes precedence over agent callbacks. When the callback in a plugin returns a value, it will short circuit all remaining plugins and agent callbacks, causing all remaining plugins and agent callbacks to be skipped.
Change Propagation Plugins and agent callbacks can both modify the value of the input parameters, including agent input, tool input, and LLM request/response, etc. They work in the exactly same way. The modifications will be visible and passed to the next callback in the chain. For example, if a plugin modifies the tool input with before_tool_callback, the modified tool input will be passed to the before_tool_callback of the next plugin, and further passed to the agent callbacks if not short circuited.
To use a plugin, implement the desired callback methods and pass an instance of your custom plugin class to the ADK Runner.
Example: A simple plugin that logs every tool call.