Define a stop sequence in an LLM API call
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.