---
title: "Stop Sequence"
slug: "stop-sequence"
category: "glossary"
tags: ["glossary", "ai-agents", "stop-sequence", "completion", "prompting", "llm"]
status: "stable"
last_updated: 2026-08-14
summary: "A stop sequence is a token passed to an LLM API that halts generation when the model produces it, bounding output length or delimiting sections."
related:
  [
    "[[glossary/completion]]",
    "[[glossary/token]]",
    "[[glossary/system-prompt]]",
    "[[glossary/structured-output]]",
    "[[prompt-engineering/prompt-design]]",
  ]
---

## Overview

This page is the atomic definition. Broader prompting and output-control patterns live at [[prompt-engineering/prompt-design]].

## Definition

A stop sequence is a string (one or more tokens) provided in the API request. When the model generates that exact sequence, generation halts and the stop sequence is not included in the response. Multiple stop sequences can be specified; the first match stops generation.

Stop sequences are useful for:
- Truncating long responses: `stop=["\n\n"]` stops after the first paragraph.
- Delimiting structured sections: parse a response that ends with `</answer>` or `---`.
- Preventing runaway generation in chat loops: stop on `User:` or `Human:` to prevent the model from simulating the next turn.
- Extracting the first item from a list: `stop=["\n"]` returns only the first line.

Stop sequences differ from `max_tokens`: `max_tokens` sets a hard ceiling on output length; stop sequences provide semantic boundaries. Both can be used together.

In the Anthropic API, the response object includes `stop_reason`: `"stop_sequence"` when a stop sequence triggered termination, and `stop_sequence` indicates which string matched.

## When it applies

Use stop sequences when you want output to end at a predictable structural boundary rather than relying on `max_tokens`. For [[glossary/structured-output|structured output]] parsing, adding a stop sequence after the expected closing delimiter prevents extraneous text from being appended.

Avoid using stop sequences as the primary length control; prefer `max_tokens` as a safety bound alongside stop sequences.

## Example

```python
import anthropic

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=512,
    stop_sequences=["</answer>"],
    messages=[{"role": "user", "content": "What is 2+2? Wrap in <answer>...</answer> tags."}]
)
print(response.stop_reason)  # "stop_sequence"
```

## Related concepts

- [[glossary/completion]] - stop sequences bound the completion returned by the model.
- [[glossary/token]] - stop sequences are matched at the token boundary, not always character-by-character.
- [[glossary/system-prompt]] - system prompts can instruct the model to use delimiters that match stop sequences.
- [[glossary/structured-output]] - stop sequences help delimit structured output sections.
- [[prompt-engineering/prompt-design]] - the prompting deep-dive on controlling output format.

## Citing this term

> See [[glossary/stop-sequence|Stop Sequence]] (llmbestpractices.com/glossary/stop-sequence).

## Related

- [[glossary/completion]]
- [[glossary/token]]
- [[glossary/system-prompt]]
- [[glossary/structured-output]]
- [[prompt-engineering/prompt-design]]
