Session Summary Metrics

Track session summary model token usage under the session_summary_model detail key.

1"""Session summary model token usage tracked under the "session_summary_model" detail key."""
2
3from kern.agent import Agent
4from kern.db.postgres import PostgresDb
5from kern.models.openai import OpenAIChat
6from kern.session.summary import SessionSummaryManager
7from rich.pretty import pprint
8
9db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")
10
11agent = Agent(
12 model=OpenAIChat(id="gpt-5.1"),
13 session_summary_manager=SessionSummaryManager(
14 model=OpenAIChat(id="gpt-4o-mini"),
15 ),
16 enable_session_summaries=True,
17 db=db,
18 session_id="session-summary-metrics-demo",
19)
20
21if __name__ == "__main__":
22 # First run
23 run_response_1 = agent.run("My name is Alice and I work at Google.")
24 print("=" * 50)
25 print("RUN 1 METRICS")
26 print("=" * 50)
27 pprint(run_response_1.metrics)
28
29 # Second run - triggers session summary
30 run_response_2 = agent.run("I also enjoy hiking on weekends.")
31 print("=" * 50)
32 print("RUN 2 METRICS")
33 print("=" * 50)
34 pprint(run_response_2.metrics)
35
36 print("=" * 50)
37 print("MODEL DETAILS (Run 2)")
38 print("=" * 50)
39 if run_response_2.metrics and run_response_2.metrics.details:
40 for model_type, model_metrics_list in run_response_2.metrics.details.items():
41 print(f"\n{model_type}:")
42 for model_metric in model_metrics_list:
43 pprint(model_metric)
44
45 print("=" * 50)
46 print("SESSION METRICS (accumulated)")
47 print("=" * 50)
48 session_metrics = agent.get_session_metrics()
49 if session_metrics:
50 pprint(session_metrics)

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