Multi-Model Metrics
Track per-model token usage with memory model breakdown in metrics.details.
1"""Per-model token breakdown: "model" vs "memory_model" in metrics.details."""23from kern.agent import Agent4from kern.db.postgres import PostgresDb5from kern.memory.manager import MemoryManager6from kern.models.openai import OpenAIChat7from rich.pretty import pprint89db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")1011agent = Agent(12 model=OpenAIChat(id="gpt-4o-mini"),13 memory_manager=MemoryManager(model=OpenAIChat(id="gpt-4o-mini"), db=db),14 update_memory_on_run=True,15 db=db,16)1718if __name__ == "__main__":19 run_response = agent.run(20 "My name is Alice and I work at Google as a senior engineer."21 )2223 print("=" * 50)24 print("RUN METRICS")25 print("=" * 50)26 pprint(run_response.metrics)2728 print("=" * 50)29 print("MODEL DETAILS")30 print("=" * 50)31 if run_response.metrics and run_response.metrics.details:32 for model_type, model_metrics_list in run_response.metrics.details.items():33 print(f"\n{model_type}:")34 for model_metric in model_metrics_list:35 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 multi_model_metrics.py