คำตอบสั้น
จะสตรีม 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 -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
}'
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")
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 และจำนวนด้านล่างเป็นเพียงตัวอย่าง
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 ระบุไว้ 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 แสดง loop แบบไม่สตรีม ซึ่งเริ่มต้นได้ง่ายกว่า
ทำไมในการเรียกแบบสตรีมจึงไม่ได้รับ usage
เพิ่ม "stream_options": {"include_usage": true} หากไม่มี สตรีมจะไม่มีจำนวนโทเค็น แต่แดชบอร์ดยังคงแสดงจำนวนเหล่านั้น