クイック回答
LLMwithCryptoでチャット補完をストリーミングするには?
POST https://llmwithcrypto.com/api/v1/chat/completionsのボディに"stream": trueを追加します。するとLLMwithCryptoがサーバー送信イベントで応答します。各data:行にはdelta.contentにテキストの一部を持つchat.completion.chunkが入り、ストリームはdata: [DONE]で終わります。"stream_options": {"include_usage": true}を追加すると、最後のチャンクでトークン数が得られます。これは2026年10月23日のドキュメントどおり、すべてのモデルで動作します。
なぜストリーミングするのですか?
人にとっては、回答全体が書き終わるのを待たずに最初の言葉がすぐに現れるからです。大規模モデルの長い回答は完了までに何秒もかかることがありますが、ストリーミングなら読者はすぐに読み始められます。
プログラムにとっては重要性が低くなります。全文が必要なスクリプトはストリーミングから何も得られず、パースの手間が増えるだけです。人間が見ているときはストリーミングし、それ以外では通常のリクエストを送ってください。価格はどちらも同じです。残高からトークン単位で支払います。入力トークンと出力トークンはモデル自身のトークナイザーでカウントされ、そのモデルの100万トークンあたりの価格で請求されます。エラーで終わった呼び出しは無料です。
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`);
}
ストリームは通信上でどのように見えますか?
各イベントはdata: で始まる1行と、その後に空行が続きます。最初のチャンクは通常roleを持ち、次のチャンクはそれぞれcontentの数文字を持ち、finish_reasonを持つチャンクで回答が締めくくられます。include_usageを使うと、空のchoices配列とusageオブジェクトを持つチャンクがもう1つ続きます。ストリームは常に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]
ストリームが途切れたらどうなりますか?
最初のトークンより前に発生したエラー(APIキーの誤りや残高不足など)は、ストリームではなくJSONボディ付きの通常のHTTPエラーとして返されます。エラーページに一覧があります。生成中のエラーは、finish_reasonなしでストリームを途中で終了させます。
自分で接続を閉じると、生成は停止します。その場合は他の呼び出しと同様に、入力と生成済みのトークンに対して支払います。finish_reasonも[DONE]もなしで終わるストリームは不完全として扱い、全文が必要ならリクエストを再送してください。
ストリーミング: よくある質問
ストリーミングするとコストが増えますか?
いいえ。同じトークンが同じ価格で課金されます。残高からトークン単位で支払います。入力トークンと出力トークンはモデル自身のトークナイザーでカウントされ、そのモデルの100万トークンあたりの価格で請求されます。エラーで終わった呼び出しは無料です。
ツール呼び出しとストリーミングを併用できますか?
はい、ツールをサポートするモデルで可能です。関数名と引数はdelta.tool_callsの中で断片的に届くため、使用前に結合する必要があります。ツール呼び出しのページでは、まず始めるのに簡単な非ストリーミングのループを紹介しています。
ストリーミング呼び出しで使用量が得られないのはなぜですか?
"stream_options": {"include_usage": true}を追加してください。これがないと、ストリームにトークン数が含まれません。ダッシュボードには引き続き表示されます。