Back to Adk Python

Callbacks and Plugins

.agents/skills/adk-workflow/references/callbacks-and-plugins.md

2.0.0b12.6 KB
Original Source

Callbacks and Plugins

Agent Callbacks

python
root_agent = Agent(
    before_agent_callback=my_before_cb,      # Before agent runs
    after_agent_callback=my_after_cb,         # After agent runs
    before_model_callback=my_before_model,    # Before LLM call
    after_model_callback=my_after_model,      # After LLM call
    before_tool_callback=my_before_tool,      # Before tool call
    after_tool_callback=my_after_tool,        # After tool call
    on_model_error_callback=my_error_cb,      # On LLM error
    on_tool_error_callback=my_tool_error_cb,  # On tool error
    ...
)

Note: CallbackContext is a backward-compatible alias for Context. Both work identically.

Callback Signatures

python
# before_agent / after_agent
def callback(callback_context: CallbackContext):
  return None  # Continue normal flow
  # OR return ModelContent to override

# before_model
def callback(callback_context, llm_request: LlmRequest):
  return None  # Continue to LLM
  # OR return LlmResponse to skip LLM

# after_model
def callback(callback_context, llm_response):
  return None  # Use actual response
  # OR return LlmResponse to override

# before_tool
def callback(tool, args, tool_context):
  return None  # Call tool normally
  # OR return dict to skip tool

# after_tool
def callback(tool, args, tool_context, tool_response):
  return None  # Use actual response
  # OR return dict to override

Multiple callbacks: Pass a list. They execute in order until one returns non-None.

Plugins (App-Level Callbacks)

python
from google.adk.plugins.base_plugin import BasePlugin

class MyPlugin(BasePlugin):
  def __init__(self):
    super().__init__(name='my_plugin')

  async def before_agent_callback(self, *, agent, callback_context):
    pass

  async def before_model_callback(self, *, callback_context, llm_request):
    pass

Built-in Plugins

PluginImportPurpose
ContextFilterPluginfrom google.adk.plugins.context_filter_plugin import ContextFilterPluginLimit history in context
SaveFilesAsArtifactsPluginfrom google.adk.plugins.save_files_as_artifacts_plugin import SaveFilesAsArtifactsPluginAuto-save file outputs
GlobalInstructionPluginfrom google.adk.plugins.global_instruction_plugin import GlobalInstructionPluginInject global instructions

Usage with App:

python
from google.adk.apps import App
from google.adk.plugins.context_filter_plugin import ContextFilterPlugin

app = App(
    name='my_app',
    root_agent=root_agent,
    plugins=[ContextFilterPlugin(num_invocations_to_keep=3)],
)