Asynchronous and Streamed Agent Execution Modes
Introduction & Context
Welcome back! In the previous lesson, you learned how to structure your agent’s output using Pydantic models and the output_type parameter. This made your agent’s responses more predictable and easier to use in your applications. You also practiced accessing specific fields from the agent’s output, which is a key skill for building robust systems.
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 tools that need to handle multiple users at once — waiting for an agent to finish its work before doing anything else can make your application feel slow or unresponsive. This is where asynchronous and streamed execution modes come in. These modes allow your program to keep working while the agent is thinking, or even to show results as soon as they are available, creating a smoother and more engaging user experience.
In this lesson, you will learn how to run agents asynchronously and how to process their outputs in real time using streaming. By the end, you will be able to build applications that feel fast and interactive, even when working with complex AI agents.
Overview Of Openai Agents SDK Execution Modes
The OpenAI Agents SDK provides three main ways to run agents: synchronous, asynchronous, and streamed execution. You have already used synchronous execution in earlier lessons, where your code waits for the agent to finish before moving on. This is simple and works well for basic scripts, but it can block your program and make it less responsive.
Asynchronous execution allows your program to start an agent task and then do other things while waiting for the result. This is especially useful in applications with user interfaces, web servers, or any situation where you do not want to block the main thread. Streamed execution takes this a step further by letting you process the agent’s output as it is being generated, token by token or event by event. This is ideal for real-time applications, such as chatbots that display text as it is typed out.
Here is a quick comparison of the three modes:
| Mode | Method | Blocking? | Real-Time Output? | Use Case Example |
|---|---|---|---|---|
| Synchronous | Runner.run_sync | Yes | No | Simple scripts, batch jobs |
| Asynchronous | Runner.run | No | No | Web servers, UI apps |
| Streamed | Runner.run_streamed | No | Yes | Chatbots, live dashboards |
By using asynchronous and streamed execution, you can make your applications more efficient and user-friendly.
