How does the serving stack actually decide when to stop generation and what bugs come from mis-set stop sequences?
Describe how an LLM serving runtime decides to terminate generation. Cover the three standard mechanisms. Then identify two production bugs that arise from stop-sequence handling and explain how to avoid them.
Decoding halts on three triggers: the model samples EOS, a user stop string matches the decoded suffix, or max_tokens fires. Mis-set stops cause runaway cost or premature truncation.
Imagine dictating a letter to an assistant who writes one word at a time and never stops on their own. You need a way to tell them when to put the pen down. There are three signals. First, you teach a secret word that means 'the end': when the assistant thinks of that word, they stop. Second, you say 'stop the moment you write the phrase Dear Sir again', so a repeated heading ends the letter. Third, you set a hard limit: 'no more than two pages, full stop.' If you forget all three, the assistant keeps writing forever and you pay for every word. If you pick a stop phrase that shows up in the middle of normal text, they stop too early and hand you half a letter.
Detailed answer & concept explanation~8 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: the three stop mechanisms (EOS, stop sequence, max_tokens) + chat template end of turn tokens + detokenized stop matching + cost of a missed stop on memory bound decode + mid content false positives.
Real products, models, and research that use this idea.
- OpenAI's API exposes a 'stop' parameter (up to four strings) plus 'max_tokens', and the response 'finish_reason' field reports 'stop' or 'length' so callers can distinguish the termination cause.
- Llama 4 chat models define a distinct end-of-turn token in the tokenizer chat template, separate from the document-level EOS, and vLLM registers it as a stop token when the template is applied.
- vLLM's SamplingParams supports 'stop' strings and 'stop_token_ids', and matches stop strings against the running detokenized output rather than at the token-id boundary.
- Hugging Face transformers ships StoppingCriteria and a StopStringCriteria helper precisely because naive token-id stop checks miss matches that cross subword boundaries.
- Anthropic's Claude Opus 4.7 API returns a 'stop_reason' of 'end_turn', 'stop_sequence', or 'max_tokens', mapping directly onto the three termination mechanisms.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy must a stop sequence be checked on detokenized text rather than at the token-id level?
QWhat exactly is the cost of a missed stop in a production decode loop?
QHow do chat templates change what counts as the stop token for an instruct model?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Checking stop sequences against raw token IDs instead of detokenized text. The same string tokenizes differently by leading character, so the match silently misses and generation runs away.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.