Stop Sequence
What Is a Stop Sequence?
A stop sequence is a text pattern that tells a language model when to stop generating output. It creates a boundary for an AI response, such as a speaker label, closing tag, or custom marker. Unlike a maximum output-token limit, which stops at a set size, a stop sequence stops when a specific pattern appears.
How Stop Sequences Work in LLMs
An application sends stop sequences with the prompt and generation settings.
- Define distinctive strings, such as
</answer>or\nUser:. - The model generates text incrementally as tokens, or small text pieces.
- The API checks generated text for a configured stop sequence.
- When it finds a match, generation ends. Whether matched text is included depends on the provider.
Stop Sequence vs. Stop Token vs. Maximum Output Tokens
These controls work together but serve different purposes.
| Control | Purpose | Set by |
|---|---|---|
| Stop sequence | Ends output at an application-defined text boundary. | Developer or application |
| Stop token | Marks the model's built-in end of a response. | Model provider |
| Maximum output tokens | Caps response length even if no boundary appears. | Developer or application |
A stop sequence can improve formatting but does not guarantee valid JSON, XML, or other structured data.
Common Stop Sequence Use Cases
Stop sequences help an AI return one bounded part of a larger format.
- End an assistant turn before it writes
User:. - Stop a transcript at a label such as
Speaker 2:. - End a response section with
</output>. - Prevent extra explanation after a machine-readable block.
Benefits of Stop Sequences
Well-chosen boundaries make output easier for software and people to handle.
- Reduce unwanted continuation into another chat turn or workflow step.
- Can reduce generated tokens when output ends early.
- Create cleaner handoffs between AI agents and application components.
- Make interfaces more predictable.
Practical Limits and Common Failure Modes
A stop sequence is a formatting control, not a reliability or security guarantee.
- Matching is often exact and case-sensitive, so small differences can prevent stopping.
- A common word may appear naturally and cut output short.
- If output begins with the marker, the response may be empty.
- Parameter names, supported sequence counts, and streaming behavior vary by API.
- Validate machine-readable output separately, and never rely on a stop sequence to block unsafe content.
How to Choose and Test a Stop Sequence
Results depend on the model, prompt, and API integration.
- Choose a distinctive delimiter unlikely to appear in ordinary language.
- Use it consistently in prompts, examples, and parsing logic.
- Test normal, long, adversarial, and empty-result cases.
- Log finish reasons and raw responses when available.
- Add schema validation and retry handling for structured output.
Stop Sequences in AI Application Development
Stop sequences help teams build reliable AI features when one generated step feeds another. Learn more about app design and development, evaluate an AI coding assistant, or browse the Emergent glossary for related concepts. Emergent helps turn bounded AI behavior into working web and mobile applications through agentic development workflows.
on Emergent today


