Stop Sequence.

A predefined string that signals an LLM to halt text generation when encountered, used to control output boundaries and structure responses.

A stop sequence is a specific string or token that instructs a language model to immediately cease generating text when it encounters or produces that sequence. This mechanism serves as a crucial control tool for developers and applications, allowing them to define clear boundaries for model outputs and prevent runaway generation. Stop sequences are essential for creating predictable, structured interactions with LLMs, particularly in applications requiring specific formatting or length constraints.

Stop sequences work by being monitored during the token-by-token generation process, where the model checks each newly generated token against a list of predefined stop conditions. Common stop sequences include newline characters, specific punctuation marks, custom delimiters like "###" or "END", or even complete phrases. Multiple stop sequences can be active simultaneously, and the model will halt upon encountering any of them. The flexibility of this system allows developers to create complex structured outputs, such as JSON responses, code blocks, or conversational turns that end at natural breakpoints.

In practice, stop sequences are fundamental to prompt engineering and application development, enabling everything from chatbot turn-taking to structured data extraction. However, choosing appropriate stop sequences requires careful consideration of the expected output format and potential edge cases where the sequence might appear naturally in valid responses. A poorly chosen stop sequence might truncate legitimate content, while an overly specific one might fail to prevent unwanted continuation. Understanding stop sequences is essential for anyone working with LLM APIs or building applications that require controlled text generation.