Streaming enables agents to return outputs immediately, significantly improving user experience. Instead of waiting for the entire response to be generated, the user can see the response being generated in real-time.
Streaming Responses
In Agency Swarm, streaming is handled through the get_response_stream method. The framework returns StreamEvent objects as they are returned by OpenAI, providing direct access to the underlying streaming events.
Tools can inject events into the parent SSE stream while they execute. This is especially useful when a tool runs a sub-agent or a long-running operation and you want the client to receive live progress updates alongside the normal agent output.
Accessing the Streaming Context
Inside any BaseTool.run() method, two properties give you access to the live stream:
streaming_ctx is None when the request is not streaming, so always guard before calling put_event.
Any object passed to streaming_ctx.put_event(event) is forwarded into the same stream your client is already consuming. There are three ways to use this, depending on how informative you want the events to be.
Method 1: Raw values
The simplest option — emit any plain value directly. Useful for quick progress strings during development.
On the consumer side:
Method 2: Existing SDK event types
While neither the OpenAI API nor the agents SDK contains event types specifically designed for streaming output from tool execution, you can construct a RawResponsesStreamEvent wrapping a ResponseTextDeltaEvent directly inside the tool. The internal tracking fields (item_id, content_index, etc.) just need placeholder values — the consumer’s existing delta-handling code treats it identically to a normal LLM text delta.
On the consumer side, these events are indistinguishable from regular LLM text deltas unless you inspect the item_id you set on the event:
Method 3: Custom event model
For the most informative output you can construct your own class that includes structured metadata — status, tool call ID for correlation, error details, etc. No built-in SDK type covers tool execution progress, so this is the recommended approach for production use, but it will require explicit checks on the consumer side to separate these events from standard SDK events.
On the consumer side, filter by type:
Regardless of the chosen method, events emitted via put_event() are only visible to the client consuming the stream — the calling agent never sees them. From the agent’s perspective, the tool returns only its final return value, which is what gets added to the conversation history and passed back to the model.