stop_sequences

API

Definition

An API parameter that provides a list of up to four strings that, when encountered in Claude's output, cause generation to halt immediately. The matched string is stripped from the response. When a stop sequence fires, `stop_reason` is set to `"stop_sequence"`. Useful for enforcing structured output boundaries or workflow step delimiters.

Example Usage

Use stop_sequences: ['</answer>'] to ensure Claude stops after producing the answer block in a structured format.

Why It Matters for the CCA-F Exam

The exam tests whether candidates know that the matched stop string is excluded from the response, that `stop_reason` becomes `"stop_sequence"` when a sequence fires, and how to distinguish this from `end_turn` or `max_tokens`. Questions frequently present a scenario ("how do you ensure the model stops at the closing XML tag?") and require selecting `stop_sequences` over alternatives like post-processing or `max_tokens`.

In Depth

The stop_sequences parameter accepts an array of strings that act as hard boundaries on Claude's output. The moment any listed string appears in the generated text, generation halts and that string is stripped from the final response. Claude never includes the matched delimiter in what you receive.

This mechanism is important when you need to enforce structural contracts on responses. Consider a pipeline where you wrap answers in XML tags like <answer> and </answer>. Passing stop_sequences: ["</answer>"] guarantees the model stops the moment the closing tag would be emitted, so you never need to post-process a truncated or over-generated response. The matched delimiter is consumed silently, leaving only the inner content.

Knowing Why Generation Stopped

When a stop sequence fires, stop_reason is set to "stop_sequence" and the stop_sequence field in the response carries the matched string — giving you a reliable way to distinguish a natural end-of-turn (end_turn) from a forced boundary. For a full description of all stop_reason values, see stop_reason.

Common Practitioner Patterns

  • Structured extraction — wrap the target span in custom XML tags and stop on the closing tag to extract clean content without post-processing.
  • Step delimiter — in agentic loops, insert a sentinel like "---END---" and stop on it to isolate each tool-planning step.
  • Code fencing — stop on ` to capture exactly one code block without trailing prose.
  • Multi-turn control — stop on "\nHuman:" or "\nUser:" in older prompt scaffolds to prevent Claude from roleplaying the next user turn.

Limits and Caching Interaction

You can pass up to four stop sequences. They are checked per token as generation proceeds, so there is no latency penalty. The first one matched (in generation order, not array order) triggers the stop.

Stop sequences do not affect prompt caching — they are a request-time parameter, not part of the cached prefix, so changing them between calls does not invalidate a cached system prompt.

One subtlety: if Claude would naturally have written the stop string mid-sentence, the sentence appears truncated. Design stop sequences to land at clean phrase boundaries unless you specifically want partial output. For richer output control, combine stop sequences with structured output or output_config schema enforcement, which guarantees shape at the JSON level rather than string level.

How It Compares

MechanismWhat it doesStop string included?Affects cost?
stop_sequencesHalts generation on matched stringNo — strippedTokens up to the stop only
max_tokensHalts after N output tokensN/ATokens up to cap only
end_turnNatural model completionN/A — no delimiterFull response cost
Structured output schemaConstrains shape at JSON levelN/ANo direct effect on token count

Frequently Asked Questions

Is the stop string itself included in the response text?

No. Claude halts generation the moment it would emit the stop string, and the stop string itself is never included. Only the content before it is returned. You can confirm which string matched by inspecting `stop_sequence` in the response.

How many stop sequences can I pass?

Up to four strings in the `stop_sequences` array. The first one matched (in generation order, not array order) triggers the stop.

What is the stop_reason value when a stop sequence fires?

The response carries `stop_reason: "stop_sequence"` and the `stop_sequence` field contains the exact string that was matched. See [stop_reason](/glossary/stop-reason) for the full list of possible values and when each occurs.