# Stop Sequences

> The Stop Sequences are strings that make a language model stop generating when they appear.

## What are stop sequences in an LLM API?

The parameter with one or more strings that end generation as soon as the model outputs them. Used to stop the answer at a specific marker and to control the format of the output.

- Topic: Generation Settings
- Type: concept

## Example

Wrong:

```js
const res = await llm.generate({
  prompt: 'Write a short product description.',
  stop: ['\n'],
});
```

Right:

```js
const res = await llm.generate({
  prompt: 'Write a product description, then write </answer>.',
  stop: ['</answer>'],
});
const text = res.text.trim();
```

A newline appears in normal prose and cuts the answer short; a unique closing tag appears only where the answer ends.

## Common mistakes

- Choosing a stop sequence that also appears inside normal output, cutting answers short.

## Key points

- Stop sequences are strings that cannot appear in a valid part of the answer.
- The code checks the stop reason to tell a stop sequence from a natural end or token limit.
- Code does not expect the stop sequence itself in the returned text.

## Interview questions

### Is the stop sequence included in the output?

Usually not. Most APIs stop before it and leave it out of the returned text, so do not search for it there.

### Stop sequences vs max tokens: what is the difference?

Max tokens ends output at a length limit; a stop sequence ends it when a chosen string appears. Check the stop reason to tell which one fired.

### Why use stop sequences?

To end output at a known boundary, such as a closing tag or a turn marker. This saves tokens and makes parsing simpler.

## Related cards

- [Max Tokens](https://wonderium.dev/ai/max-tokens.md)
- [Structured Output](https://wonderium.dev/ai/structured-output.md)
- [Prompt Template](https://wonderium.dev/ai/prompt-template.md)
- [LLM API](https://wonderium.dev/ai/llm-api.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/stop-sequences
- Last updated: 2026-11-25
