# Useful but Little-Known Features of OpenAI Agent SDK
๐ Want to stream progress in real time while a nested agent tool is running?
With the `on_stream` callback, you can receive events from child agent executions in real time on the parent side.
๐ Title: Streaming Nested Agent Runs
๐ URL:
๐งฉ Overview
When converting an agent to a tool with `as_tool()`, you can set a callback function on the `on_stream` parameter. This callback receives `AgentToolStreamEvent` and can process events in real time during child agent execution, including `raw_response_event` and `run_item_stream_event`. The event types mirror the standard streaming format, so you can reuse existing streaming handlers.
๐ How to use it
```python
from agents import Agent, Runner
research_agent = Agent(
name="researcher",
instructions="Research the topic in detail",
)
# Streaming callback
async def handle_stream(event):
# raw_response_event: model responses
# run_item_stream_event: tool calls, etc.
if hasattr(event, 'data'):
print(f"[Researching] {")
parent = Agent(
name="coordinator",
tools=[
research_agent.as_tool(
tool_name="research",
tool_description="Research a topic",
on_stream=handle_stream,
),
],
)
# Streamed execution
async for event in "Research the latest AI trends"):
print(event)
```
๐ Building it into production
ใปDisplay child agent processing progress in the UI in real time so users never feel stuck waiting
ใปUse `raw_response_event` to incrementally display generated text
ใปLog tool execution status via `run_item_stream_event`
ใปReuse existing streaming UI components as-is
๐ก Use cases
๐ฅ Real-time UI display of multi-agent processing progress
๐ Typewriter-style incremental display of child agent responses
๐ Visualizing a research agent's search and analysis process
๐ Providing users with intermediate feedback during long-running tasks
โ ๏ธ Watch out
The `on_stream` callback runs within the child agent's execution thread, so heavy processing will impact overall agent performance. Keep event handlers lightweight and consider offloading to an async queue if needed. Add guard clauses to ignore unknown event types for forward compatibility.
โจ With streaming, give users a transparent "see inside" experience for multi-agent workflows.
#
OpenAIAgentSDK# #
AIAgent#