tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Agentic AI > ADK Agent Testing > Testing ADK Agent before_tool_callback and after_tool_callback

Testing ADK Agent before_tool_callback and after_tool_callback

Author: Venkata Sudhakar

ShopMax India uses ADK agent callbacks to enforce business rules around every tool call: before_tool_callback validates that the customer is authenticated and the order ID is in the correct format before the tool executes, while after_tool_callback logs the result and redacts PII from the response. Testing these callbacks ensures the safety and audit layers work independently of the tool implementation itself.

ADK callbacks are functions invoked by the framework before and after each tool call. before_tool_callback receives the tool name and arguments and can raise an exception to block the call or modify the arguments. after_tool_callback receives the tool result and can transform or log it. In tests, call the callbacks directly with crafted inputs and assert on their outputs and side effects independently of any LLM or tool execution.

The example below defines before and after callbacks for ShopMax India's order lookup tool. The before callback validates the order ID format and blocks calls with invalid IDs. The after callback redacts the customer phone number from the result. Tests cover the validation, the block path, and the redaction.


It gives the following output,

... (3 passed in 0.01s)

In production, ShopMax India should keep callbacks stateless where possible - side effects like logging should write to an append-only audit store rather than mutable in-memory lists. Test callbacks in isolation from the tool so that a failing tool does not mask a failing callback. Version callbacks alongside the tools they guard, and run callback unit tests in CI before deploying any prompt or tool change that touches the guarded fields.


 
  


  
bl  br