Homeglossary

Stop Sequence

A stop sequence is a text pattern that tells a language model when to stop generating output. It acts as a boundary for an AI response, such as a speaker label, closing tag, or custom marker. Because matching is usually exact and provider behavior varies, teams must test stop sequences with real prompts and model responses.

Build controlled AI app responses with Emergent.

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.

  1. Define distinctive strings, such as </answer> or \nUser:.
  2. The model generates text incrementally as tokens, or small text pieces.
  3. The API checks generated text for a configured stop sequence.
  4. 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.

ControlPurposeSet by
Stop sequenceEnds output at an application-defined text boundary.Developer or application
Stop tokenMarks the model's built-in end of a response.Model provider
Maximum output tokensCaps 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.

  1. Choose a distinctive delimiter unlikely to appear in ordinary language.
  2. Use it consistently in prompts, examples, and parsing logic.
  3. Test normal, long, adversarial, and empty-result cases.
  4. Log finish reasons and raw responses when available.
  5. 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.

Start Building
on Emergent today
Start Building