You ask a chatbot where your order is, and the reply appears word by word. That is not a typing animation. The model genuinely does not know its next word until it has written the previous one. That single constraint explains streaming, token pricing, and confident wrong answers. By the end, you will understand how a large language model turns your prompt into text, one token at a time.
The Streaming Mystery
Picture a support chat in an online shop. A customer asks where an order is, and the app passes that message to a large language model. The first words arrive fast, but the complete answer takes seconds. Why not compute the whole thing and send it once? Because the full answer does not exist yet. The model produces output in small pieces called tokens, and each new token is conditioned on every token before it. Token one has to be finished before token two can even be considered. So chat apps stream. As each token appears, the server pushes it to the browser, often over server sent events, a simple one way connection. The customer sees words form immediately. Perceived speed improves, even though the total generation time barely changes. This one token design shapes everything downstream. Wait time grows with answer length. Providers usually bill per token, in and out. Long replies hold expensive accelerators. Before you tune settings or cap lengths, you need to know what a token actually is.
Text Becomes Tokens
A model cannot read letters. Internally it only handles numbers. So before your prompt arrives, a tokenizer splits the text into tokens. A token might be a whole common word, a fragment of a longer word, punctuation, or a leading space plus a word. Take where is my order. Common words like where and order usually map to one token each. An unusual product name can shatter into several pieces. The tokenizer matches each piece against a fixed vocabulary of many thousands of entries, often built with byte pair encoding. Every entry in that vocabulary owns an identification number. Your sentence becomes a short list of integers, and that list is all the model ever sees. When it answers, it emits more numbers, and the same tokenizer converts them back into readable text. Token counts have real consequences. Context windows, rate limits, and prices are all measured in tokens, not characters. Code, rare names, and many non English languages need more tokens for the same meaning. That is also one reason models struggle to count letters in a word.
The Prediction Loop
Here is the core loop. The model takes every token so far, your prompt plus whatever it has already written, and pushes them through a neural network called a transformer. What comes out is not a word. It is one score per vocabulary entry. Those raw scores are turned into probabilities that sum to one. For the shop question, the word your might score high, the word I somewhat lower, and thousands of unrelated tokens get almost nothing. The model is answering one question. What comes next? One token is selected and appended to the context. Then the loop runs again over the longer list. Pick, append, repeat. This is autoregressive generation, and it means every word you read was the input for the word after it. So when does it stop? The vocabulary includes a special end of sequence token, and picking it ends generation. Your app can also enforce a maximum token count or a stop string. Each selected token can still be streamed straight to the customer.
Sampling and Its Risks
How is that one token actually chosen? The simplest rule, greedy decoding, always takes the highest probability token. For pulling structured data out of an order confirmation email, that is often fine. For longer prose, greedy output tends to repeat itself or loop. Most chat products sample instead. Temperature reshapes the distribution before sampling. Low values sharpen it toward the top choice, and zero usually means greedy. High values flatten it, so unlikely tokens win more often. A top p filter drops the unlikely tail first. Here is the danger. The model picks what is probable, not what is true. Ask about an order it cannot see, and it may invent a confident tracking number. Once that wrong token lands in the context, every later token builds on it. The standard fix is to supply the facts. Your service queries the orders database and pastes the real status into the prompt, so the probable continuation is now grounded in real data. This pattern is usually called retrieval augmented generation.
Serving Generation at Scale
Now scale it up. Thousands of shoppers chat at the same time, and recomputing the entire conversation for every new token would throw away work, because earlier tokens never change. So inference servers keep a key value cache of attention keys and values. With that cache, each step only processes the newest token and reuses the stored results. That splits serving into two phases. Prefill handles the whole prompt in parallel, while decode emits tokens one at a time. On long answers, decode usually dominates the wait. But that cache lives in scarce graphics memory, and it grows with every token in every live conversation. Long chats plus many simultaneous users can exhaust it. When that happens, the server queues requests, or rejects them outright. Context length is never free. Serving engines such as v L L M attack this with paged cache memory and continuous batching. Rather than waiting for a whole batch to finish, the server swaps finished requests out and new ones in between decode steps. The accelerator stays busy, and shoppers get tokens together. So the word by word effect you see in every chat app is not decoration. It is the shape of the computation itself. Tokens in, one scored pick at a time, tokens out. Understand that loop, and pricing, latency, caching, and hallucination all stop being mysteries.
Recap
- Models read and write tokens, small pieces of text stored as numbers.
- Each step scores every token, picks one, and feeds it back in.
- Temperature and top p change how adventurous the next pick is.
- Retrieval grounds answers in real data and cuts invented details.
- The key value cache speeds decoding but eats scarce graphics memory.
Your turn
What temperature and top p settings do you run in production, and why those values? Tell me in the comments.
Aisora Lab explains one system design idea a day in a 60-second video, plus a deep dive every evening. Find it as @aisoralab on YouTube, Facebook and Instagram.
This article was written with AI assistance from the script of the video above, then fact-checked and reviewed before publishing.