A stop sequence is a string sent with an API request so that a language model's output ends where that string first appears. Models produce text in tokens, and a stop string does not have to line up with them. In sample outputs we wrote and split into tokens with OpenAI's public tiktoken 0.14.0 encodings, a stop string began inside a token in 19 of the 47 places it occurred, yet the stop logic of llama.cpp v1.0.0's server still cut exactly before the stop in all 47. The API references also differ on how many stops a request may carry. As read on 20 November 2026, OpenAI's Chat Completions takes up to 4 stops, Google's Gemini up to 5, and DeepSeek up to 16 of at most 512 UTF-8 bytes each, while the other three references we read give no figure. OpenAI's Responses API has no stop parameter.
The stop </answer> ends inside the token >\n, so the server trims the newline
The test set is small. It holds 12 outputs we wrote in English, Chinese and Malay, and 24 common stops such as \n, Question: and Soalan:. We tokenised each text with o200k_base and cl100k_base from tiktoken and fed the tokens one at a time to the stop code of llama.cpp v1.0.0, copied verbatim and compiled. The token streams illustrate tokenisation, and are not what any model emits.
The stop occurred in 47 of the text and stop pairs. In 19 of them, under both encodings, it began partway through a token; in 9 under o200k_base and 8 under cl100k_base it ended inside one. In all 47 the server, which matches stop strings against the decoded text, returned exactly the text before the stop string. When a stop ended inside a token, the rest of that token went too. Both encodings put the newline after </answer> in the same token as the tag's closing bracket, and the server discarded it along with the stop. In the Traditional Chinese sample, the stop 答案: ended inside the o200k_base token :約, and the server trimmed 約, which had arrived in the same token.
The start of the stop 答案: never reaches a streaming client before the stop fires
Our Simplified Chinese Q&A sample uses the stop 答案: (dá'àn, answer, and a colon). Under cl100k_base, the character 答 arrived as two byte fragments, then 案, then the full-width colon. The first fragment sent nothing, because the character was not yet complete. After the second fragment, after 案 and after the colon, the server sent no text: it held back what could still begin the stop, and the colon completed it. The server used 28 of the 50 tokens a full generation of that text would take; under o200k_base, 17 of 31.
Chinese text can also wait on its encoding, because llama.cpp sends nothing until a character's UTF-8 bytes are complete. Of the tokens in our Chinese texts, 86 of 208 under cl100k_base were not valid UTF-8 on their own, against 2 of 137 under o200k_base. The vLLM server documents a similar hold-back. In sampling_params.py at v0.34.0, read and not run, a request with stops by default holds back one character fewer than its longest stop until the sequence is finished.
A shorter stop that finishes first beats a longer one that started earlier
The llama.cpp server checks after each token and, among the stops already complete, keeps the one that starts earliest. Take the text Note: What is it? with two stops, the longer Note: What is and the shorter What. The shorter stop completes while the longer one is still half-written, so the server cuts there and keeps Note: . A rule that looked only at the finished text and picked the earliest start would cut at the longer stop and keep nothing. Two more pairs we built, 用户:你好吗?助手 against 你好 and Result: {"ok" against {, behaved the same way.
Across 170 ordered pairs from our ordinary stops where both occurred, the two rules never disagreed. When both stops complete inside the same token, the earlier-starting one wins even though it finished later. Gemini's reference says output stops "at the first appearance", which leaves open which of the two rules it follows.
OpenAI, Google and DeepSeek cap the number of stops a request may carry
| API | Parameter | What the reference states |
|---|---|---|
| OpenAI Chat Completions | stop | Up to 4; not supported on o3 and o4-mini |
| OpenAI Responses | none | No stop property in the request schema |
Google Gemini generateContent | stopSequences | Up to 5 |
| DeepSeek Chat Completions | stop | Up to 16, each at most 512 UTF-8 bytes |
| Alibaba Model Studio (Qwen) | stop | No limit stated |
| Mistral Chat | stop | No limit stated |
The OpenAI rows come from the platform.openai.com API reference, read through its published OpenAPI specification, version 2.3.0. DeepSeek documents a Responses API too; the table's Responses row describes OpenAI's alone. Google's generateContent reference says the field takes "character sequences (up to 5)", and DeepSeek's reference requires each stop to be "a non-empty string of at most 512 bytes (UTF-8 encoded)". Alibaba Cloud's "Qwen API via OpenAI Chat Completions" gives no limit, and the entry in Mistral's reference reads "Stop generation if this token is detected".
Anthropic's reply names the matched stop in a field of its own
Anthropic's Messages API reference takes stop_sequences as an array of strings and states no maximum. When one fires, stop_reason becomes "stop_sequence" and a separate stop_sequence field holds "the matched stop sequence". The page does not say whether the content itself holds the stop text.
Only OpenAI and Google say whether the stop string appears in the output. OpenAI's reference reads "The returned text will not contain the stop sequence.", and Google's reads "The stop sequence will not be included as part of the response." The DeepSeek, Alibaba and Mistral references do not address it.
A typed \n\n stays four literal characters until something reads the escape
In a JSON request body, "\n\n" becomes two newlines when the JSON is parsed, and a text box does no such parsing. Until the fix of 20 November 2026, our stop sequence tester searched for the backslash and letter pairs exactly as typed, so its own default blank-line stop never fired on a real blank line. It also split the stop box on newlines, so none of the 8 stops among our 24 that contain a newline could be entered. Positions were counted in UTF-16 code units, so an emoji counted as two.
The revision of that day reads \n, \t, \\ and similar as JSON-style escapes and counts code points. Given the text Para one., a blank line and Para two., the revised tester with its default stops cuts after Para one. It lists each stop's length in characters and in UTF-8 bytes, the unit DeepSeek's limit uses; for example, the stop 答案: is 3 characters and 9 bytes. On our arithmetic, a 512-byte stop holds at most 170 three-byte Chinese characters, since 170 take 510 bytes and 171 take 513.
The tester has no tokenizer. Its main cut follows the stop that starts earliest in the finished text, so for the Note: What is it? case above it keeps nothing. A second line flags the overlap, shows that a check after every character would keep Note: , and says that a server checking after every token can land on either cut.
An API's finish_reason can say stop on a model that ignored the stop words
In OpenAI's specification, finish_reason is "stop if the model hit a natural stop point or a provided stop sequence", and DeepSeek's reference says the same, so the value cannot tell the two apart.
Alibaba Cloud's "Qwen API via OpenAI Chat Completions" names one hosted model, MiniMax/MiniMax-M3.1-Flash, that "does not support this parameter". On that model the output "will not be truncated at the specified stop words", and the request "may still succeed" and return a finish_reason of stop.
Go further
Relevant tools
More from AI & ML