Streaming Metrics

Capture metrics from streaming responses.

Capture metrics from streaming responses. Use yield_run_output=True to receive a RunOutput at the end of the stream.

streaming_metrics.py
"""
Streaming Metrics
=============================

Demonstrates how to capture metrics from streaming responses.
Use yield_run_output=True to receive a RunOutput at the end of the stream.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.run.agent import RunOutput
from rich.pretty import pprint

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    model=OpenAIChat(id="gpt-5.6-luna"),
)

# ---------------------------------------------------------------------------
# Run Agent (Streaming)
# ---------------------------------------------------------------------------
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

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run the example

Save the code above as streaming_metrics.py, then run:

python streaming_metrics.py

Full source: cookbook/02_agents/14_advanced/streaming_metrics.py