How to Stream AI Agent Responses in 5 Min

Your agent calls three tools, thinks for 20 seconds, then dumps a wall of text. The user sees nothing until it's done. That's a broken experience. Here's how to stream agent responses -- both text ...

By · · 1 min read
How to Stream AI Agent Responses in 5 Min

Source: DEV Community

Your agent calls three tools, thinks for 20 seconds, then dumps a wall of text. The user sees nothing until it's done. That's a broken experience. Here's how to stream agent responses -- both text tokens and tool call events -- so users see progress in real time. The Code import asyncio from agents import Agent, Runner, function_tool from openai.types.responses import ResponseTextDeltaEvent @function_tool def lookup_price(ticker: str) -> str: """Look up the current price of a stock.""" prices = {"AAPL": "$198.50", "GOOG": "$176.30", "TSLA": "$245.10"} return prices.get(ticker.upper(), f"No data for {ticker}") agent = Agent( name="StockAssistant", instructions="You help users check stock prices. Use the lookup_price tool.", tools=[lookup_price], ) async def main(): result = Runner.run_streamed(agent, input="What's the price of AAPL and GOOG?") async for event in result.stream_events(): if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent): print(eve