Streaming Agent Responses
Introduction & Context
Welcome back! In the previous lesson, you learned how to structure your agent's output using Zod schemas and the outputType parameter. This made your agent's responses more predictable and type-safe, allowing you to confidently access specific fields in the agent's output — an essential skill for building robust JavaScript applications.
Now, let's take the next step: making your agent-powered applications more responsive and interactive. In many real-world scenarios — such as chatbots, web apps, or interactive tools — waiting for an agent to complete its entire response before showing anything to the user can make your application feel slow or unresponsive. This is where streaming execution comes in. While the standard async execution waits for the complete response, streaming allows you to process and display the agent's output as it's being generated, creating a smoother and more engaging user experience.
In this lesson, you'll learn the differences between non-streaming and streaming execution modes, and how to implement streaming in your JavaScript applications. By the end, you'll be able to build applications that feel fast and interactive, even when working with complex AI agents.
Understanding Non-Streaming vs Streaming Execution
The OpenAI Agents SDK for JavaScript provides two ways to handle agent responses: non-streaming (default) and streaming execution. Both are asynchronous operations (using async/await), but they differ in how and when you receive the agent's output.
Non-streaming execution is what you've been using so far. When you call await run(agent, input), your code waits for the agent to completely finish generating its response before returning the result. This is simple and works well for many use cases, but it means users have to wait for the entire response before seeing anything.
Streaming execution allows you to receive and process the agent's output as it's being generated, token by token or chunk by chunk. This creates a more interactive experience, similar to how ChatGPT displays text as it "types" out responses. To enable streaming, you simply add { stream: true } to your run call.
Here's a quick comparison:
| Aspect | Non-Streaming | Streaming |
|---|---|---|
| Method | await run(...) | await run(..., { stream: true }) |
| Response timing | All at once when complete | Incrementally as generated |
| Result type | RunResult | StreamedRunResult |
| User experience | Wait, then see full response | See response appearing live |
| Use cases | APIs, batch processing, simple apps | Chat UIs, live demos, interactive apps |
Think of streaming like watching a video that loads progressively versus downloading the entire file before you can watch it — streaming provides immediate feedback and keeps users engaged throughout the process.
