Streaming Events¶
Strands Agents SDK provides real-time streaming capabilities that allow you to monitor and process events as they occur during agent execution. This enables responsive user interfaces, real-time monitoring, and custom output formatting.
Strands has two approaches for handling streaming events:
- Async Iterators: Ideal for asynchronous frameworks like FastAPI, aiohttp, or Django Channels
- Callback Handlers: Perfect for synchronous applications and custom event processing
Both methods receive the same event types but differ in their execution model and use cases.
Event Types¶
All streaming methods yield the same set of events:
Lifecycle Events¶
init_event_loop
: True at the start of agent invocation initializingstart_event_loop
: True when the event loop is startingmessage
: Present when a new message is createdevent
: Raw event from the model streamforce_stop
: True if the event loop was forced to stopforce_stop_reason
: Reason for forced stopresult
: The finalAgentResult
Text Generation Events¶
data
: Text chunk from the model's outputdelta
: Raw delta content from the model
Tool Events¶
current_tool_use
: Information about the current tool being used, including:toolUseId
: Unique ID for this tool usename
: Name of the toolinput
: Tool input parameters (accumulated as streaming occurs)
tool_stream_event
: Information about an event streamed from a tool, including:tool_use
: TheToolUse
for the tool that streamed the eventdata
: The data streamed from the tool
Reasoning Events¶
reasoning
: True for reasoning eventsreasoningText
: Text from reasoning processreasoning_signature
: Signature from reasoning processredactedContent
: Reasoning content redacted by the model
Quick Examples¶
Async Iterator Pattern¶
async for event in agent.stream_async("Calculate 2+2"):
if "data" in event:
print(event["data"], end="")
Callback Handler Pattern¶
def handle_events(**kwargs):
if "data" in kwargs:
print(kwargs["data"], end="")
agent = Agent(callback_handler=handle_events)
agent("Calculate 2+2")
Next Steps¶
- Learn about Async Iterators for asynchronous streaming
- Explore Callback Handlers for synchronous event processing
- See the Agent API Reference for complete method documentation