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."""23from kern.agent import Agent4from kern.db.postgres import PostgresDb5from kern.models.openai import OpenAIChat6from kern.session.summary import SessionSummaryManager7from rich.pretty import pprint89db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")1011agent = 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)2021if __name__ == "__main__":22 # First run23 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)2829 # Second run - triggers session summary30 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)3536 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)4445 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 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 session_summary_metrics.py