"""Capture metrics from streaming responses using yield_run_output=True."""
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.run.agent import RunOutput
from rich.pretty import pprint
agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
)
if __name__ == "__main__":
response = None
for event in agent.run("Count from 1 to 10.", stream=True, yield_run_output=True):
if isinstance(event, RunOutput):
response = event
if response and response.metrics:
print("=" * 50)
print("STREAMING RUN METRICS")
print("=" * 50)
pprint(response.metrics)
print("=" * 50)
print("MODEL DETAILS")
print("=" * 50)
if response.metrics.details:
for model_type, model_metrics_list in response.metrics.details.items():
print(f"\n{model_type}:")
for model_metric in model_metrics_list:
pprint(model_metric)
Run the Example
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/02_agents/14_advanced
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python streaming_metrics.py