Saltearse al contenido

Streaming

Streaming lets your agent send responses token-by-token as the LLM generates them. Users see text appear in real-time instead of waiting for the full response.

StreamSession API

Start a stream with agent.stream():

interface StreamSession {
delta(chunk: string): Promise<void>; // Send a chunk
done(finalPayload: SendPayload): Promise<void>; // Complete the stream
abort(): void; // Cancel the stream
}

Basic Usage

const session = agent.stream(conversationId);
// SDK sends a stream.start frame with an idempotency key
for await (const chunk of llmStream) {
await session.delta(chunk);
// Each delta is independently encrypted and sent
}
await session.done({
type: "text",
content: fullText,
});
// SDK sends stream.done with the full encrypted payload for persistence

With OpenAI

agent.onMessage(async (msg) => {
const session = agent.stream(msg.conversation_id);
const stream = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: msg.content }],
stream: true,
});
let fullText = "";
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content ?? "";
if (delta) {
fullText += delta;
await session.delta(delta);
}
}
await session.done({
type: "text",
content: fullText,
});
});

Aborting a Stream

Cancel a stream if the user sends a new message or an error occurs:

session.abort();
// SDK sends a stream.abort frame; the client discards partial content

Encryption

Each delta frame is independently encrypted. This means each chunk is a self-contained encrypted payload. The final done frame carries the complete encrypted message for server-side persistence.

Limits

ConstraintValue
Maximum stream duration5 minutes
Orphan detection timeout30 seconds (no delta)
Large groups (201-500 members)Streaming disabled

If the server detects a stream timeout, it sends a stream.error frame. The SDK aborts the session and emits the error via onEvent. The agent decides whether to restart.

Next Steps