Tool Call Metrics

Access tool execution timing with ToolCallMetrics on each ToolExecution in the run output.

Code

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

Usage

Create a Python file

Create tool_call_metrics.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 Agent

1python tool_call_metrics.py