---
title: "Stop sequences (language models)"
source: "https://systems-analysis.info/eng/Stop_sequences_(language_models)"
wiki: "systems-analysis.info/eng"
article: "Stop_sequences_(language_models)"
language: "en"
categories:
  - "Category:English"
  - "Category:Large language models"
  - "Category:Machine learning"
  - "Category:Technology"
revision_id: 349
wiki_created_at: 2026-09-06T22:22:27Z
wiki_modified_at: 2026-09-06T22:22:27Z
downloaded_at: 2026-09-07T22:22:53Z
---

# Stop sequences (language models)

**Stop Sequence** in the context of [large language models](https://systems-analysis.info/eng/Large_language_model "Large language model") (LLMs) is a special sequence of characters or [tokens](https://systems-analysis.info/eng/Token_(LLM) "Token (LLM)") that signals the model to stop generating text<sup>[\[1\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-promptitude_def-1)</sup>. This mechanism is a crucial component of autoregressive language models, ensuring controlled and predictable completion of the response.

When a stop sequence is used, the model checks at each generation step whether the already generated text ends with one of the specified sequences. If a match is found, the process immediately terminates, and the stop sequence itself is **not included** in the final output<sup>[\[2\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-openai_api-2)</sup>. This allows a developer to precisely control the boundaries of the response without altering the prompt itself.

## Basic Principles of Operation

In autoregressive language models, text generation occurs sequentially, token by token. At each step, the model predicts the next token based on the entire preceding sequence (the input prompt and the text already generated). Mathematically, this is expressed as a conditional probability:

$$
P(y_{t} \mid y_{< t},x)
$$

where $y_{t}$ is the current token being generated, $y_{< t}$ is the sequence of previously generated tokens, and $x$ is the input sequence<sup>[\[3\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-vellum_params-3)</sup>.

The **stop sequence** mechanism functions as an external criterion for interrupting this iterative process.

## Types of Stop Sequences

There are several main types of stopping mechanisms that can be used either individually or in combination.

### 1. End-of-Sequence (EOS) Tokens

**End-of-Sequence (EOS)** tokens are special tokens (e.g., \`\<\|endoftext\|\>\`) built into the model's vocabulary, designed to signify the end of a logical text segment. The model is trained to generate an EOS token when it considers the response complete, as all texts in the training dataset end with this token<sup>[\[4\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-gpt3_survey-4)</sup>. Upon detection of an EOS token, generation automatically stops.

Research indicates that the presence of EOS tokens influences the attention architecture: models develop internal position-counting mechanisms, which, however, can limit their ability to extrapolate to sequences significantly longer than the training examples<sup>[\[5\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-no_eos_extrapolation-5)</sup>.

### 2. Custom Sequences

These are arbitrary strings that a developer specifies for a particular task. They are not part of the model's vocabulary but are tracked at the character level. Examples include:

- **Newline characters**: \`\n\` or \`\n\n\` to stop after a paragraph.
- **Contextual markers**: \`Human:\`, \`User:\`, or \`Q:\` to separate turns in a dialogue.
- **Special markers**: \`###\`, \`\</output\>\`, or \`END\`.

### 3. Structural Sequences

These are specialized markers used to terminate specific structural elements, which is critical when generating formatted content<sup>[\[1\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-promptitude_def-1)</sup>:

- **Code**: Triple backticks (\`\`\`) to terminate a code block.
- **JSON/XML**: Closing brackets (\`}\`) or tags (\`\</element\>\`).

## Technical Implementation and Challenges

Effective detection of a stop sequence is a non-trivial task associated with several complexities.

### Detection Algorithm and Optimization

The detection process in real-world systems includes:

1.  **Check at each step**: After generating each new token, the system checks if the current output ends with one of the specified stop sequences.
2.  **Handling partial matches**: The system must track situations where part of a sequence has been generated, but a full match has not yet occurred.
3.  **Multi-criteria check**: Most systems (e.g., the OpenAI API) allow tracking several (up to four) stop sequences simultaneously<sup>[\[2\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-openai_api-2)</sup>.

In the [Hugging Face](https://systems-analysis.info/eng/Hugging_Face "Hugging Face") [Transformers](https://systems-analysis.info/eng/Transformer_architecture "Transformer architecture") framework, this is implemented through the abstract \`StoppingCriteria\` class, which allows for the creation of custom stopping criteria, such as \`MaxLengthCriteria\` (by length) or \`EosTokenCriteria\` (by EOS token)<sup>[\[4\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-gpt3_survey-4)</sup>.

### Problems and Limitations

- **[Tokenization](https://systems-analysis.info/eng/Tokenization_(NLP) "Tokenization (NLP)") problem**: This is the primary technical challenge. The same sequence of characters (e.g., \`\nUser:\`) can be split into tokens differently depending on the context. This complicates reliable detection, as a stop sequence might be divided across multiple tokens<sup>[\[5\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-no_eos_extrapolation-5)</sup>.
- **Performance**: Checking for multiple long stop sequences at each step can slow down generation, especially when working with long sequences in real time.
- **False positives**: A specified sequence might accidentally appear in the middle of the desired response, leading to premature termination. Therefore, it is important to choose sufficiently unique and specific markers (e.g., \`\n###\n\`)<sup>[\[6\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-finetune_gpt3-6)</sup>.

## Applications and Use Cases

Stop sequences are a powerful tool for controlling the behavior of LLMs.

- **Controlling length and cost**: They allow limiting the maximum response size and, consequently, reducing token consumption, which is important when using paid APIs.
- **Dialogue systems**: Used to clearly separate turns between speakers, preventing the model-assistant from generating a response on behalf of the user.
- **Generating structured content**: Indispensable for obtaining correct output in formats like JSON, XML, or when writing code, preventing the addition of extraneous information after the structure is complete<sup>[\[7\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-cursor_backticks-7)</sup>.
- **Preventing undesirable behavior**: Help to interrupt generation when repetitive or incorrect content ([hallucinations](https://systems-analysis.info/eng/LLM_hallucinations "LLM hallucinations")) appears.
- **Training and [fine-tuning](https://systems-analysis.info/eng/Fine-tuning_(deep_learning) "Fine-tuning (deep learning)")**: In training datasets, unique markers (e.g., \`###\`) are often used as stop sequences to teach the model to end the response at the correct point<sup>[\[6\]](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_note-finetune_gpt3-6)</sup>.

## Current Research Directions

- **Adaptive stopping criteria**: Development of methods that dynamically determine the termination point based on the context and quality of the generated text.
- **Entropy-based approaches**: Using the entropy of the token distribution as a criterion. High entropy can indicate model uncertainty and serve as a signal to stop generation.

## External links

- <a href="https://help.openai.com/en/articles/5072263-how-do-i-use-stop-sequences-in-the-openai-api" class="external text" rel="nofollow">OpenAI documentation on using stop sequences</a>
- <a href="https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.StoppingCriteria" class="external text" rel="nofollow">Hugging Face documentation on StoppingCriteria</a>

## See also

- [Temperature (LLM)](https://systems-analysis.info/eng/Temperature_(LLM) "Temperature (LLM)")
- [Top-k sampling](https://systems-analysis.info/eng/Top-k_sampling "Top-k sampling")
- [Top-p sampling](https://systems-analysis.info/eng/Top-p_sampling "Top-p sampling")
- [Context window](https://systems-analysis.info/eng/Context_window "Context window")
- [Function calling (LLM)](https://systems-analysis.info/eng/Function_calling_(LLM) "Function calling (LLM)")

## Literature

- Sutskever, I.; Vinyals, O.; Le, Q. V. (2014). *Sequence to Sequence Learning with Neural Networks*. <a href="https://arxiv.org/abs/1409.3215" class="external text" rel="nofollow">arXiv:1409.3215</a>.
- Vaswani, A. et al. (2017). *Attention Is All You Need*. <a href="https://arxiv.org/abs/1706.03762" class="external text" rel="nofollow">arXiv:1706.03762</a>.
- Keskar, N. S. et al. (2019). *CTRL: A Conditional Transformer Language Model for Controllable Generation*. <a href="https://arxiv.org/abs/1909.05858" class="external text" rel="nofollow">arXiv:1909.05858</a>.
- Holtzman, A. et al. (2020). *The Curious Case of Neural Text Degeneration*. <a href="https://arxiv.org/abs/1904.09751" class="external text" rel="nofollow">arXiv:1904.09751</a>.
- Brown, T. et al. (2020). *Language Models are Few-Shot Learners*. <a href="https://arxiv.org/abs/2005.14165" class="external text" rel="nofollow">arXiv:2005.14165</a>.
- Zong, M.; Krishnamachari, B. (2022). *A Survey on GPT-3*. <a href="https://arxiv.org/abs/2212.00857" class="external text" rel="nofollow">arXiv:2212.00857</a>.
- Zhao, Y. et al. (2022). *Calibrating Sequence Likelihood Improves Conditional Language Generation*. <a href="https://arxiv.org/abs/2210.00045" class="external text" rel="nofollow">arXiv:2210.00045</a>.
- Hu, J. C.; Cavicchioli, R.; Capotondi, A. (2023). *A Request for Clarity over the End-of-Sequence Token in the Self-Critical Sequence Training*. <a href="https://arxiv.org/abs/2305.12254" class="external text" rel="nofollow">arXiv:2305.12254</a>.
- Zhu, W. et al. (2024). *Improving Open-Ended Text Generation via Adaptive Decoding*. <a href="https://arxiv.org/abs/2402.18223" class="external text" rel="nofollow">arXiv:2402.18223</a>.
- Zhang, H. et al. (2024). *Adaptable Logical Control for Large Language Models*. <a href="https://arxiv.org/abs/2406.13892" class="external text" rel="nofollow">arXiv:2406.13892</a>.
- Suh, Y. J. et al. (2025). *The Curious Case of Sequentially Mis-calibrated Language Models*. <a href="https://arxiv.org/abs/2205.11916" class="external text" rel="nofollow">arXiv:2205.11916</a>.

## References

1.  <span id="cite_note-promptitude_def-1">↑ <sup>[1.0](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-promptitude_def_1-0)</sup> <sup>[1.1](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-promptitude_def_1-1)</sup> “Stop Sequence: Understanding & Setting It Correctly”. *Promptitude.io Help Center*. <a href="https://help.promptitude.io/en/articles/8897040-stop-sequence-understanding-setting-it-correctly" class="external autonumber" rel="nofollow">[1]</a></span>
2.  <span id="cite_note-openai_api-2">↑ <sup>[2.0](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-openai_api_2-0)</sup> <sup>[2.1](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-openai_api_2-1)</sup> “How do I use stop sequences in the OpenAI API?”. *OpenAI Help Center*. <a href="https://help.openai.com/en/articles/5072263-how-do-i-use-stop-sequences-in-the-openai-api" class="external autonumber" rel="nofollow">[2]</a></span>
3.  <span id="cite_note-vellum_params-3">[↑](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-vellum_params_3-0) “How to use stop sequences?”. *Vellum*. <a href="https://www.vellum.ai/llm-parameters/stop-sequence" class="external autonumber" rel="nofollow">[3]</a></span>
4.  <span id="cite_note-gpt3_survey-4">↑ <sup>[4.0](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-gpt3_survey_4-0)</sup> <sup>[4.1](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-gpt3_survey_4-1)</sup> Brown, Tom, et al. “A Survey on GPT-3”. *arXiv:2212.00857* \[cs.CL\], 1 Dec. 2022. <a href="https://arxiv.org/pdf/2212.00857" class="external autonumber" rel="nofollow">[4]</a></span>
5.  <span id="cite_note-no_eos_extrapolation-5">↑ <sup>[5.0](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-no_eos_extrapolation_5-0)</sup> <sup>[5.1](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-no_eos_extrapolation_5-1)</sup> Suh, Y. J., et al. “The Curious Case of Sequentially Mis-calibrated Language Models”. *arXiv:2205.11916* \[cs.CL\], 24 May 2022. <a href="https://arxiv.org/pdf/2205.11916" class="external autonumber" rel="nofollow">[5]</a></span>
6.  <span id="cite_note-finetune_gpt3-6">↑ <sup>[6.0](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-finetune_gpt3_6-0)</sup> <sup>[6.1](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-finetune_gpt3_6-1)</sup> Eric, Mihail. “How to Finetune GPT3”. *mihaileric.com*. <a href="https://www.mihaileric.com/posts/how-to-finetune-gpt3/" class="external autonumber" rel="nofollow">[6]</a></span>
7.  <span id="cite_note-cursor_backticks-7">[↑](https://systems-analysis.info/eng/Stop_sequences_(language_models)#cite_ref-cursor_backticks_7-0) Corin, Daniel. “Way Enough - Cursor Triple Backticks Stop Sequence”. *danielcorin.com*. <a href="https://www.danielcorin.com/til/cursor/triple-backticks-stop-sequence/" class="external autonumber" rel="nofollow">[7]</a></span>
