Performance with Database Logging

Example showing how to store performance evaluation results in the database.

Create a Python file

1"""Example showing how to store evaluation results in the database."""
2
3from kern.agent import Agent
4from kern.db.postgres.postgres import PostgresDb
5from kern.eval.performance import PerformanceEval
6from kern.models.openai import OpenAIResponses
7
8
9# Simple function to run an agent which performance we will evaluate
10def run_agent():
11 agent = Agent(
12 model=OpenAIResponses(id="gpt-5.2"),
13 system_message="Be concise, reply with one sentence.",
14 )
15 response = agent.run("What is the capital of France?")
16 print(response.content)
17 return response
18
19
20# Setup the database
21db_url = "postgresql+psycopg://ai:ai@localhost:5432/ai"
22db = PostgresDb(db_url=db_url, eval_table="eval_runs_cookbook")
23
24simple_response_perf = PerformanceEval(
25 db=db, # Pass the database to the evaluation. Results will be stored in the database.
26 name="Simple Performance Evaluation",
27 func=run_agent,
28 num_iterations=1,
29 warmup_runs=0,
30)
31
32if __name__ == "__main__":
33 simple_response_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 psycopg

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