Switch language

Streaming

Process chat completion tokens in real time using Server-Sent Events.

When stream: true is set, the Bitdeer AI API returns tokens incrementally as Server-Sent Events (SSE). The stream ends with a data: [DONE] message.

Supported models

All text generation models support streaming.

Python

from openai import OpenAI

client = OpenAI(
    base_url="https://api-inference.bitdeer.ai/v1",
    api_key="YOUR_API_KEY",
)

stream = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": "Write a short poem about clouds."}],
    max_tokens=256,
    stream=True,
)

for chunk in stream:
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)

JavaScript / TypeScript

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.BITDEER_API_KEY,
  baseURL: 'https://api-inference.bitdeer.ai/v1',
});

const stream = await client.chat.completions.create({
  model: 'deepseek-ai/DeepSeek-V4-Pro',
  messages: [{ role: 'user', content: 'Write a short poem about clouds.' }],
  max_tokens: 256,
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) process.stdout.write(content);
}

Stream options

Set stream_options.include_usage: true to receive token usage statistics in the final chunk:

stream = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[{"role": "user", "content": "Hello"}],
    max_tokens=256,
    stream=True,
    stream_options={"include_usage": True},
)

The last chunk before [DONE] will contain the full usage object with prompt_tokens, completion_tokens, and total_tokens.

cURL

curl https://api-inference.bitdeer.ai/v1/chat/completions \
  -H "Authorization: Bearer $BITDEER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-ai/DeepSeek-V4-Pro",
    "messages": [{"role": "user", "content": "Write a short poem about clouds."}],
    "max_tokens": 256,
    "stream": true
  }'

Each line in the response is prefixed with data: followed by a JSON chunk, and the stream terminates with data: [DONE].

Last updated on

On this page

Streaming · Bitdeer AI