๊ฐ€์ž… ํ›„ ์ดˆ๋Œ€ ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜๋ฉด ๋™์˜์ƒ ์žฌ์ƒ ๋ฐ ์ดˆ๋Œ€ ๋ณด์ƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
๊ฐ€์ž… May 2026
280 ํŒ”๋กœ์ž‰ ์ค‘    415 ํŒฌ
# 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#
๋” ๋ณด๊ธฐ