Streaming Responses
Get real-time token-by-token responses for better user experience.
💡Streaming is ideal for chat interfaces where you want to display text as it's generated.
Basic Streaming
typescript
import { getAgent } from '@sekuire/sdk';
const agent = await getAgent('assistant');
// Stream tokens
for await (const chunk of agent.chatStream('Tell me a story')) {
process.stdout.write(chunk);
}
console.log(); // newline at endWeb Streaming (Server-Sent Events)
Stream responses to web clients using SSE:
Express.js
typescript
import express from 'express';
import { getAgent } from '@sekuire/sdk';
const app = express();
app.get('/chat/stream', async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
const agent = await getAgent('assistant');
const message = req.query.message as string;
for await (const chunk of agent.chatStream(message)) {
res.write(`data: ${JSON.stringify({ chunk })}\n\n`);
}
res.write('data: [DONE]\n\n');
res.end();
});Client-Side Consumption
Consume the SSE stream in a browser:
typescript
// Browser JavaScript
const eventSource = new EventSource('/chat/stream?message=Hello');
eventSource.onmessage = (event) => {
if (event.data === '[DONE]') {
eventSource.close();
return;
}
const { chunk } = JSON.parse(event.data);
// Append to UI
document.getElementById('response').textContent += chunk;
};
eventSource.onerror = () => {
eventSource.close();
};Streaming Options
typescript
for await (const chunk of agent.chatStream('Hello', {
temperature: 0.7,
maxTokens: 500,
stopSequences: ['\n\n'],
})) {
process.stdout.write(chunk);
}