Zubnet AIApprendreWiki › Streaming
Using AI

Streaming

Server-Sent Events, Token Streaming
Envoyer la sortie du modèle à l'utilisateur token par token au fur et à mesure qu'elle est générée, plutôt que d'attendre la réponse complète. Le streaming utilise Server-Sent Events (SSE) sur HTTP — la connexion reste ouverte et le serveur pousse chaque nouveau token comme un petit événement. C'est pourquoi tu vois le texte apparaître mot par mot dans les interfaces de chat.

Pourquoi c'est important

Le streaming transforme l'expérience utilisateur. Une réponse qui prend 10 secondes se sent acceptable quand tu la vois se construire mot par mot. La même réponse livrée d'un coup après 10 secondes d'écran blanc se sent cassée. Le streaming laisse aussi les utilisateurs interrompre les mauvaises réponses tôt, sauvant des tokens et de l'argent.

Deep Dive

Technically, streaming uses the stream: true parameter in API calls. The server responds with a stream of SSE events, each containing one or a few tokens plus metadata (like token counts, stop reason). The client reads these events incrementally and renders them. Most SDKs handle the SSE parsing for you, but understanding the underlying mechanism helps when debugging latency issues or building custom streaming UIs.

Streaming Affects Architecture

Streaming isn't just a UI feature — it affects how you build applications. With streaming, you can't post-process the complete response before showing it (since it's not complete yet). If you need to validate, filter, or transform the response, you either process it in chunks (harder) or buffer the full response and show it after (defeating the purpose). Outils like function calling also interact with streaming: the model might stream a tool call, then pause while your code executes the tool, then resume streaming the final answer.

Time to First Token

In a streaming context, the key latency metric is TTFT (Time to First Token) — how long before the first token appears. This depends on prompt processing time (longer prompts take longer to process before generation starts) and server load. TTFT of under 500ms feels instant; over 2 seconds feels sluggish. After the first token, inter-token latency (the gap between successive tokens) determines how smooth the stream looks. Most providers achieve 20–50ms inter-token latency, which looks natural.

Concepts liés

← Tous les termes
← Stochastic Parrot Structured Output →