Fleeexdocs

Streaming

Stream tokens as they're generated — identical to the OpenAI SDK.

Streaming works exactly as in the OpenAI SDK: pass stream: true and iterate the async iterable. The SDK changes nothing about the shape of the stream.

const stream = await client.chat.completions.create({
  model: "anthropic.claude-3-haiku-20240307-v1:0",
  messages: [{ role: "user", content: "Write a haiku." }],
  stream: true,
});
 
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Each chunk is a standard OpenAI chat.completion.chunk. The return type is fully typed: stream: true yields a stream, stream: false (or omitted) yields a single completion — the same overloads as upstream.

Billing on streams

Output length isn't known until the stream ends, so Fleeex reserves a ceiling up front and settles the real cost when the stream finishes — the wallet can never go negative. From the SDK's side there's nothing to do: you just iterate.

Payment errors happen before the first chunk

Solvency is checked before any tokens flow, so a 402 surfaces when you await the create call — not mid-iteration. Handle it the same way as a non-streaming call:

import { PaymentRequiredError } from "@fleeex/sdk";
 
try {
  const stream = await client.chat.completions.create({
    model: "anthropic.claude-3-haiku-20240307-v1:0",
    messages,
    stream: true,
  });
  for await (const chunk of stream) {
    /* … */
  }
} catch (err) {
  if (err instanceof PaymentRequiredError) redirect(err.topupUrl);
  else throw err;
}

See Balance & payments for the full payment flow.