> ## Documentation Index
> Fetch the complete documentation index at: https://docs.neosantara.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming Respons

> Aliran token inkremental real-time menggunakan protokol Server-Sent Events (SSE).

Streaming pada endpoint `/v1/chat/completions` memungkinkan aplikasi Anda menampilkan token teks seketika saat dihasilkan oleh model AI, memangkas *perceived latency* untuk antarmuka chat dan CLI.

```python Python (OpenAI SDK) icon="python" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
from openai import OpenAI
import os

client = OpenAI(
    base_url="https://api.neosantara.xyz/v1",
    api_key=os.environ["NEOSANTARA_API_KEY"]
)

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Tuliskan panduan arsitektur microservices."}],
    stream=True
)

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

```javascript TypeScript (Node.js) icon="js" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.neosantara.xyz/v1",
  apiKey: process.env.NEOSANTARA_API_KEY,
});

const stream = await client.chat.completions.create({
  model: "deepseek-v4.1-flash",
  messages: [{ role: "user", content: "Jelaskan prinsip SOLID secara ringkas." }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
console.log();
```

```bash cURL icon="terminal" theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
curl -N -X POST https://api.neosantara.xyz/v1/chat/completions \
  -H "Authorization: Bearer $NEOSANTARA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4.1-flash",
    "messages": [{"role": "user", "content": "Halo"}],
    "stream": true
  }'
```

## Format Event Stream

Setiap chunk dikirim sebagai event SSE dengan format `data: <JSON>`:

```json theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","created":1741764000,"model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"content":"Halo"},"finish_reason":null}]}
```

Ketika generasi token selesai, gateway mengirimkan sinyal penutup:

```text theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
data: [DONE]
```

## Melacak Penggunaan Token saat Streaming

Sertakan parameter `stream_options` untuk menerima total token yang digunakan pada chunk terakhir:

```python theme={"theme":{"light":"catppuccin-latte","dark":"ayu-dark"}}
stream = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Halo"}],
    stream=True,
    stream_options={"include_usage": True}
)

for chunk in stream:
    if chunk.usage:
        print(f"\nTotal token: {chunk.usage.total_tokens}")
    else:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
```

## Langkah Selanjutnya

| Kebutuhan | Panduan |
| :- | :- |
| Panggil Fungsi Eksternal | [Function Calling](https://docs.neosantara.xyz/id/gateway/chat-completions/tool-calling.md) |
| Output JSON Tervalidasi | [Structured Outputs](https://docs.neosantara.xyz/id/gateway/chat-completions/structured-outputs.md) |
| Ekstraksi Pemikiran Model | [Penalaran Model](https://docs.neosantara.xyz/id/gateway/chat-completions/reasoning.md) |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.