Stop Sequence: A Key Mechanism for Controlling When a Model Stops Generating
A Stop Sequence is a preset character sequence used to make a model stop outputting during generation. It controls response length and structure through a stop signal, making it suitable for prompt engineering, structured output, and boundary control in agent workflows.
What Is a Stop Sequence?
Stop Sequence is usually translated as stop sequence, and some people also call it an end sequence. It refers to a predefined piece of text, string, or character sequence. Its role is to provide a stop signal during model generation: when the output triggers this preset boundary, the model stops continuing to generate text.
In some model APIs, this type of configuration may appear under parameter names such as stop_sequences. Different platforms may have different parameter names and implementation details, but the general idea is similar: use a clear character boundary to control “how far” the model generates.
You can understand a stop sequence as a “termination marker” set for model output. The prompt tells the model what to write, while the stop sequence helps the system determine when to stop writing.
How Does a Stop Sequence Affect the Model Generation Process?
When a model generates text, it produces subsequent content step by step. A stop sequence participates in this process as a preset stop signal: when the generated content meets the stop condition, the system stops continuing output.
This does not mean the model semantically “realizes it is time to summarize.” Instead, it constrains the output boundary through a predefined character sequence. For example, a dedicated delimiter, tag, or end marker can be used as a stop sequence so that the model no longer continues expanding the content after this boundary is triggered.
A Simple Example
Suppose you want the model to output only one specific answer and not continue generating the next round of dialogue or additional explanations. You can define a boundary marker in the format that is unlikely to appear in the main text. When the generation process triggers this marker, the stop sequence can act as a truncation condition, preventing the model from continuing to output unnecessary content.
Key point: A stop sequence focuses on “where to stop,” not “whether the content is good enough.” Content quality, task scope, and format requirements still need to be defined through clear prompts.
What Output Control Problems Can Stop Sequences Solve?
Stop sequences are often used to control the length and structure of model responses, especially for tasks with clear output boundaries. They can reduce the probability that the model continues generating irrelevant content after completing the target segment, and they can also make fixed-format responses easier for subsequent programs to process.
| User Need | Solution | Effect |
|---|---|---|
| Do not want the model to keep adding irrelevant content after the answer ends | Set a clear end marker as the stop sequence | Stops output after reaching the boundary, reducing redundant continuation |
| Only need the model to generate a certain segment | Set a fixed delimiter or tag after the target segment | Helps the output stay within the specified scope |
| Want the response to maintain a fixed structure | Use a stop sequence together with the format rules in the prompt | Stops at paragraph, tag, or delimiter boundaries |
| Need to control response length | Use a stop sequence to define the boundary for further model expansion | Truncates output at an appropriate position, preventing overly long answers |
A stop sequence is not the same as a general length control parameter. It is more suitable when you already know a certain boundary marker, tag, or delimiter; if the task itself has no clear boundary, relying only on a stop sequence is usually not enough.
How Is a Stop Sequence Different from Ordinary Prompt Constraints?
Ordinary prompt constraints mainly rely on the model following natural language instructions, such as “answer in only three sentences” or “do not output explanations.” A stop sequence is more like a structured boundary: as long as the generation process triggers the preset character sequence, output stops.
| Comparison Item | Ordinary Prompt Constraint | Stop Sequence |
|---|---|---|
| Control method | Uses natural language to describe the task and limitations | Uses a predefined character sequence to set a stop boundary |
| Trigger condition | Depends on the model understanding and following instructions | The generation process triggers the preset stop signal |
| Suitable scenarios | Defining content scope, tone, format, and quality requirements | Controlling format boundaries, paragraph boundaries, and fixed delimiters |
| Limitation | Instructions may not always be followed completely | Only solves “when to stop” and cannot guarantee content quality |
Therefore, a stop sequence should not replace prompt design. A more reasonable approach is to use the prompt to specify the task, format, and content scope, and then use a stop sequence to control the output boundary.
How to Choose an Appropriate Stop Sequence?
A stop sequence can be any character sequence, but that does not mean it can be chosen arbitrarily. A good stop sequence should be as unlikely as possible to appear in the normal body of an answer; otherwise, the model may stop too early, resulting in incomplete output.
Selection Principles
- Prefer character sequences that are unlikely to appear naturally in the main text.
- Avoid using common punctuation, ordinary words, or high-frequency phrases as stop sequences.
- If the output has a fixed format, choose tags, separator lines, or special markers that align with the format boundaries.
- Make sure the stop sequence does not conflict with the main content, example content, or user input.
- Before formal use, test the stopping effect with typical inputs and observe whether premature truncation or failure to stop occurs.
Checklist
| Check Item | Question to Confirm |
|---|---|
| Uniqueness | Does this sequence rarely appear naturally in the model’s main text? |
| False trigger risk | Could user input, examples, or common answers contain it? |
| Format consistency | Is it consistent with the boundary position in the target output format? |
| Debuggability | If the model stops prematurely, is it easy to identify which sequence was triggered? |
| Maintainability | Will it still apply after later modifications to the prompt or format? |
Key point: The more common a stop sequence is, the more likely it is to cause premature stopping; the closer a stop sequence is to a real format boundary, the easier it is to control output stably.
Typical Application Scenarios for Stop Sequences
Stop sequences are suitable for tasks with clear boundaries and relatively fixed output structures. In prompt engineering, structured output, and agent workflows, they can all serve as a mechanism for controlling the boundaries of model generation.
| Scenario | User Need | Solution | Effect |
|---|---|---|---|
| Prompt engineering | Output only the target answer, without additional explanations | Set a stop sequence after the target segment | Reduces unnecessary notes or subsequent expansion |
| Structured output | Maintain tag, paragraph, or delimiter boundaries | Use a fixed boundary symbol as the stop signal | Helps the output stop at the specified structural position |
| Multi-turn workflows | Prevent the model from continuing to generate the next turn’s content | Use turn markers or boundary markers to control stopping | Reduces the probability of cross-turn generation |
| Agent workflows | Distinguish task turns, action outputs, or subsequent nodes | Use a stop sequence as one boundary control mechanism | Makes it easier for the workflow to determine where the current output ends |
In agent-style applications, a stop sequence usually does not work alone, but is used together with prompts, task state, action formats, and external workflow control. It can help the system identify output boundaries, but it is not responsible for determining whether the task has truly been completed.
What Should You Pay Attention to When Using Stop Sequence?
A stop sequence is a practical means of output boundary control, but its effectiveness depends on sequence selection, prompt format, and task design. When using it, you can check according to the following approach:
- First clarify where you want the model to stop, such as at the end of a paragraph, the end of a tag, before a delimiter, or at the end of a workflow node.
- Then choose a character sequence that is unlikely to appear naturally in the main text.
- Keep format boundaries consistent in the prompt, and avoid having the model generate main-text content that conflicts with the stop sequence.
- Test with real task examples to check whether it stops too early, fails to stop, or truncates key information.
- If output quality is unstable, do not only adjust the stop sequence; also check whether the prompt clearly specifies the output content and format.
Overall, Stop Sequence is suitable for solving boundary problems in model output. It can help control response length and structure, but it cannot replace complete prompt engineering design. In practical applications, it should be treated as a mechanism for “controlling where the model generation stops,” rather than a mechanism for “guaranteeing that the model’s answer is correct.”