Team with Tool Hooks

Attach a logging tool hook to a team and its HackerNews and YFinance member agents to time each tool call.

Use tool hooks with teams and agents to intercept tool calls. This synchronous hook measures ordinary calls and generator consumption, including failures. Team delegation yields events, so its timer ends when that generator finishes or closes.

Code

team_with_tool_hooks.py
import inspect
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_time = time.perf_counter()

    def log_duration():
        duration = time.perf_counter() - start_time
        logger.info(f"Function {function_name} took {duration:.2f} seconds to execute")

    try:
        result = function_call(**arguments)
    except BaseException:
        log_duration()
        raise

    if inspect.isgenerator(result):
        def consume():
            try:
                return (yield from result)
            finally:
                log_duration()
        return consume()

    log_duration()
    return result



# News agent with tool hooks
news_agent = Agent(
    name="News Agent",
    id="news-agent",
    role="Search HackerNews for information",
    model=OpenAIResponses(id="gpt-5.2"),
    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

Create a Python file

Create team_with_tool_hooks.py with the code above.

Set up your virtual environment

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

Install dependencies

uv pip install -U agno openai yfinance

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run Team

python team_with_tool_hooks.py