Streaming responses

Stream chat completions through TKEN

Use the TKEN OpenAI-compatible gateway for token-by-token user interfaces, CLI output and agent logs while keeping one base URL: https://www.tken.shop/v1.

Node.js stream loop
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.TKEN_API_KEY,
  baseURL: "https://www.tken.shop/v1"
});

const stream = await client.chat.completions.create({
  model: process.env.TKEN_CHAT_MODEL,
  stream: true,
  messages: [{ role: "user", content: "Write three short bullets." }]
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Improve perceived latency
Keep a non-streaming fallback
Log errors without request secrets

cURL smoke test

Test the transport before wiring it into a UI. A streaming route should deliver incremental chunks and close cleanly when the completion is finished.

HTTP stream=true
curl https://www.tken.shop/v1/chat/completions \
  -H "Authorization: Bearer $TKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'$TKEN_CHAT_MODEL'",
    "stream": true,
    "messages": [
      {"role": "user", "content": "Stream a one sentence answer."}
    ]
  }'

Streaming rollout checklist

1. Verify chunk parsing

Confirm your SDK or HTTP parser handles empty deltas, final chunks, disconnects and model-specific stream behavior.

2. Add timeouts and cancellation

User interfaces should support aborting a request and should show a clear error if the stream stalls or the network drops.

3. Keep a normal completion route

Use a non-streaming fallback for batch jobs, retries and clients that cannot consume server-sent events reliably.

Disclosure: TKEN is an independent third-party API gateway, not an official endpoint for OpenAI or other model providers.

Test streaming before a UI rollout

Start with a terminal smoke test, then wire the same https://www.tken.shop/v1 base URL into your application.