ストリーミング

すべてのプロトコルで使えるServer-Sent Eventsと、ストリームで使用量とコストが届く位置。

Chat Completionsをストリーミングする

stream: trueでSSEが返ります。stream_options: {"include_usage": true}を加えると最後に使用量チャンクが届き、usage.costとトップレベルのproviderが含まれます。

POST/v1/chat/completions
curl -N https://api.experientiallabs.ai/v1/chat/completions \
  -H "Authorization: Bearer <YOUR_API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-6-luna", "stream": true, "stream_options": {"include_usage": true},
       "messages": [{"role": "user", "content": "Count to five."}]}'

プロトコルごとの使用量の届き方

プロトコルストリームの指定使用量とコスト
Chat Completionsstream: truestream_options.include_usageを設定した場合の最終チャンク
Responsesstream: trueresponse.completedイベント
Anthropic Messagesstream: true最後のmessage_delta(確定値)
Gemini:streamGenerateContent?alt=sse最終フレームのusageMetadata
埋め込み非対応1つのJSONボディ
POST/v1/responses
with client.responses.stream(model="gpt-6-luna", input="Count to five.") as s:
    for event in s:
        if event.type == "response.output_text.delta":
            print(event.delta, end="")
    final = s.get_final_response()
    print(final.usage)

ストリーム途中の失敗

フェイルオーバーは、最初の出力差分より前にのみ行われます。出力が始まる前にプロバイダーが停止した場合、ゲートウェイは次のルートへフェイルオーバーし、残りのルートがなければエラー(502 "provider stream failed")を返します。出力が始まった後に停止した場合、別のルートをつなぎ合わせることはなく、プロトコルの終端エラーでストリームを終了します(Chatはerrorフレームの後に[DONE]、Messagesはevent: error、Responsesはresponse.failed)。モデルが何も出力せずにターンを終えた場合は、ヘッダーx-gateway-warning: empty_completion付きの200が返ります。すでにヘッダーを送信済みのストリームでは、この状態は終端フレームにのみ現れます。エラーを参照してください。

HTTP/2 200
x-gateway-warning: empty_completion
x-request-id: 3f2a...