Performance on Agent Instantiation with Tool

Example showing how to analyze the runtime and memory usage of an Agent that is using tools.

Create a Python file

1"""Run `uv pip install kern-ai openai memory_profiler` to install dependencies."""
2
3from typing import Literal
4
5from kern.agent import Agent
6from kern.eval.performance import PerformanceEval
7from kern.models.openai import OpenAIResponses
8
9
10def get_weather(city: Literal["nyc", "sf"]):
11 """Use this to get weather information."""
12 if city == "nyc":
13 return "It might be cloudy in nyc"
14 elif city == "sf":
15 return "It's always sunny in sf"
16
17
18tools = [get_weather]
19
20
21def instantiate_agent():
22 return Agent(model=OpenAIResponses(id="gpt-5.2"), tools=tools) # type: ignore
23
24
25instantiation_perf = PerformanceEval(
26 name="Tool Instantiation Performance", func=instantiate_agent, num_iterations=1000
27)
28
29if __name__ == "__main__":
30 instantiation_perf.run(print_results=True, print_summary=True)

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 openai kern-ai memory_profiler

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 performance_instantiation_with_tool.py