Zenaique

Flashcard: what is a stop sequence in an LLM API call and what is it used for?

Flashcard·Easy·4.0 · 0·~30s·Asked atDeepseekLyzrMckinsey·Relevant atAnthropic
Attempt it
TL;DR

A stop sequence is a string the model halts generation on the moment it produces it, used to enforce structural boundaries in output, distinct from max-tokens which caps length.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine asking a friend to tell you a story but to stop the second they say the end. A stop sequence is exactly that instruction for an LLM. You hand the API a string, and the model writes one token at a time, checking after each step whether the running output now ends with your stop string. The moment it does, generation halts and that stop string is sliced off before the reply is returned. This is different from telling the friend stop after exactly two minutes, which is what max-tokens does. The stop sequence is a content rule. Max-tokens is a length rule. You usually want both: the stop sequence is the intent, max-tokens is the safety net in case the model never reaches the stop string.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Stop sequences look like a trivial API parameter until you try to ship a structured-output prompt and discover the model keeps generating past the boundary you wanted. The model does not natively know about your downstream parser; you have to tell it where to stop. The stop sequence is how you tell it.

This deep dive defines stop sequences, walks through the difference from max-tokens, covers the three production patterns where stop sequences are load-bearing, and names the provider-specific gotchas that catch real teams.

What a stop sequence is and how it is enforced

A stop sequence is a string (or a list of strings) you pass to the LLM API alongside your prompt. During generation, the inference loop decodes one token at a time, appends it to the running output, and after each step checks whether the running output ends with any of the stop strings. On match, decoding halts immediately, the stop string is removed from the returned text, and the response is finalized.

The enforcement is at the inference level, not the prompt level. You can tell the model in the system prompt to stop when it produces some delimiter, but model compliance with prose-level instructions is probabilistic. The stop sequence parameter is deterministic: the inference loop will halt the moment the match condition is true, regardless of whether the model wanted to continue.

APIs vary in how many stop strings they accept. OpenAI's chat completions cap at 4. Anthropic accepts a longer list. Most providers treat the list as an OR: generation halts on whichever stop string appears first in the output. The order you pass them in does not matter; the first one the model produces wins.

Why max-tokens is not a substitute
The three production patterns
Provider-specific gotchas
Designing a good stop sequence
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Anthropic's Claude API accepts a stop_sequences array; the response payload reports stop_reason=stop_sequence when one fires, letting clients distinguish clean termination from length-cap termination.
  • OpenAI's chat completions API accepts up to 4 stop strings via the stop parameter; the finish_reason field reports stop on a successful match.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does Anthropic match stop sequences on character boundaries while OpenAI matches on token boundaries, and what difference does it make?
A

Character boundaries are more forgiving when your stop string starts inside a token; token boundaries are faster but require the stop to align with the tokenizer's output. Test multi-token stop strings with the actual tokenizer.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Confusing stop sequences with max-tokens; one is a content boundary (terminate on string match), the other is a length cap (terminate after N tokens regardless of content).

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • What a stop sequence is (string trigger that halts generation)

  • How it differs from max-tokens (content vs length trigger)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
When is self-refine (LLM…
MCQ·Medium