Flashcard: what is a stop sequence in an LLM API call and what is it used for?
A stop sequence is a string the model halts generation on the moment it produces it, used to enforce structural boundaries in output, distinct from max-tokens which caps length.
Imagine asking a friend to tell you a story but to stop the second they say the end. A stop sequence is exactly that instruction for an LLM. You hand the API a string, and the model writes one token at a time, checking after each step whether the running output now ends with your stop string. The moment it does, generation halts and that stop string is sliced off before the reply is returned. This is different from telling the friend stop after exactly two minutes, which is what max-tokens does. The stop sequence is a content rule. Max-tokens is a length rule. You usually want both: the stop sequence is the intent, max-tokens is the safety net in case the model never reaches the stop string.
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.
6 min: define stop sequence as content-based termination, contrast with max-tokens, walk through three production uses, name provider differences in match semantics, point to stop_reason for debugging.
Real products, models, and research that use this idea.
- Anthropic's Claude API accepts a stop_sequences array; the response payload reports stop_reason=stop_sequence when one fires, letting clients distinguish clean termination from length-cap termination.
- OpenAI's chat completions API accepts up to 4 stop strings via the stop parameter; the finish_reason field reports stop on a successful match.
- LangChain's ReAct agent uses '\nObservation:' as a stop sequence to halt the model after each reasoning step so the orchestrator can inject the real tool output.
- Many JSON-mode prompts use '```' as a stop sequence to terminate the fenced code block cleanly when the model is asked to produce JSON wrapped in a code fence.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does Anthropic match stop sequences on character boundaries while OpenAI matches on token boundaries, and what difference does it make?
QHow would you use the stop_reason field in production logging to catch failing prompts?
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.
Confusing stop sequences with max-tokens; one is a content boundary (terminate on string match), the other is a length cap (terminate after N tokens regardless of content).
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.