Stream responses
Stream an agent’s output as it happens instead of waiting for the final response: text tokens, tool calls, and lifecycle events, as they occur. This is what drives responsive user interfaces and real-time monitoring.
Strands emits the same set of events regardless of how you consume them; you choose the consumption model that fits your application. Async iterators suit asynchronous server frameworks, and callback handlers suit synchronous Python.
Stream an agent’s response
Section titled “Stream an agent’s response”A running stream
Section titled “A running stream”The smallest real thing streaming does is print an agent’s answer token by token as the model produces it. Iterate over the stream and forward the text chunks:
import asynciofrom strands import Agent
agent = Agent(callback_handler=None)
async def main(): async for event in agent.stream_async("Tell me about agents in one sentence."): if "data" in event: print(event["data"], end="", flush=True)
asyncio.run(main())const agent = new Agent({ tools: [notebook] })
for await (const event of agent.stream('Calculate 2+2')) { if ( event.type === 'modelStreamUpdateEvent' && event.event.type === 'modelContentBlockDeltaEvent' && event.event.delta.type === 'textDelta' ) { // Print out the model text delta event data process.stdout.write(event.event.delta.text) }}console.log('\nDone!')Each data chunk is one piece of the model’s output. The event reference lists everything else the stream carries: tool calls, lifecycle signals, and multi-agent coordination.
Where to go next
Section titled “Where to go next”New to streaming? Start with Async Iterators to consume the stream in an async server, or Callback Handlers if your Python application is synchronous. Both receive the same events; the event reference is the complete list of what you can react to.