Team with Tool Hooks

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

1import time
2from typing import Any, Callable, Dict
3
4from kern.agent import Agent
5from kern.models.openai import OpenAIResponses
6from kern.team import Team
7from kern.tools.hackernews import HackerNewsTools
8from kern.tools.yfinance import YFinanceTools
9from kern.utils.log import logger
10
11
12def logger_hook(function_name: str, function_call: Callable, arguments: Dict[str, Any]):
13 """
14 Tool hook that logs function calls and measures execution time.
15
16 Args:
17 function_name: Name of the function being called
18 function_call: The actual function to call
19 arguments: Arguments passed to the function
20
21 Returns:
22 The result of the function call
23 """
24 if function_name == "delegate_task_to_member":
25 member_id = arguments.get("member_id")
26 logger.info(f"Delegating task to member {member_id}")
27
28 # Start timer
29 start_time = time.time()
30 result = function_call(**arguments)
31 # End timer
32 end_time = time.time()
33 duration = end_time - start_time
34 logger.info(f"Function {function_name} took {duration:.2f} seconds to execute")
35 return result
36
37
38# News agent with tool hooks
39news_agent = Agent(
40 name="News Agent",
41 id="news-agent",
42 role="Search HackerNews for information",
43 model=OpenAIResponses(id="gpt-5.2"),
44 tools=[HackerNewsTools(cache_results=True)],
45 instructions=[
46 "Find information about the company on HackerNews",
47 ],
48 tool_hooks=[logger_hook],
49)
50
51# Finance agent with tool hooks
52finance_agent = Agent(
53 name="Finance Agent",
54 id="finance-agent",
55 role="Get stock prices and financial data",
56 model=OpenAIResponses(id="gpt-5.2"),
57 tools=[YFinanceTools(cache_results=True)],
58 instructions=[
59 "Get stock prices and financial information",
60 ],
61 tool_hooks=[logger_hook],
62)
63
64# Create team with tool hooks
65research_team = Team(
66 name="Research Team",
67 model=OpenAIResponses(id="gpt-5.2"),
68 members=[
69 news_agent,
70 finance_agent,
71 ],
72 markdown=True,
73 instructions=[
74 "You are a team that researches companies.",
75 "Use the news agent for HackerNews discussions and finance agent for stock data.",
76 ],
77 show_members_responses=True,
78 tool_hooks=[logger_hook],
79)
80
81if __name__ == "__main__":
82 research_team.print_response(
83 "Research NVIDIA - get the stock price and find any HackerNews discussions.",
84 stream=True,
85 )

Usage

Create a Python file

Create team_with_tool_hooks.py with the code above.

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai yfinance

Export your OpenAI API key

1export OPENAI_API_KEY="your_openai_api_key_here"
1$Env:OPENAI_API_KEY="your_openai_api_key_here"

Run Team

1python team_with_tool_hooks.py