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 Server-Sent Events. Each event's data: line carries a JSON chunk, and the text fragment lives in choices[0].delta.content. The stream ends with three things:
- a usage chunk whose
choicesis empty ([]) and whoseusageholds the token counts, sent on every stream, whether or not you setstream_options.include_usage; - the
data: [DONE]sentinel, which you should skip rather than parse; - a
tchavievent (event: tchavi) whosedata:line holds the same object as thetchavifield of a non-streamed response:credits_used,credits_remainingand the model's rates. A stream carries noX-Credits-*headers, which are sent before the charge is known; this event is where it reports the charge.
OpenAI SDKs stop reading at [DONE], so they never see the tchavi event. A hand-written parser should not assume every chunk has a choices[0]. The usage chunk and the tchavi event have none, as the examples below show.
Examples
import Tchavi from '@tchavi/sdk';
const client = new Tchavi({ apiKey: process.env.TCHAVI_API_KEY });
const stream = await client.chat.completions.create({
model: 'claude-haiku-5-5',
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.