Practical technical guides / AI engineering

Stop Sequences Can Silently Cut Your Model's Answer Short

A stop string that also occurs in the answer ends it early, and most APIs report the same finish value as a complete answer. Measured on Ollama.

By Sandeep Belgavi · Published 2026-12-01

A stop string that also occurs in the answer ends it early, and most APIs report the same finish value as a complete answer. Measured on Ollama.

A stop string is a content filter you did not mean to write

Several major model APIs support stop sequences on compatible models: strings that end generation as soon as the model produces them. Support varies by endpoint and model; check the selected model before sending the parameter. They are older than chat APIs. Completion-style prompting used them to stop a model from writing the next turn of a transcript, and many code paths still set them by habit: a blank line, a triple backtick, Observation: in a ReAct loop, or </answer>.

The problem is simple to state. A stop sequence does not know what the model meant. If the string also occurs inside a legitimate answer, generation ends there, and the caller receives the first part of the answer. Worse, most APIs report that ending with the same value they use for a natural finish, so the caller cannot tell a complete answer from a cut one. This guide measures that on a local model, shows what each major API tells you, and gives three ways to make the cut visible.

The running example is a support-writing service at Belgavi's Book Store. It drafts short help articles, some of which include a code sample for the store's partner API. Dinky added a stop sequence of three backticks, the opening of a Markdown code fence, months ago to keep a model from rambling after a code block in an older template.

Measured: one prompt, three stop settings

The companion example stop_sequence_cut_probe sends one prompt to two local models through Ollama 0.40.18, temperature 0 and a fixed seed. The prompt asks for two short paragraphs on checking an order status, followed by a three-line Python example in a fenced code block. No hosted provider was called.

Model and settingCharacters returnedTokens generateddone_reasonContains the code block
qwen2.5 0.5b, no stop1,481342stopyes
qwen2.5 0.5b, stop at a code fence559134stopno
qwen2.5 0.5b, stop at a blank line6916stopno
qwen2.5 0.5b, num_predict 208020lengthno
qwen2.5 1.5b, no stop2,268505stopyes
qwen2.5 1.5b, stop at a code fence959221stopno
qwen2.5 1.5b, stop at a blank line6916stopno
qwen2.5 1.5b, num_predict 207920lengthno

With the fence as a stop string, both models wrote their prose, introduced the example, and were stopped at the opening of the code block. The 0.5b answer ends with "demonstrates how to check an order status:" followed by a blank line. With a blank line as the stop string, both models stopped after one sentence: "you can follow these steps:". In every stop case, done_reason was stop, exactly the value returned for the complete answers. Only the token limit produced a different value, length. The OpenAI-compatible endpoint of the same server behaved the same way: finish_reason was stop for the complete answer and for the fence-truncated one, and length for the token limit.

What each API tells you

The ambiguity is documented, not an Ollama quirk.

  • OpenAI Chat Completions. The reference says finish_reason "will be stop if the model hit a natural stop point or a provided stop sequence". The two cases share one value.
  • Gemini API. The FinishReason enum describes STOP as "Natural stop point of the model or provided stop sequence".
  • Anthropic Messages. The response separates them. stop_reason is end_turn for a natural finish and stop_sequence when one of your sequences fired, and the stop_sequence field says which one.
  • Ollama. done_reason is stop in both cases, as measured above, and length when num_predict ends the response.

So on three of the four, a caller that checks the finish reason, as it should for token limits, still cannot detect a stop-sequence cut.

Three ways to make the cut visible

1. Remove stop strings that can occur in content. Most modern chat templates end the assistant turn with a special token, and the server stops there without any user-supplied string. Delete legacy stop sequences, then re-run your evaluations. If a template needed a stop string to prevent a runaway turn, a token limit plus a check of length is a safer bound.

2. Ask for an end marker and do not stop on it. Tell the model to finish with a sentinel line such as <<END-OF-ARTICLE>>. Do not pass it as a stop sequence. A complete answer ends with the marker, which you strip. An answer without it fails the completion contract: it may have been cut, or the model may simply have omitted the marker. A present marker alone cannot prove semantic completeness.

SENTINEL = "<<END-OF-ARTICLE>>"

def finish(text: str, finish_reason: str) -> str:
    if finish_reason == "length":
        raise ValueError("cut by the token limit")
    body = text.rstrip()
    if not body.endswith(SENTINEL):
        raise ValueError("no end marker: the answer was cut or the model ignored the instruction")
    return body[: -len(SENTINEL)].rstrip()

This costs a few tokens per response and turns a silent truncation into an error you can count. A model that ignores the instruction also shows up as an error, which is the right failure to see in evaluation.

3. Check the structure you asked for. If the prompt asks for a code block, require an even number of fences and at least one block. If it asks for numbered steps, require the count. Structural checks catch cuts that happen to end on a full sentence, which a reader would not notice.

On the Anthropic API you can also read stop_reason and stop_sequence directly, which is the cleanest signal when it is available. Keep the structural check anyway, because a stop sequence that fires at the very end of a complete answer and one that fires halfway look the same in that field too.

Where stop strings bite in agent code

ReAct-style loops often stop on Observation: so the model cannot invent a tool result. If a tool result quoted back into the conversation contains that word, or a model writes it inside an argument, the call is cut mid-argument and the parser fails in confusing ways. Native tool calling removes the need for that stop string, because the API returns tool calls as structured objects.

Templates that stop on a blank line to keep answers to one paragraph break as soon as a model adds a heading or a list. The measured blank-line rows show how short the result can be: 16 tokens.

Stop strings copied between providers are another source. A string that was harmless with one model's style can occur constantly in another's. Re-check stop settings when you switch models, not only prompts.

When a stop sequence is the right tool

Stop sequences remain useful when you control both the format and the content, for example extracting the first line of a generated label, or stopping a base model at a delimiter it will never emit inside the content. The rule is to choose strings that cannot appear in a valid answer and to verify completeness in another way.

How to verify it

Search your code and configuration for every place a stop parameter is set, including SDK defaults and framework templates. For each one, run your evaluation set with and without it and compare answer lengths and structural checks. Track the rate of answers missing their end marker or failing their structure check as a production metric next to the length rate. The companion example reproduces the table above against a local Ollama server and checks that the truncated answers carry the same finish value as complete ones; run it against your own server and model before trusting either.

Primary references and authorship

Sources checked 2026-12-01. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/stop_sequence_cut_probe. Views are my own and do not represent any employer, past or present.