Course contentsShow
← AI Engineering
Lesson 141 of 1,8864. Prompt Engineering FoundationsFree lesson

Stop Sequences and Early Termination

Using stop sequences to halt generation at specific markers for controlled output boundaries.

Stop Sequences and Early Termination

What you'll learn: How to use stop sequences to automatically halt text generation at specific markers, giving you precise control over where the output ends.

What are Stop Sequences?

A stop sequence is a specific string of text that tells the model "stop generating immediately when you encounter this." Think of it like setting a finish line in a race—the moment the model writes that exact sequence, generation halts, and everything up to (but not including) that marker is returned.

This is different from max tokens (which sets a length limit). Stop sequences let you end generation based on content rather than just length.

Why Use Stop Sequences?

Imagine you ask a model to generate a list of three items. Without a stop sequence, it might keep going and generate four, five, or more items. By setting stop=["\n4."], you ensure it stops before writing a fourth item.

Common use cases:

  • Structured output: Stop at "---" to mark section boundaries
  • Dialogue: Stop at "\nUser:" to prevent the model from continuing a conversation on both sides
  • Code snippets: Stop at "```" to end a code block cleanly
  • Lists: Stop at the next numbered item you don't want

The Analogy

Think of stop sequences like automatic doors with motion sensors. You set specific "trigger words" that act as sensors—when the model generates text that trips the sensor, the door closes (generation stops). Without these sensors, the door stays open indefinitely (or until max tokens).

How It Works with Other Parameters

Stop sequences work alongside temperature, top-p, and max tokens. The model generates text token-by-token until it either:

  1. Hits a stop sequence (stops immediately)
  2. Reaches max tokens (stops at length limit)
  3. Naturally completes (model generates an end-of-sequence token)

Key Takeaway: Stop sequences give you content-based control over generation boundaries, letting you halt output precisely at logical breakpoints rather than arbitrary token counts.