Skip to main content
Last time we left off with a real server running and one question still unanswered: what happens between your prompt arriving and the model returning an answer? Opening that up reveals the core operational challenges of serving large language models (LLMs). Before we go further, one key word: a language model doesn’t read and write whole words like we do. It breaks text into pieces called tokens. A token is roughly three-quarters of a word on average for English, but exact token boundaries depend on the model’s tokenizer (byte-pair encoding, SentencePiece, or related schemes), so sometimes a token is a whole word and sometimes only part of one. Take a short sentence like this: Serving LLMs is not like serving web apps. That’s eight words, but it comes to nine tokens, and
A presentation slide titled "What Is a Token?" with teal token-like blocks reading "Serving LLMs is not like serving web apps." Below are two icons labeled "Context" (128,000 tokens) and "Cost" (dollar sign).
everything in the request counts as tokens — what the model can read (context), what you pay, and how long it takes. Keep that one word in mind. The core behavior for autoregressive LLMs is simple but powerful: given all the text so far, the model predicts the single next token. It appends that predicted token to the context, and then predicts the next token. It repeats this loop until it stops. So a 100-token answer isn’t computed in one giant pass; it’s the model running that single-token prediction loop 100 times. That’s also why you see answers “type out” token by token in streaming interfaces.
Because tokens are produced incrementally, streaming output as each token is generated is straightforward: each produced token can be sent to the client with very little additional computation beyond the generation itself. Two practical consequences follow, and both cause operational stress later. The first consequence is latency: these requests take a long time. A normal web request often completes in milliseconds, but an answer with hundreds of tokens requires hundreds of loop iterations, which can tie up expensive GPU resources for seconds.
A dark infographic titled "Requests Take a Long Time" comparing a fast web request to a slower LLM request. The web request shows 12 ms and "done," while the LLM request shows a progress bar at 2.6 s for 130 tokens.
The second, and larger, consequence is variability: requests come in wildly different sizes. “What’s the capital of France?” is tiny. “Summarize this 100-page contract” might be tens of thousands of tokens in and thousands out. Both hit the same API endpoint, but one can require orders of magnitude more compute, memory, and time than the other.
An infographic titled "No Two Requests Are Alike" that contrasts a short question ("What's the capital of France?") represented by a few blue blocks with a much larger block grid for "Summarize this contract." Both inputs are shown feeding via arrows into a single turquoise "One API" icon.
Both long runtimes and high variability matter because they stress the server in different ways. Understanding the internal loop helps explain why. Two halves of the generation loop Although the high-level loop is simple, each iteration actually relies on two different phases that stress hardware differently:
  • Context encoding (initial pass): the model runs a large forward pass over the entire input context to compute internal key/value (KV) caches for the attention layers. This step uses large matrix multiplications across the full prompt and is heavy on compute and memory bandwidth. Very long contexts increase the memory footprint substantially.
  • Per-token generation (incremental decode): for each new token the model runs a forward step that uses the cached keys and values to produce the next-token distribution, samples or selects a token, and appends it to the context. Because of caching, this per-token step is cheaper than reprocessing the whole prompt, but it still attends to all prior tokens (so cost grows with sequence length) and involves many small GPU kernel launches. Per-token work is latency-sensitive and often less throughput-efficient on GPUs than the big batched operations of the initial pass.
This split—an expensive upfront context encoding followed by many smaller per-token steps—explains a common user experience: a short pause after you submit a prompt, then streaming tokens begin. That pause is the model computing the heavy initial pass (building the KV cache); once it finishes, the model decodes tokens one by one and streams them out. Quick comparison
Very long contexts and large generated outputs can cause high costs, increased latency, or out-of-memory errors on GPUs. Design prompts and client expectations accordingly.
Why this matters for serving LLMs
  • Token count and context length directly affect cost, latency, and memory. Short prompts are fast and cheap; long contexts increase memory use and per-token compute cost.
  • Variability makes capacity planning and fair scheduling difficult: a single long request can monopolize resources and increase tail latency for others.
  • Streaming mitigates perceived latency but does not eliminate total compute time; it simply delivers tokens as they are produced.
Token count, context length, and how you structure prompts directly affect cost, latency, and memory. Short prompts may be fast and cheap; long contexts increase memory use and per-token cost.
Further reading and references Relevant links

Watch Video