Reasoning Stream

Stream reasoning events from o3-mini on a history essay with stream_events enabled.

reasoning_stream.py
"""
Reasoning Stream
================

Demonstrates this reasoning cookbook example.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.run.agent import RunEvent  # noqa


# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------
def run_example() -> None:
    # Create an agent with reasoning enabled
    agent = Agent(
        reasoning_model=OpenAIResponses(
            id="o3-mini",
            reasoning_effort="low",
        ),
        instructions="Think step by step about the problem.",
    )

    prompt = "Analyze the key factors that led to the signing of the Treaty of Versailles in 1919 Discuss the political, economic, and social impacts of the treaty on Germany and how it contributed to the onset of World War II. Provide a nuanced assessment that includes multiple historical perspectives."

    agent.print_response(prompt, stream=True, stream_events=True)

    # Use manual event loop to see all events
    # for run_output_event in agent.run(
    #     prompt,
    #     stream=True,
    #     stream_events=True,
    # ):
    #     if run_output_event.event == RunEvent.run_started:
    #         print(f"\nEVENT: {run_output_event.event}")

    #     elif run_output_event.event == RunEvent.reasoning_started:
    #         print(f"\nEVENT: {run_output_event.event}")
    #         print("Reasoning started...\n")

    #     elif run_output_event.event == RunEvent.reasoning_content_delta:
    #         # This is the NEW streaming event for reasoning content
    #         print(run_output_event.reasoning_content, end="", flush=True)

    #     elif run_output_event.event == RunEvent.reasoning_step:
    #         print(f"\nEVENT: {run_output_event.event}")

    #     elif run_output_event.event == RunEvent.reasoning_completed:
    #         print(f"\n\nEVENT: {run_output_event.event}")

    #     elif run_output_event.event == RunEvent.run_content:
    #         if run_output_event.content:
    #             print(run_output_event.content, end="", flush=True)

    #     elif run_output_event.event == RunEvent.run_completed:
    #         print(f"\n\nEVENT: {run_output_event.event}")


# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    run_example()

OpenAI schedules o3-mini and o4-mini for shutdown on October 23, 2026. The provider lifecycle notice lists their replacements. Check the selected replacement's reasoning and service-tier support before changing the source model ID.

An explicit reasoning_model runs as a separate, tool-free reasoning stage before the main model response. show_full_reasoning=True displays the reasoning data Agno receives; it cannot reveal a provider's private internal trace. Some adapters use the reasoning stage's answer text when separate reasoning content is unavailable. A failed reasoning stage can still be followed by a main-model answer, so a completed run alone does not prove the reasoning stage succeeded.

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 reasoning_stream.py, then run:

python reasoning_stream.py

Full source: cookbook/10_reasoning/models/openai/reasoning_stream.py