Basics

What is a stop sequence — and how to tell a complete answer from a truncated one

Illustration: a ribbon of text snipped at a marker

Here's an annoying one. Two answers come back from the model, and both stop mid-sentence. The first stops because you asked it to — everything's fine, the job is done. The second stops because it ran out of budget — half the answer is gone.

You can't tell them apart from the text. Not at all. But the API response has a field that tells you the truth — and most beginners have never heard of it. Let's dig in.

What a stop sequence is, in plain words

Start with the non-obvious part: the model doesn't know when to shut up.

It doesn't write the answer all at once. It emits one token at a time and decides what comes next after each one. Stopping is also a decision, and sometimes it doesn't make it: it keeps going, invents your next question for you, starts a dialogue with itself.

A stop sequence is an emergency brake made of text. You say up front: "the moment you generate this exact string — stop immediately." The model hits it and goes quiet.

In the API it's a separate request parameter. Anthropic calls it stop_sequences, OpenAI calls it stop. You pass a list of strings: "\n\nHuman:", "###", "</answer>".

The classic case: you ask the model to play a role in a dialogue, and it writes both its line and your next one. Set a stop sequence of "\nYou:" and it halts exactly where your turn should begin.

Why the stop string never appears in the answer

Here's the trap almost everyone falls into.

The stop sequence itself is not included in the returned text. It fires as a trigger and gets cut off.

Sounds reasonable — until you use something like a double line break, "\n\n", as your stop string. You meant "don't write extra paragraphs," and you got a one-paragraph answer: the model finished the first paragraph, emitted a blank line, and that was that. The text is chopped at the first match.

The rule is simple: a stop string must be something that can't show up in a normal answer by accident. "###" or "</end>" — good. A period, a line break, the word "So" — bad.

It's not the same thing as max_tokens

There's a neighbouring parameter, max_tokens — the maximum answer length. People mix them up, but they work differently:

  • max_tokens is a blunt fuse. "Don't emit more than N tokens, whatever you're doing." The model isn't aware of this limit and doesn't wrap up its thought — it just gets cut off mid-word.
  • A stop sequence is a semantic boundary. "End it right here." The cut lands somewhere meaningful.

Which leads to the key practical point: since both of them chop the text, you need a way to know which one fired.

How to tell a complete answer from a truncated one

The API response carries a field with the reason for stopping. That's your source of truth.

Anthropic calls it stop_reason:

  • end_turn — the model finished on its own. Complete answer.
  • stop_sequence — your stop string fired. Complete answer, the cut was planned.
  • max_tokens — hit the length limit. The answer is truncated.
  • tool_use — the model wants to call a tool.
  • refusal — the model declined to answer.

OpenAI calls the field finish_reason with different values: stop, length, tool_calls, content_filter. Note the difference: length is the equivalent of max_tokens, but OpenAI's stop merges two different cases — "the model finished on its own" and "it hit your stop string." Anthropic separates them; OpenAI doesn't. If the distinction matters to you, on OpenAI you'll have to infer it from the text.

The practical minimum for any code that calls a model:

  1. Got a response — look at stop_reason / finish_reason first.
  2. See max_tokens / length — don't present the text as finished. Either raise the limit and retry, or say honestly "the answer didn't fit."
  3. Everything else — safe to show.

That one step is the difference between an app that occasionally lies to users with truncated text and an app that doesn't. More on the symptom itself in the breakdown of why AI cuts off mid-sentence.

When you actually need a stop sequence

Honestly: less often than you'd think. Modern models stop themselves reasonably well, and "just ask in words" usually beats a technical workaround — the same story as with temperature and the other knobs.

Stop sequences stay useful in three situations:

  • Role-based dialogue parsing, when the model insists on writing both sides.
  • Strict output formats — you wrap the block in a marker yourself and cut on it.
  • Cost. Generation halts on the spot, so tokens after the stop string are never generated and never billed.

For everything else, try explaining the task in the prompt first. The knob is for when words didn't work.

How many stop sequences can I set?

Usually several — the parameter takes a list of strings, and whichever appears first wins. The exact cap depends on the provider, so check your model's docs. In practice one or two covers nearly everything.

Does a stop sequence save money?

A little. Everything after it is simply never generated, so you don't pay for output tokens you'd have thrown away anyway. But that's a nice side effect, not the point: it saves nothing on input tokens.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →