Back to glossary

Term

Stop Sequences

Stop sequences are strings that make an LLM end token generation as soon as they appear. They constrain the output deliberately — to prevent role switches, format markers or overly long answers.

Stop Sequences — explained in detail

Stop sequences (also called stop tokens or the stop parameter) are one or more caller-defined strings that a language model treats as a termination signal during generation. As soon as the model produces one of these sequences, token generation halts — the stop sequence itself is usually not included in the returned output. It complements the natural end-of-sequence token (EOS) that a model emits on its own, as well as the hard max_tokens limit.

Technically, after each generated token the decoder checks whether the text produced so far ends with one of the configured stop sequences. In the major providers’ APIs the parameter is typically passed as stop (a list of strings); only a few sequences are common, often with an upper bound (e.g. four) per request.

Example / Practical use

In a chat format with role markers like User: and Assistant:, the stop sequence "\nUser:" prevents the model from hallucinating a new user turn after its own answer. For structured output you can force a format boundary, for example "]" at the end of a JSON list, or a custom marker such as "###" after a few-shot block. The interplay with few-shot prompting is typical too: the separator between examples doubles as a stop sequence so the model halts after producing the first example.

max_tokens caps the output purely by the number of output tokens, regardless of content — stop sequences instead react to specific text patterns. The EOS token is a model-internal special token that the caller does not choose freely, whereas stop sequences are set on the application side. Sampling parameters like temperature or top-p / top-k control how tokens are selected, not when generation ends.

Entdecke mehr