Stop Generation

LLMs interview question · Easy difficulty

How does an LLM decide when to stop generating?easy

Status
✓
Type
conceptual
Topic
stop-generation
Frequency
common
Tags
EOS-token, generation, stop-sequences
Answer

Generation stops when the model predicts a special end-of-sequence (EOS) token it was trained to emit once a response is complete, or when an external constraint kicks in first: a max token limit set by the caller, or a developer-defined stop sequence like "\n\nUser:" in a chat template.

Explanation

The model generates text token by token, predicting one token at a time, and stops when one of several conditions is met: it predicts EOS (learned during training to signal a complete response), it reaches the max token limit the caller configured, it produces a stop sequence the developer defined, or the API/server cuts the response off due to infrastructure limits. In practice, always set a max token limit as a hard guard regardless of EOS behavior, since it's the only backstop against a runaway generation that never emits EOS.

Follow-upWhy should you always set a max token limit even if EOS behavior looks reliable?
Follow-upWhat could cause a model to never emit an EOS token?