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."""
2
3from kern.agent import Agent
4from kern.db.postgres import PostgresDb
5from kern.memory.manager import MemoryManager
6from kern.models.openai import OpenAIChat
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-4o-mini"),
13 memory_manager=MemoryManager(model=OpenAIChat(id="gpt-4o-mini"), db=db),
14 update_memory_on_run=True,
15 db=db,
16)
17
18if __name__ == "__main__":
19 run_response = agent.run(
20 "My name is Alice and I work at Google as a senior engineer."
21 )
22
23 print("=" * 50)
24 print("RUN METRICS")
25 print("=" * 50)
26 pprint(run_response.metrics)
27
28 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 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 multi_model_metrics.py