Skip to main content
This example demonstrates how to use tool hooks with teams and agents for intercepting and monitoring tool function calls, providing logging, timing, and other observability features.

Code

team_with_tool_hooks.py
import time
from typing import Any, Callable, Dict

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.team import Team
from agno.tools.hackernews import HackerNewsTools
from agno.tools.yfinance import YFinanceTools
from agno.utils.log import logger


def logger_hook(function_name: str, function_call: Callable, arguments: Dict[str, Any]):
    """
    Tool hook that logs function calls and measures execution time.

    Args:
        function_name: Name of the function being called
        function_call: The actual function to call
        arguments: Arguments passed to the function

    Returns:
        The result of the function call
    """
    if function_name == "delegate_task_to_member":
        member_id = arguments.get("member_id")
        logger.info(f"Delegating task to member {member_id}")

    # Start timer
    start_time = time.time()
    result = function_call(**arguments)
    # End timer
    end_time = time.time()
    duration = end_time - start_time
    logger.info(f"Function {function_name} took {duration:.2f} seconds to execute")
    return result


# News agent with tool hooks
news_agent = Agent(
    name="News Agent",
    id="news-agent",
    role="Search HackerNews for information",
    tools=[HackerNewsTools(cache_results=True)],
    instructions=[
        "Find information about the company on HackerNews",
    ],
    tool_hooks=[logger_hook],
)

# Finance agent with tool hooks
finance_agent = Agent(
    name="Finance Agent",
    id="finance-agent",
    role="Get stock prices and financial data",
    model=OpenAIResponses(id="gpt-5.2"),
    tools=[YFinanceTools(cache_results=True)],
    instructions=[
        "Get stock prices and financial information",
    ],
    tool_hooks=[logger_hook],
)

# Create team with tool hooks
research_team = Team(
    name="Research Team",
    model=OpenAIResponses(id="gpt-5.2"),
    members=[
        news_agent,
        finance_agent,
    ],
    markdown=True,
    instructions=[
        "You are a team that researches companies.",
        "Use the news agent for HackerNews discussions and finance agent for stock data.",
    ],
    show_members_responses=True,
    tool_hooks=[logger_hook],
)

if __name__ == "__main__":
    research_team.print_response(
        "Research NVIDIA - get the stock price and find any HackerNews discussions.",
        stream=True,
    )

Usage

1

Create a Python file

Create team_with_tool_hooks.py with the code above.
2

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate
3

Install dependencies

uv pip install -U agno openai yfinance
4

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"
5

Run Team

python team_with_tool_hooks.py