Reduce perceived response time from 15 seconds to 200 milliseconds. Master Server-Sent Events, WebSocket streaming, and real-time AI integration.
Without streaming, users stare at a loading spinner for 10-30 seconds. With streaming, they see the first word in under 300ms. The psychological difference is enormous — it transforms AI from "slow" to "instant."
from openai import OpenAI client = OpenAI(api_key="YOUR_KEY", base_url="https://tokenease.io/v1") stream = client.chat.completions.create( model="deepseek-tc", messages=[{"role": "user", "content": "Explain streaming APIs"}], stream=True # Enable streaming ) for chunk in stream: content = chunk.choices[0].delta.content if content: print(content, end="", flush=True)
const response = await fetch("https://tokenease.io/v1/chat/completions", { method: "POST", headers: { "Authorization": "Bearer YOUR_KEY", "Content-Type": "application/json" }, body: JSON.stringify({ model: "k3", messages: [{role: "user", content: "Hello!"}], stream: true }) }); const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); // Parse SSE format: data: {...} const lines = chunk.split('\\n'); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data === '[DONE]') continue; const json = JSON.parse(data); const content = json.choices?.[0]?.delta?.content; if (content) console.log(content); } } }
Streaming AI APIs use a protocol called Server-Sent Events (SSE). It's a simple text-based format over HTTP:
data: {"choices":[{"delta":{"content":"Hello"}}]}
data: {"choices":[{"delta":{"content":" world"}}]}
data: {"choices":[{"delta":{"content":"!"}}]}
data: [DONE]
Each data: line is a JSON chunk. The client parses each chunk and appends the content to the UI. The connection stays open until [DONE] is sent.
Chatbots, writing assistants, coding copilots — any interface where users wait for text output should stream. The perceived speed improvement is dramatic.
Network hiccups happen. Implement reconnection logic: store the conversation state, and on reconnect, send a new request with the same messages plus any already-received content.
If the user is typing in a chat interface, don't send a new API request on every keystroke. Debounce by 300-500ms after the last keystroke before calling the API.
Even with streaming, there's a 200-500ms delay before the first token. Show a "thinking..." or typing indicator during this window so users know the system is working.
Some models send very small chunks (1-2 characters). Buffer chunks until you have 3-5 characters before updating the DOM to avoid excessive re-renders.
For background processing, data pipelines, or bulk operations where no human is waiting, non-streaming is simpler and slightly more efficient.
| Model | Streaming | First Token | Tokens/Second |
|---|---|---|---|
| Kimi K3 | Yes | 180-350ms | 85-120 |
| DeepSeek V4 | Yes | 200-400ms | 75-110 |
| Doubao Pro | Yes | 250-500ms | 90-130 |
| GLM-5 | Yes | 400-800ms | 55-85 |
| Qwen-Plus | Yes | 300-600ms | 60-90 |
| GPT-4o | Yes | 300-500ms | 60-100 |
All models on TokenEase support streaming via the OpenAI-compatible /v1/chat/completions endpoint.
stream: true in the request body. Without it, the API returns a single JSON response.
[DONE] marker. Some clients crash when trying to JSON-parse [DONE].
role: "assistant" with no content.
One API key. All models. Streaming enabled by default. Try it free.
Get Free API Key