GenAIHub
← Back to Technical Section

Streaming UX

Real-Time Response Delivery for AI Applications

What is Streaming UX?

Streaming UX refers to the design patterns and implementation techniques for delivering AI responses in real-time, token by token, rather than waiting for complete responses. This dramatically improves perceived performance and user engagement.

πŸ’‘ Key Insight: Studies show streaming reduces perceived latency by 50-70%, even when total response time is the same. Users feel more engaged watching text appear.

⚑

Faster Feel

Immediate feedback

πŸ‘€

Engagement

Users stay focused

πŸ›‘

Control

Cancel anytime

How Streaming Works

Client Browser Server API LLM OpenAI/etc Request stream=true Tokens... SSE/WebSocket Token Stream Hello world ! β–Œ word by word
Server-Sent Events

HTTP streaming over a persistent connection. Simple to implement, widely supported.

WebSockets

Full-duplex communication for bidirectional streaming. Ideal for real-time apps.

Chunked Transfer

HTTP chunked encoding for streaming without content-length. Simple backend setup.

Implementation Examples

Python (OpenAI SDK)

from openai import OpenAI

client = OpenAI()
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
    stream=True  # Enable streaming
)

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

JavaScript (Fetch API)

const response = await fetch('/api/chat', {
    method: 'POST',
    body: JSON.stringify({ message: 'Hello!' })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    const text = decoder.decode(value);
    document.getElementById('output').textContent += text;
}

Essential UX Patterns

Blinking Cursor

Show a blinking cursor (β–Œ) at the end of streaming text to indicate more content coming.

Stop Generation

Provide a clear button to abort generation. Cancel the stream and update UI accordingly.

Auto-Scroll

Keep the latest content visible by auto-scrolling. Pause when user scrolls up manually.

Completion State

Clearly indicate when streaming is complete. Enable copy, regenerate, and other actions.

Performance Comparison

Metric Non-Streaming Streaming
Time to First Token 5-30 seconds 200-500ms
Perceived Latency High Low
User Engagement Users may leave Users stay engaged
Cancellation Must wait or reload Cancel anytime

Implementation Challenges

⚠️ Markdown Rendering

Incomplete markdown during streaming can cause flicker. Buffer and render incrementally.

⚠️ Code Blocks

Syntax highlighting mid-stream is tricky. Wait for complete blocks or use progressive highlighting.

⚠️ Error Handling

Streams can disconnect mid-response. Implement retry logic and partial content recovery.

⚠️ Mobile Performance

Frequent DOM updates can drain battery. Batch updates and use requestAnimationFrame.

Libraries & Tools

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass