> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-docs-scavio-google-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Basic Stream Metrics

> Stream a response, then read run-level and per-message metrics from the last run output.

```python basic_stream_metrics.py theme={null}
"""
Openai Basic Stream Metrics
===========================

Cookbook example for `openai/chat/basic_stream_metrics.py`.
"""

from typing import Iterator  # noqa
from agno.agent import Agent, RunOutputEvent  # noqa
from agno.models.openai import OpenAIChat
from agno.db.in_memory import InMemoryDb

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

agent = Agent(model=OpenAIChat(id="gpt-4o"), db=InMemoryDb(), markdown=True)

# Get the response in a variable
# run_response: Iterator[RunOutputEvent] = agent.run("Share a 2 sentence horror story", stream=True)
# for chunk in run_response:
#     print(chunk.content)

# Print the response in the terminal
agent.print_response("Share a 2 sentence horror story", stream=True)

run_output = agent.get_last_run_output()
print("Metrics:")
print(run_output.metrics)

print("Message Metrics:")
for message in run_output.messages:
    if message.role == "assistant":
        print(message.role)
        print(message.metrics)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `basic_stream_metrics.py`, then run:

    ```bash theme={null}
    python basic_stream_metrics.py
    ```
  </Step>
</Steps>

Full source: [cookbook/90\_models/openai/chat/basic\_stream\_metrics.py](https://github.com/agno-agi/agno/blob/main/cookbook/90_models/openai/chat/basic_stream_metrics.py)
