liveBook · Manning

Stopping criterion

As explained in Build a Reasoning Model (From Scratch)

A criterion that terminates generation when met, such as encountering an end-of-sequence token.

A stopping condition terminates sequential text generation once a target token appears or another limit is reached. Here, it ends decoding after the special end-of-sequence token <|endoftext|>. section 2.7

The generation loop keeps appending the next argmax token to the input sequence until that condition fires. If the eos token ID is provided, the loop breaks before yielding and decoding continues only for completed output. section 2.7

Common stopping behavior shown here includes:

  • Stopping after a user-specified maximum number of new tokens. section 2.7

EOS-triggered generation

The prompt is fed into generate_text_basic_stream with eos_token_id=tokenizer.eos_token_id, so generation stops when <|endoftext|> is produced instead of running to max_new_tokens.

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Defines the condition and shows where it breaks the loop, but stops short of broader sampling strategies.
Examples
Uses <|endoftext|>, tokenizer.eos_token_id, and a 100-token prompt run to demonstrate termination.
What it assumes
Assumes token IDs, decoding, and sequential next-token generation, while introducing the stopping criterion from scratch.
What you can do after
Operate generation with an EOS-aware loop that stops and streams decoded tokens safely.
How it connects
Connects to autoregressive text generation, eos_token_id, argmax, and max_new_tokens.

Also explained in

Compare how 11 books explain stopping criterion →

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.