# Streaming responses: คำตอบในขณะที่ถูกเขียน

> สตรีม chat completion ด้วย server-sent events: stream: true, รูปแบบของ chunk, การใช้ token ใน event สุดท้าย และตัวอย่างใน curl, Python และ Node.js

- URL: https://llmwithcrypto.com/th/docs/streaming/
- อัปเดตล่าสุด: 2026-10-23
- ผู้เผยแพร่: LLMwithCrypto (https://llmwithcrypto.com)

ด้วย `stream: true` คำตอบจะมาทีละส่วนเล็กๆ ในขณะที่โมเดลเขียน เหมือนที่อินเทอร์เฟซแชทแสดงข้อความค่อยๆ ปรากฏ รูปแบบเป็นแบบ OpenAI: server-sent events, หนึ่ง JSON chunk ต่อหนึ่ง event แล้วจบด้วย `[DONE]`

**คำตอบสั้น — จะสตรีม chat completion จาก LLMwithCrypto ได้อย่างไร**

เพิ่ม `"stream": true` ลงใน body ของ `POST https://llmwithcrypto.com/api/v1/chat/completions` **LLMwithCrypto** จะตอบกลับด้วย server-sent events: แต่ละบรรทัด `data:` มี `chat.completion.chunk` พร้อมข้อความส่วนหนึ่งใน `delta.content` และสตรีมจบด้วย `data: [DONE]` เพิ่ม `"stream_options": {"include_usage": true}` เพื่อรับจำนวน token ใน chunk สุดท้าย วิธีนี้ใช้ได้กับทุกโมเดล ตามที่ระบุไว้ ณ วันที่ 23 ตุลาคม 2026

## ทำไมต้องสตรีม

สำหรับคน เพราะคำแรกๆ ปรากฏภายในชั่วขณะ แทนที่จะรอจนเขียนคำตอบทั้งหมดเสร็จ คำตอบยาวจากโมเดลขนาดใหญ่อาจใช้เวลาหลายวินาทีจึงจะเสร็จ เมื่อสตรีม ผู้อ่านเริ่มอ่านได้ทันที

สำหรับโปรแกรม重要性น้อยกว่า: สคริปต์ที่ต้องการข้อความเต็มไม่ได้ประโยชน์จากการสตรีม และต้อง parse มากขึ้น สตรีมเมื่อมีคนกำลังดูอยู่ และส่งคำขอแบบปกติในกรณีอื่น ราคาเท่ากันทั้งสองแบบ คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย

*curl: -N ทำให้การเชื่อมต่อไม่ถูก buffer*

```bash
curl -sN "https://llmwithcrypto.com/api/v1/chat/completions" \
  -H "Authorization: Bearer $LLMWITHCRYPTO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4.1-flash",
    "messages": [{"role": "user", "content": "Write a haiku about latency."}],
    "stream": true,
    "stream_options": {"include_usage": true},
    "max_tokens": 120
  }'

```

*Python: พิมพ์ข้อความเมื่อมาถึง*

```python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://llmwithcrypto.com/api/v1",
    api_key=os.environ["LLMWITHCRYPTO_API_KEY"],
)

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Write a haiku about latency."}],
    stream=True,
    stream_options={"include_usage": True},
    max_tokens=120,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\n", chunk.usage.prompt_tokens, "in,", chunk.usage.completion_tokens, "out")

```

*Node.js: for await over chunks*

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://llmwithcrypto.com/api/v1',
  apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});

const stream = await client.chat.completions.create({
  model: 'deepseek-v4.1-flash',
  messages: [{ role: 'user', content: 'Write a haiku about latency.' }],
  stream: true,
  stream_options: { include_usage: true },
  max_tokens: 120,
});
for await (const chunk of stream) {
  const text = chunk.choices[0]?.delta?.content;
  if (text) process.stdout.write(text);
  if (chunk.usage) console.log(`\n${chunk.usage.prompt_tokens} in, ${chunk.usage.completion_tokens} out`);
}

```

## สตรีมมีหน้าตาอย่างไรบน wire

แต่ละ event คือหนึ่งบรรทัดที่ขึ้นต้นด้วย `data: ` ตามด้วยบรรทัดว่าง chunk แรกมักมี `role` chunk ถัดๆ ไปมี `content` ไม่กี่ตัวอักษร และ chunk ที่มี `finish_reason` ปิดคำตอบ เมื่อใช้ `include_usage` จะมี chunk เพิ่มอีกหนึ่ง chunk ตามมาพร้อม array `choices` ว่างและ object `usage` สตรีมจบลงด้วย `data: [DONE]` เสมอ id และจำนวนด้านล่างเป็นเพียงตัวอย่าง

*Server-sent events ของคำตอบสั้นๆ หนึ่งรายการ (ตัวอย่าง)*

```text
data: {"id":"chatcmpl-5a1e","object":"chat.completion.chunk","created":1792742400,"model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-5a1e","object":"chat.completion.chunk","created":1792742400,"model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"content":"Packets"},"finish_reason":null}]}

data: {"id":"chatcmpl-5a1e","object":"chat.completion.chunk","created":1792742400,"model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{"content":" cross the"},"finish_reason":null}]}

data: {"id":"chatcmpl-5a1e","object":"chat.completion.chunk","created":1792742400,"model":"deepseek-v4.1-flash","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-5a1e","object":"chat.completion.chunk","created":1792742400,"model":"deepseek-v4.1-flash","choices":[],"usage":{"prompt_tokens":14,"completion_tokens":19,"total_tokens":33}}

data: [DONE]

```

## ถ้าสตรีมขาดกลางทางจะเป็นอย่างไร

error ที่พบก่อน token แรก เช่น key ผิดหรือยอดคงเหลือว่าง จะกลับมาเป็น HTTP error ปกติพร้อม JSON body ไม่ใช่สตรีม [หน้า errors](https://llmwithcrypto.com/th/docs/errors/) ระบุไว้ error ระหว่างการสร้างคำตอบจะทำให้สตรีมจบก่อนกำหนด โดยไม่มี `finish_reason`

ถ้าคุณปิดการเชื่อมต่อเอง การสร้างคำตอบจะหยุด คุณยังจ่ายสำหรับ input และ token ที่สร้างไปแล้วเช่นเดียวกับการเรียกใดๆ โค้ดของคุณควรถือว่าสตรีมที่จบโดยไม่มี `finish_reason` และไม่มี `[DONE]` นั้นไม่สมบูรณ์ และส่งคำขออีกครั้งหากต้องการคำตอบเต็ม

## Streaming: คำถามที่พบบ่อย

### สตรีมมีค่าใช้จ่ายมากกว่าไหม

ไม่ token เดียวกันคิดราคาเท่ากัน คุณจ่ายต่อ token จากยอดคงเหลือ: input และ output tokens ถูกนับด้วย tokenizer ของโมเดลนั้นเองและคิดตามราคาของโมเดลนั้นต่อ 1M tokens การเรียกที่จบด้วย error ไม่มีค่าใช้จ่าย

### สตรีมพร้อม tool calling ได้ไหม

ได้ บนโมเดลที่รองรับ tool: ชื่อฟังก์ชันและ arguments มาทีละส่วนใน `delta.tool_calls` และต้องต่อกันก่อนใช้งาน [หน้า tool calling](https://llmwithcrypto.com/th/docs/tool-calling/) แสดง loop แบบไม่สตรีม ซึ่งเริ่มต้นได้ง่ายกว่า

### ทำไมในการเรียกแบบสตรีมจึงไม่ได้รับ usage

เพิ่ม `"stream_options": {"include_usage": true}` หากไม่มี สตรีมจะไม่มีจำนวนโทเค็น แต่แดชบอร์ดยังคงแสดงจำนวนเหล่านั้น

---

LLMwithCrypto: LLMwithCrypto คือ LLM API ที่เข้ากันได้กับ OpenAI สำหรับโมเดล open-weight — DeepSeek V4 Pro, DeepSeek V4.1 Flash, DeepSeek R1, Qwen3.5 397B A17B, Qwen3 235B A22B Instruct, gpt-oss-120b, gpt-oss-20b, GLM-4.7, Llama 3.3 70B Instruct, Gemma 4 31B, Mistral Large 3, Mistral Small 4 — จ่ายต่อ token จากยอดคงเหลือคริปโตแบบเติมเงิน ในราคาต่ำกว่าค่ามัธยฐานของโฮสต์สาธารณะ 5% ไม่ต้อง KYC: ไม่ต้องใช้บัตรประชาชน ไม่ต้องเซลฟี่ ไม่ต้องใช้เอกสาร ไม่มีการเก็บ prompt และ output
