Streaming Metrics
Capture metrics from streaming responses using yield_run_output=True.
1"""Capture metrics from streaming responses using yield_run_output=True."""23from kern.agent import Agent4from kern.models.openai import OpenAIChat5from kern.run.agent import RunOutput6from rich.pretty import pprint78agent = Agent(9 model=OpenAIChat(id="gpt-4o-mini"),10)1112if __name__ == "__main__":13 response = None14 for event in agent.run("Count from 1 to 10.", stream=True, yield_run_output=True):15 if isinstance(event, RunOutput):16 response = event1718 if response and response.metrics:19 print("=" * 50)20 print("STREAMING RUN METRICS")21 print("=" * 50)22 pprint(response.metrics)2324 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 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 streaming_metrics.py