# Streaming (/id/docs/concepts/streaming)

Terima token saat sedang dibuat, lewat server-sent events.



Atur `"stream": true`, dan respons akan datang sebagai [server-sent events](https://developer.mozilla.org/docs/Web/API/Server-sent_events), bukan satu body JSON. Setiap format tetap memakai bentuk event-nya sendiri:

| Endpoint               | Format stream                                                                 |
| ---------------------- | ----------------------------------------------------------------------------- |
| `/v1/chat/completions` | Baris `data:` berisi objek `chat.completion.chunk`, diakhiri `data: [DONE]`   |
| `/v1/responses`        | Event bertipe, misalnya `response.output_text.delta` dan `response.completed` |
| `/v1/messages`         | Event Anthropic: `message_start`, `content_block_delta`, `message_stop`       |

## Pemakaian token saat streaming [#token-usage-in-streams]

Untuk chat completions, tambahkan `"stream_options": { "include_usage": true }` supaya kamu mendapat chunk terakhir berisi jumlah token. Stream Responses dan Messages melaporkan pemakaian di event penutupnya.

## Contoh [#example]

<Tabs items="[&#x22;Python&#x22;, &#x22;TypeScript&#x22;]">
  <Tab value="Python">
    ```python
    stream = client.chat.completions.create(
        model="deepseek-v4-1-flash",
        messages=[{"role": "user", "content": "Write a haiku about rivers."}],
        stream=True,
    )
    for chunk in stream:
        if chunk.choices and chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)
    ```
  </Tab>

  <Tab value="TypeScript">
    ```ts
    const stream = await client.chat.completions.create({
      model: "deepseek-v4-1-flash",
      messages: [{ role: "user", content: "Write a haiku about rivers." }],
      stream: true,
    });
    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
    }
    ```
  </Tab>
</Tabs>
