Working with data
Streams
Ordered, durable output that clients read while it is being written.
Planned public API. Streams run in production today: our own AI features use them for agent output. The
runtime.streams and db.streams helpers on this page are not available to your code yet.A stream is an append-only sequence of chunks. An action writes it; any number of clients read it from the start or from where they left off, live, while it is still being written. As it grows it is stored in S3 in parts, so it can be replayed later.
Use streams for output that arrives over time: model tokens, logs, progress.
Write, in an action
async execute({ input, runtime }) {
const stream = await runtime.streams.create({ name: `report-${input.reportId}` });
for await (const chunk of generateReport(input)) {
await stream.append(chunk);
}
await stream.done();
return { streamId: stream.id };
}Appending several chunks at once sends them in one request:
await stream.append(["first line\n", "second line\n", "third line\n"]);Read, in a client
const reader = db.streams.read({ streamId });
for await (const chunk of reader) {
output.textContent += chunk;
}A reader that reconnects continues from the last chunk it received. A reader that opens after the stream finished gets the whole content.
Permissions
$streams is denied by default: add rules for who may create and read streams. See Permissions.