GenLucid

LLM · STEP 6

How Does the Generation Loop Work?

A full LLM answer, a paragraph, a block of code, is never produced in one shot. Every step covered so far, tokenizing, embedding, attention, layers, prediction, runs to produce exactly one new token. That token gets appended to the sequence, and the entire model runs again on the longer input to produce the next one. This loop, repeated until the answer is done, is the generation loop.

One-pager titled "The generation loop": three rows showing a context string growing by one token each time ("...is" becomes Paris, "...is Paris" becomes a comma, "...is Paris," becomes "a"), with a loop arrow back to the start, and a REMEMBER band noting one pass makes one token, the token gets appended, and the whole pass runs again.
One-pager: three snapshots of a growing sequence, each one token longer, looping back into the same model.

One full pass, one token, every time

Given "The capital of France is", one complete pass through the model produces "Paris". That word is appended to the context, making it "The capital of France is Paris", and the model runs again, in full, on that new, longer sequence to produce the next token, a comma. Append again, run again, and the next token might be "a". Nothing is cached in the sense of skipping computation for old tokens' final answer; every token in the growing context gets reprocessed by attention at every new step (though key/value caching, an engineering optimization, avoids redoing every layer's work from scratch for tokens already seen).

This is why an LLM response streams out piece by piece rather than appearing all at once: each visible token really is the result of a separate full pass through the model, not a display effect layered over an answer that was already fully computed.

Why this shapes speed, cost, and word choice

Generating a hundred-token answer means running the model roughly a hundred times, each pass a bit more expensive than the last as the context grows (see what attention is for why cost scales with sequence length). That's the direct reason response length is one of the biggest cost and latency levers in a production system, and why techniques that generate several tokens per pass (speculative decoding, among others) are an active area of engineering work specifically to cut down how many full loop iterations a response needs.

It also means the model commits to each token before it has any idea what it will write next. There's no lookahead or revision built into the base loop: once a token is chosen and appended, it's part of the fixed context every later step conditions on. Whatever looks like planning or backtracking in a model's output is either baked into patterns learned during training or added by an outer system (an agent harness re-prompting the model, or explicit reasoning steps), not a capability of this loop itself.

FAQ

How does the model know when to stop generating?
The vocabulary includes a special end-of-sequence token, and the model learns during training to predict it when a response is complete. Generation stops as soon as that token is sampled, or when a separate length limit set by the system is reached first.
Is the generation loop the same thing as an agent's reasoning loop?
No, they operate at different levels. The generation loop described here produces one token at a time inside a single model call. An agent harness (see what an agent harness is) sits above that: it can call the model multiple full times, feed in tool results between calls, and decide what to do next, a different, outer loop built on top of repeated uses of this one.

Sources

Related

Last updated 2026-09-16