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:

  1. a usage chunk whose choices is empty ([]) and whose usage holds the token counts, sent on every stream, whether or not you set stream_options.include_usage;
  2. the data: [DONE] sentinel, which you should skip rather than parse;
  3. a tchavi event (event: tchavi) whose data: line holds the same object as the tchavi field of a non-streamed response: credits_used, credits_remaining and the model's rates. A stream carries no X-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.

On this page