Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture 投稿は個人の意見です。
Joined May 2026
280 Following    415 Followers
# 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#
Show more