Zenaique

Spot the errors in this stop sequence configuration recommendation

Spot the error·Hard·4.0 · 0·~2 min·Asked atHugging FacePhonepeUber·Relevant atOpenAI
Attempt it

Click any words you think contain an error. Click again to unmark.

Mark at least one word to submit.
TL;DR

Stop sequences match the detokenized output string, not token IDs; tokenization is context-sensitive, so whitespace anchors matter and a wrong stop config plus no max_tokens means runaway generation and high cost.

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

Imagine telling a dictation assistant to stop the moment it writes the word 'Stop'. You might assume 'Stop' is always the same secret code inside the machine. But the machine breaks words into chunks, and which chunk it uses depends on what came just before, a space, a newline, or the very start. So checking the secret code number is unreliable. Instead, the assistant should write out the actual letters and look at the visible text for the word 'Stop'. And if you never tell it a maximum length and the word never appears, it keeps writing forever, running up your bill. The safe setup matches the readable text and always caps the length.

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 handling connects tokenizer behavior to the serving bill. The recommendation makes three wrong claims in four sentences, and each one leads to a real serving failure. Under those errors sits a larger one: a stop configuration that never fires, combined with no length cap, produces runaway generation and a large cost increase.

Three points explain the errors. First, how a runtime detects a stop: it decodes recent output to text and looks for the stop string; it does not compare token IDs. Second, why it must work that way: BPE tokenization is context-sensitive, so a substring has no single stable ID. Third, why whitespace in a stop string matters: the leading newline ties a stop to a turn boundary.

Termination is guaranteed by the trained EOS token and a hard max_tokens cap, not by the advisory stop strings.

Error one: stop matching runs on decoded text, not token IDs

The recommendation says the stack compares every generated token ID to the ID for 'Human:' and halts on a match. That is not how production runtimes work. The serving layer keeps a sliding window of the most recently generated tokens, decodes that window back into a string, and checks whether the string contains one of your stop sequences. The comparison happens in text space.

The reason is mechanical. There is no single ID for an arbitrary string, so there is nothing stable to compare against. A stop sequence is a sequence of characters the user wants to halt on, and the only reliable place those characters exist unambiguously is in the decoded output. Open engines such as vLLM work this way: vLLM accepts stop strings only when detokenization is on, searches the decoded text, and by default returns the output without the stop string.

This also explains why stops are described as best-effort. The match runs on a window, partial tokens at the boundary are handled carefully, and the decoded string is what the user finally sees. Matching on IDs depends on a fixed string-to-ID mapping that does not exist.

Streaming adds one more detail. Because the runtime decodes a window and searches it, a stop string can straddle two generated tokens, and the runtime must hold back partial output until it is sure no stop is forming. This is why naive token by token streaming sometimes leaks a fragment of the stop string before catching it. Good implementations buffer the tail until the match decision is unambiguous, which is only possible when you reason in decoded-text space rather than over raw IDs.

Error two: tokenization is context-sensitive, so IDs are not stable
Error three: whitespace is significant, the newline is the anchor
The correct termination model: EOS plus a hard max_tokens cap
Designing a robust stop configuration
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.

  • vLLM's SamplingParams accepts stop strings only when detokenization is on and holds back a few characters of output while it checks them, because the match runs on text, not token IDs.
  • The Anthropic Messages API returns stop_reason 'stop_sequence' with the matched string in the stop_sequence field, and also reports 'end_turn', 'max_tokens' and other reasons, so the terminator is explicit per response.
Sign in to see more production examples.

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

QWhy does a stop string that begins in the middle of a token defeat a matcher that works on token IDs?
A

The model emits whole tokens. If the stop string starts inside a merged BPE token, no sequence of token IDs lines up with the string, so an ID-based matcher never fires. A matcher on the decoded text still finds the literal characters, because it compares characters, not tokens. Anchoring stops on whitespace or a known boundary also keeps the string aligned with token boundaries.

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

Assuming a string maps to one stable token ID and matching on IDs, then shipping with no max_tokens. A missed stop with no cap means runaway generation and a large cost increase.

Sign in to see all red flags and common mistakes.

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

  • Why stop matching runs on decoded text rather than raw token IDs

  • How leading context changes the token ID for the same substring

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