Tool Call Metrics

Track tool execution timing with ToolCallMetrics on each ToolExecution.

1"""Tool execution timing: start_time, end_time, and duration on each ToolExecution."""
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIChat
5from kern.tools.yfinance import YFinanceTools
6from rich.pretty import pprint
7
8agent = Agent(
9 model=OpenAIChat(id="gpt-4o-mini"),
10 tools=[YFinanceTools()],
11 markdown=True,
12)
13
14if __name__ == "__main__":
15 run_output = agent.run("What is the stock price of AAPL and NVDA?")
16
17 # Run-level metrics
18 print("=" * 50)
19 print("RUN METRICS")
20 print("=" * 50)
21 pprint(run_output.metrics)
22
23 # Each tool call carries its own timing metrics
24 print("=" * 50)
25 print("TOOL CALL METRICS")
26 print("=" * 50)
27 if run_output.tools:
28 for tool_call in run_output.tools:
29 print(f"Tool: {tool_call.tool_name}")
30 if tool_call.metrics:
31 pprint(tool_call.metrics)
32 print("-" * 40)
33
34 # Per-model breakdown
35 print("=" * 50)
36 print("MODEL DETAILS")
37 print("=" * 50)
38 if run_output.metrics and run_output.metrics.details:
39 for model_type, model_metrics_list in run_output.metrics.details.items():
40 print(f"\n{model_type}:")
41 for model_metric in model_metrics_list:
42 pprint(model_metric)

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/14_advanced
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python tool_call_metrics.py