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."""23from kern.agent import Agent4from kern.models.openai import OpenAIChat5from kern.tools.yfinance import YFinanceTools6from rich.pretty import pprint78agent = Agent(9 model=OpenAIChat(id="gpt-4o-mini"),10 tools=[YFinanceTools()],11 markdown=True,12)1314if __name__ == "__main__":15 run_output = agent.run("What is the stock price of AAPL and NVDA?")1617 # Run-level metrics18 print("=" * 50)19 print("RUN METRICS")20 print("=" * 50)21 pprint(run_output.metrics)2223 # Each tool call carries its own timing metrics24 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)3334 # Per-model breakdown35 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 repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python tool_call_metrics.py