# Useful but Little-Known Features of ADK 2.0
๐ Do you know the different types of callbacks in ADK 2.0 and when to use each one?
ADK 2.0 provides callbacks across three layers: agent lifecycle, LLM calls, and tool execution. The Before/After pattern at each layer lets you flexibly inject validation, guardrails, logging, and more.
๐ Title: Types and Patterns of Callbacks
๐ URL:
๐งฉ Overview
ADK 2.0 callbacks fall into three categories. Agent lifecycle callbacks (`BeforeAgentCallback` / `AfterAgentCallback`) insert processing before and after agent execution โ useful for validation and cleanup. LLM callbacks (`BeforeModelCallback` / `AfterModelCallback`) operate around model calls for request modification and guardrails. Tool callbacks (`BeforeToolCallback` / `AfterToolCallback`) handle validation and result processing around tool execution.
๐ How to use it
Callbacks are specified when defining an agent. In Python, exact parameter names (`callback_context`, `llm_request`, `tool_context`) are required.
```python
from adk import Agent
async def before_agent(callback_context) -> None:
"""Validate before agent execution."""
print(f"Agent starting: {callback_context.agent_name}")
# Return None to continue, return a value to skip
async def before_model(callback_context, llm_request):
"""Guardrails before model call."""
if contains_sensitive_info(llm_request):
return block_response() # returning a value skips the model call
return None # continue with normal model call
async def after_tool(callback_context, tool_context, tool_response):
"""Log after tool execution."""
log_tool_usage(tool_context.tool_name, tool_response)
return None
agent = Agent(
name="my_agent",
model="gemini-3.5-flash",
before_agent_callback=before_agent,
before_model_callback=before_model,
after_tool_callback=after_tool,
)
```
Before callbacks that return a value skip subsequent processing; returning None continues normal execution.
๐ Building it into production
ใปUse `BeforeAgentCallback` for input validation and auth checks to reject bad requests early
ใปApply guardrails (PII detection, harmful content filters) in `BeforeModelCallback`
ใปValidate model output format and policy compliance in `AfterModelCallback`
ใปRecord tool execution results in `AfterToolCallback` for audit trails
๐ก Use cases
๐ก Block prompts containing personal information with `BeforeModelCallback`
๐ Record agent execution results to a database with `AfterAgentCallback`
โ
Validate tool call parameters with `BeforeToolCallback`
๐ Verify JSON format of model output in `AfterModelCallback` and trigger retries
โ ๏ธ Watch out
In Python, callback function parameter names must be exact โ `callback_context`, `llm_request`, `tool_context`, etc. Mismatched names will cause silent failures. Be careful not to accidentally return a value from Before callbacks, as this skips model calls or tool execution. Also remember that callbacks execute after plugins in the processing order.
โจ Using the right callbacks at the right layer gives you fine-grained control over agent behavior. Combine callbacks across layers to meet your security, quality assurance, and audit requirements.
#
ADK# #
AIAgent#