# ストリーミング (/ja/docs/concepts/streaming)

Server-Sent Eventsで、生成されたトークンを順次受け取ります。



`"stream": true`を設定すると、レスポンスは1つのJSON本文ではなく[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events)として届きます。イベントの構造はAPI形式ごとに異なります。

| エンドポイント                | ストリームの形式                                                            |
| ---------------------- | ------------------------------------------------------------------- |
| `/v1/chat/completions` | `chat.completion.chunk`オブジェクトの`data:`行。最後は`data: [DONE]`            |
| `/v1/responses`        | `response.output_text.delta`や`response.completed`などの型付きイベント         |
| `/v1/messages`         | Anthropicのイベント：`message_start`、`content_block_delta`、`message_stop` |

## ストリームでのトークン使用量 [#token-usage-in-streams]

Chat Completionsでは、`"stream_options": { "include_usage": true }`を追加すると、最後のチャンクでトークン数を受け取れます。ResponsesとMessagesのストリームは、終了時のイベントで使用量を返します。

## 例 [#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>
