0

🎯 Tutorial 6.3: Tool Execution Callbacks

awesome-llm-apps6_3_tool_execution_callbacks

Tool execution callbacks allow you to monitor when agents use tools, track their execution lifecycle, and analyze the results. This provides visibility into how agents interact with external systems and APIs.

Sign in to save downloads to your library and vote.

Preview

🎯 Tutorial 6.3: Tool Execution Callbacks

🎯 What You'll Learn

  • Before Tool Callbacks: Monitor when tools begin execution
  • After Tool Callbacks: Track tool completion and results
  • Tool Context: Understand how tool execution is monitored

🧠 Core Concept: Tool Execution Monitoring

Tool execution callbacks allow you to monitor when agents use tools, track their execution lifecycle, and analyze the results. This provides visibility into how agents interact with external systems and APIs.

Tool Execution Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Tool Call     │───▢│  Before Tool    │───▢│  Tool Execution β”‚
β”‚   (Agent)       β”‚    β”‚   Callback      β”‚    β”‚   (External)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚                       β”‚
                              β–Ό                       β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚  After Tool     β”‚    β”‚  Tool Result    β”‚
                       β”‚   Callback      β”‚    β”‚   (Agent)       β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Callback Execution Timeline

Time β†’ 0ms    5ms    10ms   15ms   20ms   25ms
       β”‚      β”‚      β”‚      β”‚      β”‚      β”‚
       β–Ό      β–Ό      β–Ό      β–Ό      β–Ό      β–Ό
    [Tool] [Before] [Exec] [After] [Result]
    Call   Callback Start  Callback Return

Use Cases

  • Execution Monitoring: Track when tools start and complete
  • Parameter Validation: Check tool inputs before execution
  • Result Logging: Record tool outputs and errors
  • Debugging: Understand tool execution patterns
  • Analytics: Monitor which tools are used most

πŸš€ Tutorial Overview

In this tutorial, we'll create an agent with tool execution callbacks that:

  • Uses a simple calculator tool
  • Monitors tool execution start and end
  • Tracks tool parameters and results
  • Provides detailed tool usage visibility

πŸ“ Project Structure

6_3_tool_execution_callbacks/
β”œβ”€β”€ README.md              # This file
β”œβ”€β”€ requirements.txt       # Dependencies
β”œβ”€β”€ agent.py              # Agent with tool callbacks
└── app.py                # Streamlit interface

🎯 Learning Objectives

By the end of this tutorial, you'll understand:

  • βœ… Before Tool Callbacks: How to monitor tool execution start
  • βœ… After Tool Callbacks: How to track tool completion
  • βœ… Tool Context: How to access tool and agent information
  • βœ… FunctionTool: How to properly register tools with callbacks
  • βœ… Callback Integration: How to integrate callbacks with agents

πŸš€ Getting Started

Setup

  1. Install dependencies: pip install -r requirements.txt
  2. Set up environment: Create .env with GOOGLE_API_KEY=your_key
  3. Run the app: streamlit run app.py

Test the Agent

# Run the Streamlit app
streamlit run app.py

# Try these test messages:
- "Calculate 15 + 27"
- "What is 100 divided by 4?"
- "Multiply 8 by 12"

πŸ”§ Key Concepts

1. Before Tool Callback

  • Trigger: When tool execution begins
  • Parameters: tool, args, tool_context
  • Use Cases: Log tool usage, validate parameters, record start

2. After Tool Callback

  • Trigger: When tool execution completes
  • Parameters: tool, args, tool_context, tool_response
  • Use Cases: Log results, handle errors, provide feedback

3. Tool Context

  • Agent Information: Access tool_context.agent_name
  • State Management: Use tool_context.state for data sharing
  • Tool Details: Access tool information via tool.name

πŸ” Testing Examples

Basic Tool Usage

User: "Calculate 15 + 27"

πŸ”§ Tool calculator_tool started
πŸ“ Parameters: {'operation': 'add', 'a': 15.0, 'b': 27.0}
πŸ“‹ Agent: tool_execution_demo_agent

βœ… Tool calculator_tool completed
⏱️ Duration: 0.0012s
πŸ“„ Result: 15 + 27 = 42

Error Handling

User: "What is 10 divided by 0?"

πŸ”§ Tool calculator_tool started
πŸ“ Parameters: {'operation': 'divide', 'a': 10.0, 'b': 0.0}
πŸ“‹ Agent: tool_execution_demo_agent

βœ… Tool calculator_tool completed
⏱️ Duration: 0.0008s
πŸ“„ Result: Error: Division by zero

🎯 What Each Metric Tells You

Before Tool Callback Output

  • πŸ”§ Tool Name: Which tool is being executed
  • πŸ“ Parameters: Input parameters passed to the tool
  • πŸ“‹ Agent: Which agent is using the tool

After Tool Callback Output

  • βœ… Completion Status: Tool execution completed successfully
  • ⏱️ Duration: How long the tool took to execute
  • πŸ“„ Result: The output or result from the tool

🎯 Critical Implementation Notes

FunctionTool Requirement

Tools must be wrapped with FunctionTool for callbacks to work:

# βœ… Correct - Use FunctionTool
calculator_function_tool = FunctionTool(func=calculator_tool)
agent = LlmAgent(tools=[calculator_function_tool], ...)

# ❌ Incorrect - Raw function won't trigger callbacks
agent = LlmAgent(tools=[calculator_tool], ...)

Callback Signatures

Use the correct parameter order for tool callbacks:

# βœ… Correct signatures
def before_tool_callback(tool: BaseTool, args: dict, tool_context: ToolContext):
    pass

def after_tool_callback(tool: BaseTool, args: dict, tool_context: ToolContext, tool_response: any):
    pass

Event Loop Completion

Don't break the event loop immediately after is_final_response():

# βœ… Do this - allows callbacks to complete
if event.is_final_response() and event.content:
    response_text = event.content.parts[0].text.strip()
    # Don't break - let the loop complete naturally

🎯 Advanced Patterns

Multiple Tools

Register multiple tools with the same callbacks:

def weather_tool(city: str) -> str:
    return f"Weather in {city}: Sunny, 25Β°C"

def calculator_tool(operation: str, a: float, b: float) -> str:
    # ... implementation

# Register multiple tools
weather_function_tool = FunctionTool(func=weather_tool)
calculator_function_tool = FunctionTool(func=calculator_tool)

agent = LlmAgent(
    name="multi_tool_agent",
    model="gemini-3-flash-preview",
    tools=[calculator_function_tool, weather_function_tool],
    before_tool_callback=before_tool_callback,
    after_tool_callback=after_tool_callback
)

Parameter Validation

Implement validation in before_tool_callback:

def before_tool_callback(tool: BaseTool, args: dict, tool_context: ToolContext):
    tool_name = tool.name
    
    # Validate calculator tool parameters
    if tool_name == "calculator_tool":
        if "operation" not in args:
            print("⚠️ Warning: Missing operation parameter")
        if "a" not in args or "b" not in args:
            print("⚠️ Warning: Missing numeric parameters")
    
    print(f"πŸ”§ Tool {tool_name} started")
    print(f"πŸ“ Parameters: {args}")
    return None

Result Modification

Modify tool results in after_tool_callback:

def after_tool_callback(tool: BaseTool, args: dict, tool_context: ToolContext, tool_response: any):
    tool_name = tool.name
    
    # Add context to calculator results
    if tool_name == "calculator_tool" and "result" in tool_response:
        operation = args.get("operation", "unknown")
        tool_response["context"] = f"Performed {operation} operation"
    
    print(f"βœ… Tool {tool_name} completed")
    print(f"πŸ“„ Result: {tool_response}")
    return tool_response  # Return modified response

πŸ”— Next Steps

After completing this tutorial, you'll be ready for:

πŸ“š Additional Resources

Ingestion metadata

Source catalog
awesome-llm-apps
Repository
Shubhamsaboo/awesome-llm-apps Β· main
File path
ai_agent_framework_crash_course/google_adk_crash_course/6_callbacks/6_3_tool_execution_callbacks/README.md
Last refreshed
7/24/2026, 3:00:13 AM (52m ago)
Refresh schedule
Daily Β· 03:00 UTC
Dedupe status
Unique Β· deduped by (source, url)