# Why Stream Tokens?

> Why AI chat apps show words as they are written, what streaming costs in checks and errors, and how to explain it for your AI interview.

**In short:** Streaming sends each part of the reply as soon as the model writes it, so people can start reading early instead of waiting for the full answer. It does not make the whole reply finish sooner, and it makes checking the text before anyone sees it harder.

AI and LLM interview question Q315, Inference, performance and cost. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/why-stream-tokens

## Say it in 30 seconds

Streaming means the model sends its reply in small pieces as it writes them, instead of holding everything until the end. The user sees the first words early, so the app feels fast, even though the full reply takes about the same time to finish. The costs are that you can't check the whole text before it shows, and an error can cut the stream halfway. So I hold back a few tokens for checks, and I plan for a broken stream.

## Say it in 2 minutes

A language model writes its answer one token at a time. A token is a small piece of text, often a word or part of one. By default, the API waits until the whole answer is ready and then sends it in one go. Streaming changes that. The server sends each piece as soon as it is written, over one open connection, so the app can show it straight away.

The point is time to first token. If a reply is long, the user waits a long time before seeing anything, and that feels slow even when the total time is fine. With streaming, they start reading almost at once, while the rest arrives. Streaming does not make the full answer finish sooner, so I measure both numbers: time to first token and end-to-end latency.

The trade-offs are real. A half-written answer is hard to check. If a bank's assistant might quote a wrong fee, I hold back a short buffer, check the amounts in it, then show it, and check each later block the same way. Errors can also arrive mid-stream, so the app must keep what it got, mark the reply as cut off, and offer a retry. Tool use and thinking blocks cannot be partly recovered, so after a break I keep the finished text and resume from the most recent text block.

So in an interview I say streaming is a choice about when users see text. The model writes the same amount either way. I use it for chat screens, and I skip it for background jobs where nobody is watching the text, unless the output is so large that the SDK requires streaming to avoid HTTP timeouts.

## What is streaming, in plain words?

A model writes its answer one token at a time. A token is a small piece of text, often a word or part of one. Streaming means each piece goes to the screen as soon as it is written, instead of waiting for the full answer.

Without streaming, the app waits for the whole reply, then shows it at once. With streaming, the first words appear while the rest is still being written. The answer is the same. Only the timing changes.

Picture a bank teller reading a long account statement aloud. She reads each line aloud as she goes, so the customer hears the first lines early. If she waited until the whole page was ready, the customer would sit in silence first. A bank's support chat works the same way: the first sentence of a fee answer shows while the rest is still being written.

- **Token:** A small piece of text the model reads and writes, often a word or part of one.
- **Streaming:** Sending a reply in pieces as each one is written, not all at once at the end.
- **Time to first token:** How long until the first piece of text comes back from the model. Reasoning or tool work before the answer can delay what the user sees.
- **Server-sent events:** A way for a server to keep sending small messages down one open web connection (HTTP).

## How does streaming send the reply, step by step?

By default the server writes the whole answer, then sends it in one piece. OpenAI's guide puts the cost of that plainly, and the fix is to send pieces as they are written.

> "When generating long outputs, waiting for a response can take time." (OpenAI)
>
> "Streaming lets the user start reading before generation finishes." (OpenAI)

1. **Ask for a stream:** The app sends the request and asks for a stream, so the server answers in pieces instead of one block.
2. **The first piece lands:** The first token arrives and the app can show it. This is the wait the user feels most.
3. **Each piece is added:** The server keeps sending small events, each with one or more tokens. The app adds each one to the text on screen.
4. **A closing message ends it:** The stream finishes with a final message that says the reply is complete. Until then, the app treats the reply as still being written.

*Diagram, Same reply, two ways to send it: The same made-up 480-token reply, sent two ways. Without streaming, the agent sees nothing until the last token is written. With streaming, the first words show while the rest is still being written.*
1. The model writes the full reply before anything leaves the server.
2. The whole reply waits on the server, then arrives in one go at 12.9 s.
3. The model writes piece by piece, and each piece is sent as it is ready.
4. Pieces arrive as they come, so the agent reads from 0.9 s.

Two numbers matter, and a good answer names both.

- **Time to first token: When the first text appears.** How long the user waits before anything shows. Streaming cuts this wait.
- **End-to-end latency: When the full result is ready.** How long the user waits for the complete answer, tools and retries included. Streaming does not shorten this.

## Why does the first word matter in a real product?

Take a made-up bank. Its support assistant drafts a 480-token reply to a customer who disputes a card fee, while the agent is on a live call. The numbers are made up: the model waits 0.9 seconds, then writes 40 tokens a second.

Without streaming, the agent stares at a blank panel for 12.9 seconds, then the whole draft appears. Streamed, the first words show at 0.9 seconds, so the agent can start talking while the rest is still being written.

A safety check changes the maths. The bank scans the first part of each draft for account numbers before anyone sees it. Holding back 60 tokens for that scan moves the first visible text to 2.4 seconds. That is still well ahead of 12.9. But the scan only reads those 60 tokens, so each later chunk needs its own check before it shows.

```python
# A made-up call-centre assistant drafts a 480-token reply to a billing dispute.
# Made-up numbers: the model waits 0.9 s, then writes 40 tokens a second.
REPLY_TOKENS = 480
TOKENS_PER_SECOND = 40
START_SECONDS = 0.9

def seconds_until_text(held_back=0):
    # Text shows after the first token, or after a safety check has read held-back tokens.
    return START_SECONDS + held_back / TOKENS_PER_SECOND

def seconds_until_done():
    return START_SECONDS + REPLY_TOKENS / TOKENS_PER_SECOND

print("No streaming: agent sees nothing until", round(seconds_until_done(), 1), "s")
print("Streaming: first words at", round(seconds_until_text(), 1), "s")
print("Streaming, after checking 60 tokens first:", round(seconds_until_text(60), 1), "s")
print("Whole reply is finished at", round(seconds_until_done(), 1), "s in every case")
```

Output:

```text
No streaming: agent sees nothing until 12.9 s
Streaming: first words at 0.9 s
Streaming, after checking 60 tokens first: 2.4 s
Whole reply is finished at 12.9 s in every case
```

The finish time stays at 12.9 seconds in every case. Streaming moves the first words earlier, and nothing else.

## What are the costs and risks of streaming?

Streaming is not free. Four things can go wrong, and a good answer names them.

- **Checks get harder:** Half a reply is hard to judge. OpenAI warns that streaming makes moderating output harder. If you request moderation scores with a generation request, they arrive after the full output.
- **Errors can arrive mid-stream:** The API might send an error inside the stream, after the first words are already on screen. Anthropic says the SDK, the code library your app uses, then ends the stream with an error. The app keeps what arrived, marks it cut off, and offers a retry.
- **Some blocks can't be patched:** Anthropic's guide says tool use and thinking blocks (the model's reasoning) cannot be partly recovered. After a break, keep the finished text and resume from the most recent text block.
- **Pieces split words:** A piece can end halfway through a word. Join the pieces first, then check the joined text.

> "Note that streaming the model's output in a production application makes it more difficult to moderate the content of the completions, as partial completions may be more difficult to evaluate." (OpenAI)

> **Tip:** Say that streaming moves the first words earlier. The total time stays the same, and naming that trade-off is what interviewers listen for.

### How would you stream a bank reply that quotes a fee?

A customer asks why a card fee changed, and the assistant drafts a reply that quotes the new fee. Streaming suits the chat window, but the fee must come from the bank's fee table, never from the model's memory.

1. **Fetch the fee from code:** A tool reads the bank's fee table. The model reads the answer and writes around it. It can still misquote a number, so the check on every chunk matters.
2. **Hold back each block:** Hold back each short block, check every amount and any account number in it, then show that block. Repeat for the rest of the reply. If a check fails, stop the stream.
3. **Plan for a cut-off stream:** If the stream breaks halfway, the chat says the reply was cut off and offers a human agent, rather than showing half a fee.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: the reply is sent in pieces as it is written.
- [ ] The user's wait: time to first token, not only the total time.
- [ ] Streaming does not make the full answer finish sooner.
- [ ] The costs: harder checks, errors mid-stream, partly sent tool calls.
- [ ] When to use it: chat screens yes, background jobs usually no.
- [ ] A buffer for safety checks, and a plan for a broken stream.

**Common traps**

- Trap: "Streaming makes the model faster." Better: It makes the first words show sooner. The full reply still takes as long.
- Trap: "Just show every token as it comes." Better: Check the text before it shows, even if that costs a short buffer.
- Trap: "If the stream breaks, just retry." Better: Save what arrived first, as Anthropic's guide says, then mark the reply cut off and offer a fix.

## Can you spot the flaw?

**Spot the flaw:** Which snippet streams the reply and checks each part before the customer sees it?

A. for piece in stream: screen.show(piece)
B. text = model(prompt); check(text); screen.show(text)
C. for block in buffered(stream, 60): check(block); screen.show(block)
D. for piece in stream: screen.show(piece); check(piece)

**Answer:** C. The third one holds back each block of 60 tokens, checks it, then shows it, so the first words still come early. The first shows text unchecked, the second waits for the full reply, and the fourth shows each piece before checking it.

**Quick check:** A call-centre assistant streams its replies, and a network drop cuts one off halfway. What should the app do first?

A. Retry the whole request silently
B. Keep what arrived, mark the reply cut off, then offer a retry
C. Show the half reply as if it were complete
D. Wait for the model to finish on its own

**Answer:** B. Keep the partial text, mark it as cut off, and let the agent or customer choose the retry. A silent retry hides the break, and showing a half reply as complete misleads the customer.

## Can you answer these from memory?

**Flash cards**

- **What is streaming?** The reply is sent in pieces as each one is written, not all at once.
- **Does streaming make the full answer faster?** No. The first words appear sooner, but the full reply takes about the same time.
- **What is time to first token?** How long until the first text starts coming back to the app.
- **Why is streaming harder to moderate?** Half a reply is hard to judge, and requested moderation scores arrive after the full output.
- **What if a stream breaks halfway?** Keep the partial text, mark it cut off, and offer a retry. Never show it as complete.
- **When would you not stream?** Background jobs nobody watches. Very large max_tokens may need streaming to avoid HTTP timeouts.

## Likely follow-up questions

- Q314 Static vs continuous batching? https://designervenkat.online/interview/ai/static-vs-continuous-batching
- Q316 TTFT vs tokens/sec vs total latency? https://designervenkat.online/interview/ai/ttft-vs-tokens-per-second-vs-latency
- Q317 The latency vs throughput trade-off? (full answer coming)
- Q318 How do you diagnose a slow LLM pipeline? https://designervenkat.online/interview/ai/how-to-diagnose-a-slow-llm-pipeline
- Q322 What is model routing (cascades)? https://designervenkat.online/interview/ai/what-is-model-routing

## Learn it in depth

- [Streaming Responses](https://designervenkat.online/learn/ai-engineering/streaming-responses)
