---
title: "Stop sequences (language models) — ストップシーケンス"
source: "https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9"
wiki: "systems-analysis.info/int"
article: "Stop_sequences_(language_models)_—_ストップシーケンス"
language: "ja"
categories:
  - "Category:Japanese"
  - "Category:Large language models"
  - "Category:Machine learning"
  - "Category:Systems analysis terminology"
revision_id: 6953
wiki_created_at: 2026-09-07T00:13:43Z
wiki_modified_at: 2026-09-07T00:13:43Z
downloaded_at: 2026-09-07T23:16:36Z
---

# Stop sequences (language models) — ストップシーケンス

**Stop Sequence**（停止シーケンス）は、大規模言語モデル（LLM）において、テキスト生成を停止すべきであることをモデルに知らせる、特殊な文字またはトークンの並びである<sup>[\[1\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-promptitude_def-1)</sup>。このメカニズムは自己回帰言語モデルの重要な構成要素であり、応答を制御可能かつ予測可能に終了させる役割を果たす。

ストップシーケンスを用いると、モデルは生成の各ステップで、生成済みのテキストが指定されたシーケンスのいずれかで終わっていないかを確認する。一致が見つかった場合、生成は即座に停止され、ストップシーケンス自体は最終的な出力には含まれない<sup>[\[2\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-openai_api-2)</sup>。これにより開発者は、プロンプト自体を変更することなく、応答の境界を正確に制御できる。

## 機能の基本原則

自己回帰言語モデルでは、テキスト生成はトークンごとに逐次的に行われる。各ステップで、モデルは先行する系列全体（入力プロンプトと既に生成されたテキスト）に基づいて次のトークンを予測する。これは数学的には、次のような条件付き確率として表される。

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

ここで、$y_{t}$ は現在生成中のトークン、$y_{< t}$ はそれ以前に生成されたトークン列、$x$ は入力系列である<sup>[\[3\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-vellum_params-3)</sup>。

ストップシーケンスのメカニズムは、この反復的な生成プロセスを中断するための外部基準として機能する。

## ストップシーケンスの種類

停止メカニズムにはいくつかの主要な種類があり、単独でも組み合わせても利用できる。

### 1. End-of-Sequence (EOS) tokens - シーケンス終了 (EOS) トークン

**End-of-Sequence (EOS)** は、モデルの語彙に組み込まれた特殊なトークン（例：\`\<\|endoftext\|\>\`）であり、テキストのまとまりの終わりを示すために設けられている。訓練データ中のすべてのテキストがこのトークンで終わっているため、モデルは応答が完了したと判断した時点でEOSトークンを生成するように学習する<sup>[\[4\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-gpt3_survey-4)</sup>。EOSトークンが検出されると、生成は自動的に停止する。

研究によれば、EOSトークンを用いて訓練されたモデルは、内部表現において系列内の位置を符号化する傾向を持つ。これは、訓練時に見られた長さを大幅に超える系列への外挿（length extrapolation）能力を制限する場合がある<sup>[\[5\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-newman_eos-5)</sup>。

### 2. ユーザー定義シーケンス

これらは、開発者が特定のタスクのために設定する任意の文字列である。モデルの語彙には含まれないが、文字レベルで追跡される。例として次のものがある。

- **改行文字**: 段落の後で停止させるための \`\n\` または \`\n\n\`。
- **コンテキストマーカー**: 対話での発話を区切るための \`Human:\`、\`User:\`、\`Q:\` など。
- **特殊マーカー**: \`###\`、\`\</output\>\`、\`END\` など。

### 3. 構造的シーケンス

特定の構造的要素を完結させるために用いる特殊なマーカーで、整形された（フォーマット済みの）コンテンツを生成する際に重要となる<sup>[\[1\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-promptitude_def-1)</sup>。

- **コード**: コードブロックを終了するためのトリプルバッククォート（\`\`\`）。
- **JSON/XML**: 閉じ括弧（\`}\`）や閉じタグ（\`\</element\>\`）。

## 技術的実装と課題

ストップシーケンスを的確に検出することは、見かけほど単純ではなく、いくつかの難しさを伴う。

### 検出アルゴリズムと最適化

実際のシステムにおける検出プロセスには、次のものが含まれる。

1.  **各ステップでのチェック**: 新しいトークンが生成されるたびに、システムは現在の出力が指定されたストップシーケンスのいずれかで終わっていないかを確認する。
2.  **部分一致の処理**: シーケンスの一部が生成されているが完全な一致にはまだ至っていない状況を、システムが追跡する必要がある。
3.  **複数基準のチェック**: 多くのシステム（例：OpenAI API）では、複数（最大4つ）のストップシーケンスを同時に追跡できる<sup>[\[2\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-openai_api-2)</sup>。

Hugging Face Transformersでは、この目的のために抽象クラス \`StoppingCriteria\` が用意されており、\`MaxLengthCriteria\`（長さに基づく）や \`EosTokenCriteria\`（EOSトークンに基づく）といったカスタムな停止基準を作成できる。

### 課題と制限

- **トークン化の問題**: これが主要な技術的課題である。同じ文字列（例：\`\nUser:\`）でも、文脈によって異なる形でトークンに分割されることがある。ストップシーケンスが複数のトークンにまたがる場合があり、信頼性の高い検出が難しくなる。
- **パフォーマンス**: 各ステップで多数の長いストップシーケンスを照合すると、特にリアルタイム処理では生成が遅くなることがある。
- **誤検出**: 指定したシーケンスが意図した応答の途中で偶然現れ、早期終了を招くことがある。そのため、十分に固有で識別しやすいマーカー（例：\`\n###\n\`）を選ぶことが重要である<sup>[\[6\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-finetune_gpt3-6)</sup>。

## 応用と利用シナリオ

ストップシーケンスは、LLMの振る舞いを制御するための強力なツールである。

- **長さとコストの制御**: 応答の最大サイズを制限し、その結果としてトークン消費を抑えられる。これは有料APIを利用する際に重要となる。
- **対話システム**: 話者の発話を明確に区切るために用いられ、アシスタントのモデルがユーザーになりかわって応答を生成してしまうのを防ぐ。
- **構造化コンテンツの生成**: JSONやXML形式の正しい出力、コードの記述において不可欠であり、構造が完結した後に余分な情報が付加されるのを防ぐ<sup>[\[7\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-cursor_backticks-7)</sup>。
- **望ましくない振る舞いの防止**: 繰り返しや誤った内容（ハルシネーション）が現れた場合に、生成を中断するのに役立つ。
- **学習とファインチューニング**: 訓練データでは、モデルが適切な箇所で応答を終えられるように、固有のマーカー（例：\`###\`）がストップシーケンスとして頻繁に用いられる<sup>[\[6\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_note-finetune_gpt3-6)</sup>。

## 現代の研究動向

- **適応的な停止基準**: 文脈や生成されたテキストの品質に基づいて、終了点を動的に決定する手法の開発。
- **エントロピーに基づくアプローチ**: トークン分布のエントロピーを指標として用いる。高いエントロピーはモデルの不確実性を示すことがあり、生成を停止する信号として機能しうる。

## 外部リンク

- <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のストップシーケンス利用に関するドキュメント</a>
- <a href="https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.StoppingCriteria" class="external text" rel="nofollow">Hugging FaceのStoppingCriteriaに関するドキュメント</a>

## 参考文献

- 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>.
- Newman, B. et al. (2020). *The EOS Decision and Length Extrapolation*. <a href="https://arxiv.org/abs/2010.07174" class="external text" rel="nofollow">arXiv:2010.07174</a>.

## 脚注

1.  <span id="cite_note-promptitude_def-1">↑ <sup>[1.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_ref-promptitude_def_1-0)</sup> <sup>[1.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#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/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_ref-openai_api_2-0)</sup> <sup>[2.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#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/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#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">[↑](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_ref-gpt3_survey_4-0) Zong, M.; Krishnamachari, B. "A Survey on GPT-3". *arXiv:2212.00857*, 2022. <a href="https://arxiv.org/pdf/2212.00857" class="external autonumber" rel="nofollow">[4]</a></span>
5.  <span id="cite_note-newman_eos-5">[↑](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_ref-newman_eos_5-0) Newman, Benjamin, et al. "The EOS Decision and Length Extrapolation". *BlackboxNLP 2020, arXiv:2010.07174*. <a href="https://arxiv.org/abs/2010.07174" class="external autonumber" rel="nofollow">[5]</a></span>
6.  <span id="cite_note-finetune_gpt3-6">↑ <sup>[6.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#cite_ref-finetune_gpt3_6-0)</sup> <sup>[6.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#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/int/Stop_sequences_(language_models)_%E2%80%94_%E3%82%B9%E3%83%88%E3%83%83%E3%83%97%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9#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>
