Sistava

What is Stop Sequence?

Also called stop token, stop string.

A stop sequence is a string that causes a model to halt generation as soon as it is produced. The matched text is normally excluded from the returned output. Stop sequences give the caller control over where a response ends, independent of the model's own end-of-turn signal or the maximum token limit.

Generation ends for one of three reasons: the model emits its end-of-sequence token, the output reaches the configured maximum length, or a caller-supplied stop sequence appears. Most interfaces report which one occurred as a finish reason, and reading that field is the reliable way to distinguish a complete answer from a truncated one.

The common use is enforcing structure when a model would otherwise continue past the useful part. Setting a stop on a delimiter, a closing marker, or a role label keeps the model from inventing the next turn of a dialogue or appending commentary after a structured payload. This is especially useful with base models that were never trained to stop on their own.

Matching happens on decoded text rather than on tokens, because the same string can be tokenized differently depending on surrounding characters. This means a stop sequence can trigger mid-token from the model's perspective, and it also means a stop string that legitimately appears inside desired content will truncate that content unexpectedly.

Choose stop sequences that cannot appear in valid output. A newline is a frequent mistake in tasks where multi-line answers are wanted. Unusual delimiters are safer. Stop sequences are also not a safety control: they end generation after unwanted text has been produced internally, so they shape output format, not model behavior.

Key points

In practice

A prompt asks the model to complete one side of a dialogue formatted with "User:" and "Assistant:" labels. Left alone, the model happily writes the user's next line too. Setting "User:" as a stop sequence ends generation the moment it starts inventing that turn. The team checks the finish reason on every call so they can tell a clean stop from a response that simply ran into the token limit.

Related terms

Back to the AI Glossary