Streaming
Receive real-time streaming responses using Server-Sent Events.
Overview
Enable streaming to receive agent responses token-by-token using Server-Sent Events (SSE). This provides a better user experience for longer responses.
Request
Set stream: true in your chat request:
curl https://api.your-domain.com/v1/chat \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"message": "Explain quantum computing",
"stream": true
}'Response Format
The response is a stream of SSE events:
data: {"type": "start", "conversation_id": "conv_abc123"}
data: {"type": "token", "content": "Quantum"}
data: {"type": "token", "content": " computing"}
data: {"type": "token", "content": " is"}
data: {"type": "token", "content": " a"}
data: {"type": "sources", "sources": ["https://example.com/quantum"]}
data: {"type": "end", "message_id": "msg_xyz789"}Client Implementation
JavaScript
const response = await fetch('https://api.your-domain.com/v1/chat', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Explain quantum computing',
stream: true,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(l => l.startsWith('data: '));
for (const line of lines) {
const data = JSON.parse(line.slice(6));
if (data.type === 'token') {
process.stdout.write(data.content);
}
}
}Event Types
| Type | Description |
|---|---|
start | Stream has started, includes conversation_id |
token | A single token of the response |
sources | Source documents used for the response |
end | Stream complete, includes message_id |
error | An error occurred during generation |