Streaming Metrics

Capture metrics from streaming responses using yield_run_output=True.

1"""Capture metrics from streaming responses using yield_run_output=True."""
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIChat
5from kern.run.agent import RunOutput
6from rich.pretty import pprint
7
8agent = Agent(
9 model=OpenAIChat(id="gpt-4o-mini"),
10)
11
12if __name__ == "__main__":
13 response = None
14 for event in agent.run("Count from 1 to 10.", stream=True, yield_run_output=True):
15 if isinstance(event, RunOutput):
16 response = event
17
18 if response and response.metrics:
19 print("=" * 50)
20 print("STREAMING RUN METRICS")
21 print("=" * 50)
22 pprint(response.metrics)
23
24 print("=" * 50)
25 print("MODEL DETAILS")
26 print("=" * 50)
27 if response.metrics.details:
28 for model_type, model_metrics_list in response.metrics.details.items():
29 print(f"\n{model_type}:")
30 for model_metric in model_metrics_list:
31 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 streaming_metrics.py