← All news

News / Learn / Working with model APIs

How does streaming work in LLM APIs?

You'll be able to read streamed LLM output as it arrives and explain how it differs from a normal request.

A normal API call waits until the model finishes, then returns the whole answer at once. Streaming sends the answer in small pieces as the model generates them. Your app shows each piece right away, so the user sees text appear word by word.

Streaming makes an app feel fast even when the full answer takes seconds to produce. The first words arrive quickly, so users stay engaged and can stop a bad answer early. It also changes how you handle errors, timeouts, and saving results, so you need to understand it before you build a chat or writing feature.

A model writes its output one token at a time. A token is a small chunk of text, often a word or part of a word. In a standard request, the server collects every token and sends one response when generation ends.

With streaming, the server opens a connection and sends each token, or a small group of tokens, as soon as it is produced. Most providers send these as server-sent events, which is a simple one-way stream over HTTP. Each event carries a small piece of data, and the client reads events in order until the stream signals that it is done.

Your code has to assemble the pieces. You keep a running string, append each new piece to it, and update the screen after each append. The final string is the complete answer. Some streams also send metadata, such as token counts or a stop reason, near the end. Read those fields when they appear, not only the text.

Streaming does not make the model faster. The total time to finish is about the same. What changes is time to first token, which is how long the user waits before anything appears. Streaming lowers that wait, so the app feels responsive.

A minimal streaming loop in Python. The exact client library and event shape vary by provider, so check your provider's docs for the field names.

import sys

def stream_answer(client, prompt):
    parts = []
    with client.stream(prompt) as stream:
        for chunk in stream:
            text = chunk.text or ""
            parts.append(text)
            sys.stdout.write(text)
            sys.stdout.flush()
    full_answer = "".join(parts)
    return full_answer

A short sketch to show the idea. Check the official docs of the tool you use.

  • Assuming every chunk contains text Some events carry only metadata or control signals. Check each event's type and skip the ones without text.
  • Saving only partial output on failure If the connection drops midway, the stored text is incomplete. Mark the result as partial or retry the request rather than treating it as final.
  • Forgetting to handle timeouts A stream can stall without closing. Set a timeout for silence between chunks and cancel the request if nothing arrives.
  • Expecting streaming to cut total cost Streaming changes when output appears, not how many tokens are billed. Budget for the same tokens as a normal call.

Learn server-sent events and how HTTP keeps a connection open. Then study how your framework wraps streams, and how to cancel a stream cleanly when a user leaves the page.

Does streaming cost more?

Generally no. You pay for the same tokens either way. Check your provider's pricing page for current rates, since they change.

Why do I see text in odd-sized pieces?

Chunks follow token boundaries, not words or sentences. A chunk can be a fragment of a word, so never split or parse the text until you have the full string.

Can I stream structured output like JSON?

You can, but partial JSON is not valid until the stream ends. Wait for the complete response before parsing it.

Is streaming the same as tool calling?

No. Streaming delivers output as it is generated. Tool calling lets the model request an action. Some APIs stream tool call arguments too, so read the events carefully.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.