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:orHuman: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 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
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-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
- completion - stop sequences bound the completion returned by the model.
- token - stop sequences are matched at the token boundary, not always character-by-character.
- system-prompt - system prompts can instruct the model to use delimiters that match stop sequences.
- structured-output - stop sequences help delimit structured output sections.
- prompt-design - the prompting deep-dive on controlling output format.
Citing this term
See Stop Sequence (llmbestpractices.com/glossary/stop-sequence).