Inferensys

Glossary

Stop Sequence

A predefined string of characters that signals a language model to halt generation, used to prevent rambling and ensure output ends cleanly at a structural boundary.
ML engineer working on model compression and quantization, laptop showing performance benchmarks, technical workspace.
GENERATION CONTROL

What is a Stop Sequence?

A stop sequence is a predefined string of characters that signals a language model to immediately halt the token generation process, ensuring the output terminates cleanly at a structural boundary.

A stop sequence is a specific string of text, such as ### or </output>, designated as a termination signal during model inference. When the language model generates this exact sequence, the decoding loop breaks instantly, preventing the model from continuing to produce extraneous or rambling text beyond the intended structural endpoint.

This mechanism is critical for structured output formatting and API integrations, where a response must end precisely after a JSON object or a specific section. By defining stop sequences, developers enforce syntactic boundaries without relying on post-processing truncation, ensuring the raw output is immediately parseable and free of trailing artifacts.

TERMINATION CONTROL

Key Characteristics of Stop Sequences

Stop sequences are predefined character strings that act as a hard boundary for language model generation, ensuring outputs terminate cleanly at structural or semantic endpoints rather than rambling into irrelevance.

01

Structural Boundary Enforcement

Stop sequences serve as explicit termination signals that prevent models from generating beyond a defined structural boundary. When the model emits the stop token, generation halts immediately.

  • Common stop sequences include \n\n, ###, </output>, or custom delimiters
  • Essential for JSON mode where a closing brace } or bracket ] must be the final character
  • Prevents the model from appending conversational filler like 'I hope this helps!' after structured data
  • Works in conjunction with token masking to physically block continuation tokens
02

Token-Level Halting Mechanism

Stop sequences operate at the token level, not the character level. The model's tokenizer must recognize the stop sequence as a distinct token or token sequence to trigger termination.

  • A stop sequence like \n\n may be a single token or two consecutive newline tokens depending on the tokenizer
  • Logit bias can be applied to suppress tokens that would extend generation beyond the stop point
  • In guided decoding, the finite state machine transitions to a terminal state when the stop sequence is emitted
  • Mismatched tokenization can cause stop sequences to fail if the model never generates the exact token pattern
03

Multi-Sequence Stop Conditions

Production systems typically define multiple stop sequences to handle different termination scenarios within a single generation call.

  • A JSON generation might stop on } for normal completion, \n\n for error truncation, and a custom [ERROR] token
  • Stop sequences are evaluated incrementally as each token is generated, not retroactively on the full output
  • Frameworks like Outlines and LMQL allow defining stop conditions as part of the grammar specification
  • Multiple stop sequences prevent the model from getting stuck in repetitive loops by providing multiple escape paths
04

Chain-of-Thought Termination

In chain-of-thought structuring, stop sequences delineate the boundary between reasoning traces and final answers, ensuring the model doesn't blend the two.

  • A stop sequence like [ANSWER] signals the transition from internal reasoning to user-facing output
  • The ReAct agent format uses Observation: as both a structural delimiter and an implicit stop for the previous section
  • Without proper stop sequences, reasoning traces can leak into final outputs, creating confusing responses
  • Stop sequences enable structured data extraction by cleanly separating the model's scratchpad from its structured payload
05

Infinite Generation Prevention

Stop sequences act as a critical safety mechanism against runaway generation that consumes compute resources and produces garbage output.

  • Combined with a max_tokens parameter as a hard upper bound for defense-in-depth
  • Models without stop sequences can enter repetitive loops, generating the same phrase indefinitely
  • Grammar-constrained generation uses stop sequences as terminal symbols in the formal grammar definition
  • In API deployments, stop sequences prevent billing overages from excessively long generations
06

Schema-Aware Termination

When generating structured outputs against a JSON Schema, stop sequences must align with the schema's expected closing delimiters to produce valid, parseable results.

  • A schema defining an array of objects expects ] as the natural stop point after the final element
  • Schema validation fails if generation stops prematurely before required fields are emitted
  • Pydantic and Instructor libraries implicitly manage stop sequences based on the target model's structure
  • Schema drift can occur if stop sequences are hardcoded while the expected output format evolves
STOP SEQUENCE

Frequently Asked Questions

Clear answers to common questions about using stop sequences to control language model generation and ensure clean, structurally sound outputs.

A stop sequence is a predefined string of characters that signals a language model to immediately halt the token generation process. When the model's output stream matches the exact stop sequence, the inference engine terminates generation, preventing the model from producing extraneous or rambling text beyond a desired structural boundary. This mechanism is implemented at the decoding layer, where the generated text is continuously scanned for the presence of the stop token. Common stop sequences include ###, <|endoftext|>, </s>, or custom delimiters like [END]. By defining a stop sequence, developers enforce a clean termination point, which is critical for structured output formatting tasks where a precise schema boundary must be respected.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.