Streaming responses

Stream tokens the moment a model produces them, using Server-Sent Events in the OpenAI format. Every model on MeshTok supports streaming — pass stream: true and parse the data: lines as they arrive.

Why stream? Users see the first token in ~200–800ms instead of waiting for the full response. For long generations this cuts perceived latency by an order of magnitude.

Python (OpenAI SDK)

from openai import OpenAI

client = OpenAI(base_url="https://meshtok.com/v1", api_key="sk-...")

stream = client.chat.completions.create(
    model="deepseek/deepseek-v4-pro",
    messages=[{"role": "user", "content": "Write a haiku about latency."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)

JavaScript / Node (OpenAI SDK)

import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://meshtok.com/v1", apiKey: "sk-..." });

const stream = await client.chat.completions.create({
  model: "bigmodel/glm-5.2",
  messages: [{ role: "user", content: "Write a haiku about latency." }],
  stream: true,
});

for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content ?? "";
  process.stdout.write(delta);
}

cURL (raw SSE)

curl -N https://meshtok.com/v1/chat/completions \
  -H "Authorization: Bearer sk-..." \
  -H "Content-Type: application/json" \
  -d '{"model":"anthropic/claude-sonnet-5","messages":[{"role":"user","content":"Hi"}],"stream":true}'

# data: {"choices":[{"delta":{"content":"Hello"}}]}
# data: {"choices":[{"delta":{"content":" there"}}]}
# data: [DONE]

Parsing the SSE wire format

Each event is a line starting with data: followed by JSON. An empty delta means the role is being set or the model is thinking; a non-empty delta.content is a token. The stream ends with the literal data: [DONE].

// Minimal browser-side parser
const resp = await fetch("/v1/chat/completions", { method: "POST", headers, body, signal });
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buf = "";
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  buf += decoder.decode(value, { stream: true });
  for (const line of buf.split("\n")) {
    if (!line.startsWith("data:")) continue;
    const payload = line.slice(5).trim();
    if (payload === "[DONE]") return;
    const { choices } = JSON.parse(payload);
    appendToken(choices[0].delta.content ?? "");
  }
}
→ Tool calling → Structured outputs Try it in Playground