SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.3 KB · 17 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-19 (exit 0 in 1.7s)2// Chat Completions streaming with the openai npm client on xAI; shows reasoning_content deltas before content.3import OpenAI from "openai";4import fs from "node:fs";5const client = new OpenAI({ apiKey: process.env.XAI_API_KEY, baseURL: "https://api.x.ai/v1" });6const stream = await client.chat.completions.create({ model: "grok-4.3", messages: [{ role: "user", content: "What is 17*23? Reply with the number only." }], max_completion_tokens: 32, stream: true, stream_options: { include_usage: true } });7let text = "", reasoning = "", usage: any = null;8for await (const chunk of stream) {9  const d: any = chunk.choices[0]?.delta;10  if (d?.reasoning_content) reasoning += d.reasoning_content;11  if (d?.content) text += d.content;12  if (chunk.usage) usage = chunk.usage;13}14fs.appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "xai", method: "POST", path: "/v1/chat/completions", status: 200, est_cost_usd: 0.0005, note: "example xai/streaming/chat_stream.ts" }) + "\n");15console.log("reasoning:", reasoning || "(none)", "| text:", text, "| reasoning tokens:", usage?.completion_tokens_details?.reasoning_tokens);16if (!text.includes("391")) throw new Error("unexpected " + text);17