{"post":{"slug":"stop-sequences-explained-2026","title":"Stop Sequences: Controlling Where Generation Ends","excerpt":"A model stops when it emits an end token or hits the token limit. Stop sequences give you a third option, and the details of what gets returned catch people out.","category":"ai","date":"January 27, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">G</span>eneration ends one of three ways: the model emits an end-of-turn token, the output reaches the token limit, or it produces a string you nominated as a stopping point. The third is the one you control directly, and it is genuinely useful for structured extraction and for older-style formats.\n\nIt is also a common source of silent bugs, because the different ways of stopping are indistinguishable if you only look at the text.\n\n## What changed in 2026\n\n- **Structured outputs reduced the need substantially.** Schema-constrained generation terminates correctly by construction, removing the main reason people reached for stop sequences.\n- **Native tool calling removed another.** Parsing tool calls out of text with stop markers became unnecessary once structured tool-call formats arrived.\n- **Stop reason checking got emphasised.** The failure mode of treating a truncated response as complete became better documented.\n- **They remained useful for specific formats.** Extraction tasks and delimiter-based outputs still benefit.\n\n## The three stop reasons\n\n| Reason | Means | Your response |\n| --- | --- | --- |\n| End of turn | Model finished naturally | Use the output |\n| Max tokens | Hit the limit, output is truncated | Retry with a higher limit, or handle partial |\n| Stop sequence | Your marker was produced | Use the output |\n| Refusal | The model declined | Different handling entirely |\n\nThe critical one is max tokens. A response cut off mid-sentence is still a string, and code that reads only the text treats it as a complete answer. In an extraction pipeline that means a partial record written to your database with no indication anything was wrong.\n\n**Always check the stop reason before using the output.** This is a two-line change that prevents a whole class of silent data corruption, and it is skipped constantly.\n\n## Is the sequence included?\n\nBehaviour varies by provider, and getting it wrong breaks parsing in ways that look mysterious.\n\nMost implementations exclude the stop sequence from the returned text — generation stops and what you get is everything before it. Some include it. Some let you configure the behaviour.\n\nCheck for your specific provider rather than assuming, and write parsing that tolerates both where you can. If you are splitting on a delimiter that may or may not be present in the returned string, handle both cases.\n\n## Choosing a sequence\n\nThe requirement is that it cannot appear in legitimate output. A stop sequence that occurs naturally truncates valid responses, and the resulting bug is intermittent and confusing — most responses are fine, some are mysteriously short.\n\nPoor choices: common words, single newlines, punctuation, anything that appears in ordinary prose.\n\nBetter choices: distinctive multi-character markers that would never occur naturally, ideally ones you also instruct the model to emit explicitly.\n\nThe pairing matters — a stop sequence works best when the prompt tells the model to produce that marker when finished. Then the sequence is a mechanism enforcing an instruction the model already understands, rather than an external cut-off.\n\nNote also that most providers accept several stop sequences, and matching is generally on exact strings including whitespace. A sequence with a trailing space will not match output without one.\n\n## When not to use them\n\n**For length control.** Use the maximum token parameter. Stop sequences terminate on structure, not on length, and using them to approximate a length limit works badly.\n\n**For reliable structured output.** Schema-constrained generation is strictly better — it guarantees valid output and terminates correctly without delimiter parsing. If you are using stop sequences to extract JSON from a response, structured outputs solve the problem properly — see [structured outputs](/blog/structured-outputs-llm-2026).\n\n**For tool calls.** Native tool-calling formats have made stop-marker parsing obsolete for this purpose.\n\nWhere they remain genuinely useful: extraction tasks with a repeated delimiter format, generation that should stop before an example the model tends to continue into, and interoperating with formats that predate structured output support.\n\n## Common mistakes\n\n- **Not checking the stop reason.** Truncated responses treated as complete.\n- **Assuming the sequence is included or excluded.** Provider-dependent.\n- **A sequence that appears in valid output.** Intermittent truncation.\n- **Using them for length control.** Max tokens is the right parameter.\n- **Using them where structured outputs apply.** Weaker guarantee, more parsing.\n- **Whitespace mismatches.** Exact matching means trailing spaces matter.\n- **Not instructing the model to emit the marker.** Relying on it appearing by chance.\n\n## FAQ\n\n**How many stop sequences can I use?**\nProviders typically allow several, with a limit. More sequences means more chances one appears in valid output, so use the fewest that work.\n\n**What if the model never emits my sequence?**\nGeneration continues to the token limit. That is why the stop reason check matters, and why instructing the model to produce the marker is part of the design rather than optional.\n\n**Do they save cost?**\nMarginally, by stopping generation earlier than it would otherwise. Not a meaningful cost lever compared with prompt length or caching.\n\n**Are they available with structured outputs?**\nUsually, and they are typically unnecessary there — schema enforcement handles termination.\n\n## Where to go next\n\nFor the better approach to structured generation, read [structured outputs](/blog/structured-outputs-llm-2026). For the parameters governing token choice, [sampling parameters](/blog/sampling-parameters-explained-2026), and for validating what comes back, [LLM output validation](/blog/llm-output-validation-2026).","hue":216,"metaTitle":"Stop Sequences: Controlling Where Generation Ends","metaDescription":"How stop sequences terminate LLM generation, why you must always check the stop reason, and how to choose sequences that do not truncate valid output.","keywords":["stop sequences","generation termination","stop reason llm","max tokens truncation","output parsing","llm api parameters","structured generation","llm engineering 2026"],"tldr":["**The stop sequence itself is usually excluded from the output** — check, because assuming wrongly breaks parsing.","**Check the stop reason, always** — hitting the token limit and finishing naturally look identical if you only read the text.","**Choose sequences that cannot appear in legitimate output** — a common word truncates valid responses.","**Structured outputs remove most of the need** — schema enforcement handles termination better.","**Skip:** stop sequences as a length control. Set max tokens for that; stop sequences are for structure."],"readTime":"4 min read","wordCount":885},"related":[{"slug":"constrained-decoding-explained-2026","title":"Constrained Decoding: Making Model Output Always Parse","excerpt":"Structured output modes guarantee valid JSON by restricting what tokens the model may emit. Here is how that works, what it costs, and where it quietly hurts quality.","category":"ai","date":"August 11, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":213},{"slug":"prompt-engineering-best-practices-2026","title":"Prompt engineering best practices for 2026","excerpt":"A working checklist for writing prompts that hold up in production, from structure and examples to evaluation, without the outdated tricks that stopped mattering.","category":"ai","date":"July 3, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":280},{"slug":"json-mode-vs-tool-calling-2026","title":"JSON Mode vs Tool Calling: Which One You Actually Want","excerpt":"Both produce structured output and they solve different problems. Here is when to use each, why mixing them causes trouble, and how reliability differs.","category":"ai","date":"June 26, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":210}],"sameCategory":[{"slug":"agent-cost-attribution-2026","title":"Agent Cost Attribution: Finding Where the Tokens Went","excerpt":"A single monthly API bill tells you that agents are expensive. It does not tell you which feature, which customer, or which retry loop is responsible. That gap is fixable.","category":"ai","date":"August 19, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":220},{"slug":"human-in-the-loop-agents-2026","title":"Human-in-the-Loop Agents: Where to Put the Approval Gate","excerpt":"Approve everything and the agent is a slow autocomplete. Approve nothing and you have handed irreversible actions to a system that can be talked into things.","category":"ai","date":"August 19, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":197},{"slug":"reasoning-effort-controls-2026","title":"Reasoning Effort Controls: Tuning How Hard a Model Thinks","excerpt":"Fixed thinking budgets gave way to effort levels, and the default is rarely the right setting. Here is how the dial behaves and where each level actually belongs.","category":"ai","date":"August 19, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":210},{"slug":"retrieval-metrics-explained-2026","title":"Retrieval Metrics: Measuring the Half of RAG That Fails","excerpt":"When a RAG system answers badly, the retrieval usually failed before the model ever spoke. Measuring generation quality alone hides that entirely.","category":"ai","date":"August 19, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":215}],"newer":{"slug":"self-driving-cars-2026-where-they-are","title":"Self-driving cars in 2026: which cities have real Level 4, which don't","excerpt":"Robotaxis are real in some cities, fantasy in others. Here's where self-driving cars actually work in 2026 and where they don't.","category":"tech","date":"January 27, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":240},"older":{"slug":"best-ai-interview-prep-tools-2026","title":"Best AI interview prep tools in 2026: mock interviews + behavioral coaches","excerpt":"AI interview coaches now run realistic mock interviews with voice and feedback. Here's the honest comparison of the four worth using in 2026.","category":"ai","date":"January 26, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":210}}