Streaming
Receive responses token-by-token as Server-Sent Events by setting stream: true.
Set stream: true to receive the response token-by-token as Server-Sent Events (SSE). This lets you display text as it arrives rather than waiting for the full response.
How it works
When streaming is enabled, the API returns a series of SSE lines. Each line begins with data: followed by a JSON chunk. The text fragment for each chunk lives in choices[0].delta.content. The stream ends with a final data: [DONE] sentinel line, which you should skip rather than parse.
Examples
import Tchavi from '@tchavi/sdk';
const client = new Tchavi({ apiKey: process.env.TCHAVI_API_KEY });
const stream = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Tell me a short story.' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
}Streaming uses the same OpenAI-compatible request format as a normal call — you only add stream: true.
See Chat Completions for the full request and response reference.