You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 25, 2026. It is now read-only.
Repository navigation
Commit 55d9697
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: content/Guides/agent-streaming.mdx
+95-13Lines changed: 95 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,11 +188,11 @@ Chain as many agents as you like; each one can inspect, transform, or just relay
188
188
189
189
## Low-Level Stream Control
190
190
191
-
For advanced use cases, you can use `context.stream.create()` to create and manage streams directly. This gives you fine-grained control over stream creation, data flow, and background processing.
191
+
For advanced use cases, you can createand manage streams directly with `context.stream.create()`, giving you fine-grained control over stream creation, data flow, and background processing.
192
192
193
193
### Creating Streams with `context.stream.create`
194
194
195
-
The `context.stream.create()`method creates a named, writable stream that returns immediately, allowing for non-blocking operation and background data processing.
195
+
This method returns a Promise that resolves to a named, writable stream. The stream is created immediately, enabling background data processing without blocking the response:
@@ -239,15 +238,15 @@ export default async function Agent(
239
238
240
239
### Manual Stream Writing
241
240
242
-
You can also write to streams manually for complete control over the data flow:
241
+
You can write to streams manually for complete control over the data flow. The Stream API provides a simple `write()` method that handles writer management automatically:
@@ -281,6 +283,84 @@ export default async function Agent(
281
283
}
282
284
```
283
285
286
+
**Writing Patterns**
287
+
288
+
The Stream API supports two writing patterns:
289
+
290
+
-**`stream.write()`** - Simplified API that handles writer acquisition and locking automatically. Recommended for most use cases.
291
+
-**`stream.getWriter()`** - Direct access to the underlying WritableStream writer for scenarios requiring precise control over the writer lifecycle (e.g., sharing a single writer across multiple operations or custom error handling).
292
+
293
+
**Tracking Stream Progress**
294
+
295
+
The `bytesWritten` property provides real-time visibility into stream progress:
Enable compression for highly compressible data formats:
358
+
- JSON APIs and structured data streams
359
+
- Text-based logs and documents
360
+
- CSV files and tabular data
361
+
362
+
The `bytesWritten` property tracks uncompressed bytes, while clients receive gzip-encoded responses that are automatically decompressed by standard HTTP clients.
363
+
284
364
### Stream as Direct Response
285
365
286
366
You can return a stream directly from your agent handler, which will automatically redirect clients to the stream URL:
@@ -293,7 +373,7 @@ export default async function Agent(
0 commit comments