Zenaique

Define a stop sequence in an LLM API call

Flashcard·Easy·4.0 · 0·~30s·Asked atAi4bharatBaiduHaptik·Relevant atOpenAI
Attempt it
TL;DR

A stop sequence is a string the server matches against decoded output during generation; when it matches, decoding halts and the matching tokens are stripped from the response.

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

Picture a child reading a book aloud. You tell them: stop the moment you read the word `THE END`. They keep reading, watching for that exact phrase, and the instant they say it, they snap the book shut. A stop sequence is the same instruction handed to an LLM. The runtime watches every token as it streams out and compares the recent suffix against the list of stop strings you provided. The moment one matches, generation halts. The matching string itself gets snipped off before the response is returned, so you get a clean cut. People use this to end a tool call cleanly, to stop at a section heading, to prevent the model from pretending the user said something next, or to honor any custom delimiter their format uses.

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.

Three things can stop an LLM generation: the model decides it is done and emits its EOS token, the caller's max_tokens cap is reached, or a caller-supplied stop sequence matches the output. The third one, stop sequences, is the most flexible and the most often misused. It is also the source of subtle bugs in production streaming systems.

This deep dive covers what a stop sequence is, where in the decoder it actually fires, how it interacts with tokenization and streaming, how providers surface it in their API responses, and the situations where it is the right tool versus the wrong one.

The mechanics: where the check happens

A stop sequence is a string the runtime checks against the model's decoded output after every generation step. The check happens after the sampler picks a token and that token is appended to the output buffer, but before the token is flushed to the client.

The key implementation choice is that the match is against the detokenized text, not against token IDs. This matters because the stop string the caller provides (</tool>, \n\nUser:, ---) is rarely a single BPE token. It typically spans two or three tokens, and which tokens specifically depends on what came before in the context. Matching at the token-ID level would require enumerating every possible token sequence that detokenizes to the stop string, which is intractable for a real tokenizer.

So the runtime maintains a running decoded output, appends each new token to it, and runs a substring-suffix check against each configured stop string. The cost is small: a few microseconds per step against the model's tens of milliseconds per token. Engines like vLLM and TGI keep a tail buffer (the last 50 to 100 characters of decoded output) and check that buffer rather than the full response, to keep the substring search bounded.

When a match fires, the runtime trims the matched suffix from the output and ends the generate loop. The response carries finish_reason: stop and (in providers like Anthropic) the matched stop string is surfaced for debugging.

Three termination mechanisms, one request
Streaming, lookahead buffers, and visible pauses
Typical uses and provider conventions
When stop sequences cause bugs
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.

  • OpenAI Chat Completions accepts up to four `stop` strings; if any appears in the output, generation halts and `finish_reason` is `stop`.
  • Anthropic Messages exposes `stop_sequences` and reports the matched string in `stop_sequence` on the response for debugging.
Sign in to see more production examples.

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

QHow does a stop sequence work when the stop string spans two BPE tokens?
A

The runtime matches against the detokenized output, not against token IDs. Each step decodes the recent tail to a string and runs a substring check. This is why short stops can cause the runtime to buffer one or two tokens during streaming before flushing.

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

Conflating stop sequences with `max_tokens`. `max_tokens` is an unconditional length cap; a stop sequence fires only when specific content is generated. Both can apply to one request.

Sign in to see all red flags and common mistakes.

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

  • Define a stop sequence.

  • Explain when the runtime checks the output suffix.

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
What is the KV cache in transformer inference?
Flashcard·Easy