liveBook · Manning

Generation

As explained in Build a Reasoning Model (From Scratch)

The process of producing new textual output from a language model given prior context or instructions.

Text generation produces output from a prompt by preparing input tokens, running a pretrained language model, selecting tokens, and decoding them into text. section 2.1 section 4.4.1

A practical generation loop follows these steps:

  • Tokenize the prompt into token IDs and pass the sequence to the model. section 3.3
  • Read the logits for the final input position, then select the highest-scoring vocabulary entry as the next token. section 4.4.1
  • Feed generated tokens back into the loop until reaching the token limit or end-of-sequence marker, while optionally streaming decoded text. section 2.8 section 3.3

For longer outputs, KV caching stores attention keys and values so each iteration processes the new token instead of recomputing the unchanged prefix. section 2.8

Selecting “Berlin”

Given “The capital of Germany is”, tokenization produces IDs; the model scores the vocabulary, and argmax selects token 19846, decoded as “ Berlin”. section 4.4.1

Figure 2.2 An overview of an LLM generating a response (output text) given a user query (input text)

Use the pipeline diagram to trace movement from user input through the model to generated output. section 2.1

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Explains token preparation, logits, argmax, streaming, and KV caching, while deferring attention and architectural internals.
Examples
Demonstrates a Berlin completion, streamed decoded output, a cached generation function, and a reusable generation wrapper.
What it assumes
Assumes access to a pretrained LLM and tokenizer, while teaching input preparation and generation mechanics from the implementation level.
What you can do after
Build it from scratch using tokenization, autoregressive decoding, streaming, end-of-sequence handling, and KV-cache optimization.
How it connects
Connects tokenization, pretrained language models, autoregressive decoding, inference, KV caching, and temperature scaling.

Also explained in

Compare how 20 books explain generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Reasoning Model (From Scratch). The section links point to the parts of the book it draws on.