Define a stop sequence in an LLM API call
A stop sequence is a string the server matches against decoded output during generation; when it matches, decoding halts and the matching tokens are stripped from the response.
Picture a child reading a book aloud. You tell them: stop the moment you read the word `THE END`. They keep reading, watching for that exact phrase, and the instant they say it, they snap the book shut. A stop sequence is the same instruction handed to an LLM. The runtime watches every token as it streams out and compares the recent suffix against the list of stop strings you provided. The moment one matches, generation halts. The matching string itself gets snipped off before the response is returned, so you get a clean cut. People use this to end a tool call cleanly, to stop at a section heading, to prevent the model from pretending the user said something next, or to honor any custom delimiter their format uses.
Detailed answer & concept explanation~6 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.
5 min: define a stop sequence as a caller-supplied termination string, walk the decode-loop check against the output suffix, contrast with `max_tokens` and EOS, name the streaming lookahead subtlety, and give one production use case like role-tag or tool-call termination.
Real products, models, and research that use this idea.
- OpenAI Chat Completions accepts up to four `stop` strings; if any appears in the output, generation halts and `finish_reason` is `stop`.
- Anthropic Messages exposes `stop_sequences` and reports the matched string in `stop_sequence` on the response for debugging.
- Tool-use prompts often use `</tool>` or `\n\nUser:` as a stop to prevent the model from continuing past the structured boundary.
- vLLM and Hugging Face TGI both implement stop-sequence matching against the detokenized output and buffer one to two tokens during streaming to disambiguate matches.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does a stop sequence work when the stop string spans two BPE tokens?
QWhy might a stop sequence cause noticeable pauses in a streamed response?
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.
Conflating stop sequences with `max_tokens`. `max_tokens` is an unconditional length cap; a stop sequence fires only when specific content is generated. Both can apply to one request.
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.