# What Do Stop Sequences and Max Tokens Do?

> What stop sequences and max tokens do, how each one cuts off a model's reply, and how to handle a cut-off answer, for your AI interview.

**In short:** Max tokens is a limit on how long a model's reply can be. A stop sequence is a piece of text that makes the model stop as soon as it writes it. Both cut the output short, so your code must check why the reply ended.

AI and LLM interview question Q029, LLM fundamentals. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-do-stop-sequences-and-max-tokens-do

## Say it in 30 seconds

Max tokens is the most a model may write in one reply. Reaching it cuts the reply off, often mid-sentence. A stop sequence is text I name in advance, such as a closing tag, and the model stops as soon as it writes it. Both are limits on output, so they do not make the answer more accurate. My code reads the stop reason on every reply, and it saves only the ones that finished on their own.

## Say it in 2 minutes

A language model writes one token at a time, and left alone it keeps going until it decides it is done. Two settings change where it stops. Max tokens is a ceiling on how much it may write. A stop sequence is a piece of text I choose in advance, and the model stops the moment it writes that text.

Take a claims team at a mid-size insurer that uses a model to summarise each rear-end crash claim. The team caps each summary at a few hundred tokens, which fits a normal claim. One day a claim with three vehicles and a medical note arrives, and the summary stops halfway through the next-steps line. Nothing in the text says it was cut, and an adjuster, the person who checks each claim, could miss the missing steps.

So the stop reason matters as much as the text. The API, the way your code talks to the model, returns a field that says why the reply ended. A finished reply completes naturally, a cut-off reply reaches max tokens, and a marker ends it on a stop sequence. My code saves only the finished ones, retries the cut-off ones with a bit more room, and sends repeat cut-offs to a person.

Both settings only limit output. They do not make the model more accurate. A marker only matches the exact characters you give it. Google's docs say the strings are case-sensitive, so a closing tag written in another case slips past. In an interview, I would say that, and show the stop-reason check in code.

## What do stop sequences and max tokens do, in plain words?

A language model writes text one token at a time. A token is a small piece of text, often part of a word. Left alone, the model keeps going until it decides the answer is finished. Two settings let you change where it stops.

Max tokens is a ceiling on how much the model may write in one reply. The model does not aim for that length. It may stop well short of it, and if it reaches the ceiling, it stops there.

A stop sequence is text you choose in advance, such as a closing tag. When the model writes that text, generation ends at once. You use it when the output has a clear end marker that your code can rely on.

Think of a claims clerk filling in a paper form. The box only holds so many words, so the clerk must stop when it is full. A second clerk is told to stop as soon as they reach a line that says END OF SUMMARY. The first rule is a box size, and the second is a marker. In AI terms, the same thing happens to a claims summary.

- **Token:** A small chunk of text the model reads and writes, often part of a word.
- **Max tokens:** The most the model may write in one reply. A ceiling it may never pass.
- **Stop sequence:** Text you name in advance. The model stops once it writes it.
- **Stop reason:** The field the API returns to say why the reply ended.
- **Cut off:** The reply ended because it hit the cap, often mid-sentence.

## What does max tokens do, and what goes wrong?

When the model reaches the cap, it stops at once, which can leave a sentence unfinished.

> "The response reached your max_tokens limit." (Anthropic)
>
> "Maximum number of tokens that can be generated in the response." (Google)

Google's docs say a token is about four characters, so a cap of 300 tokens is roughly 1,200 characters. That is enough for a normal claim summary, and too little for a long one.

Here is the real case. A claims team at a mid-size insurer caps each summary at 300 tokens. One day a claim with three vehicles and a medical note comes in, and the summary stops halfway through the next-steps line. The text gives no warning, and an adjuster skimming it could miss the missing steps.

*Diagram, Where the reply stops: Two settings go in with each request, and the model stops in one of three ways. A reply that hits the cap is cut off, and a reply that hits a marker ends just before it. Your code reads the stop reason to tell them apart.*
1. Your cap goes into the request with the prompt.
2. Your list of markers is sent the same way.
3. If the model finishes first, the reply ends normally.
4. If the cap is reached first, the reply stops, maybe mid-sentence.
5. If a marker appears, the text ends just before it.

## How does a stop sequence work in practice?

A stop sequence is a list of exact strings. The model stops when the text it writes matches one of them. Google's docs describe it like this:

> "Specifies a list of strings that tells the model to stop generating text if one of the strings is encountered in the response." (Google)
>
> "Claude emitted one of your stop_sequences." (Anthropic)

OpenAI's docs name the same two endings, among a few more reasons. The reason is stop when the model reaches a natural end or a stop sequence, and length when the cap is reached.

Pick markers the model will not write by accident. A closing tag such as </summary> works well. A common word such as the does not, because it cuts the reply at its first match. A marker only matches the exact characters you give it, so a marker written with a different capital letter slips past.

Here is the logic in plain Python. A script stands in for the model and writes one piece at a time. The two limits decide where it stops.

```python
# A simplified generator. Real models also write one token at a time, then check two limits.
SCRIPT = ["Claim 4471:", " rear-end crash,", " two cars,", " no injuries reported.", " </summary>", " Claim 4472 is pending."]

def generate(max_tokens, stop_sequences):
    text, used = "", 0
    for piece in SCRIPT:
        if used == max_tokens:
            return text, "max_tokens"
        text += piece
        used += 1
        for stop in stop_sequences:
            if stop in text:
                return text[:text.index(stop)].strip(), "stop_sequence"
    return text, "end_turn"

for limit, stops in [(10, ["</summary>"]), (3, ["</summary>"]), (10, ["Rejected"])]:
    text, reason = generate(limit, stops)
    print(f"{reason}: {text}")
```

Output:

```text
stop_sequence: Claim 4471: rear-end crash, two cars, no injuries reported.
max_tokens: Claim 4471: rear-end crash, two cars,
end_turn: Claim 4471: rear-end crash, two cars, no injuries reported. </summary> Claim 4472 is pending.
```

> **Tip:** Always read the stop reason next to the text. The text alone cannot tell you whether the reply is finished.

### How would you stop a cut-off summary reaching an adjuster?

Treat the stop reason as data. Your code reads it before anything is saved, and each outcome has its own path.

*Diagram, What your code does with a stop reason: A successful Claude reply always comes back with its stop reason. Your code reads it and picks the next step, so a cut-off summary never reaches the adjuster as if it were finished.*
1. The reply comes back with its reason attached.
2. Finished on its own: the summary is safe to save.
3. Cut off the first time: run it again with more room.
4. Cut off again: a person finishes the summary.

1. **Read the stop reason first:** The API tells you why the reply ended. Save only the replies that finished on their own.
2. **Retry with more room:** If the reply was cut off, raise the cap for that claim and run it again once.
3. **Shorten the ask:** Ask for fewer fields, such as the three facts an adjuster needs first.
4. **Send the rest to a person:** If the second try is still cut off, a person finishes the summary. Nothing reaches the adjuster half-written.
5. **Log every cut-off:** Count the cut-offs each week. A rising count means the cap or the prompt is wrong.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: max tokens caps the reply, a stop sequence ends it at a marker.
- [ ] Both are limits on output. Neither makes the answer more accurate.
- [ ] A cut-off reply can end mid-sentence, and the text itself gives no warning.
- [ ] Your code reads the stop reason and handles each case.
- [ ] Markers match exact text, so pick ones the model will not write by accident.
- [ ] Leave room in the cap, and log how often replies get cut off.

**Common traps**

- Trap: "Max tokens sets how long the answer should be." Better: It is a ceiling. The model may stop well short, and a cut-off reply can look normal, so check the stop reason.
- Trap: "A stop sequence makes the model stop at the right place." Better: It stops only exact matches. A marker written with a different capital letter gets straight past it.
- Trap: "If the reply reads well, it finished." Better: A cut-off reply can read well. Check the stop reason in code, not how the text looks.

## Can you spot the cut-off?

**Spot the cut-off:** Your code gets back the stop reason max_tokens for a claim summary. What happened to the reply?

A. The model decided it had said enough.
B. The reply hit the length cap and was cut off.
C. The reply hit a stop sequence and ended at a marker.
D. The reply was too long for the context window, the most text the model can hold at once.

**Answer:** B. max_tokens means the cap was reached, so the reply was cut where it stopped. A finished reply reports that it completed naturally, and a marker reports a stop sequence.

**Quick check:** Your summariser must end at the closing tag </summary> and never run past it. Which setting does that job?

A. Max tokens
B. A stop sequence
C. Temperature
D. A longer system prompt

**Answer:** B. A stop sequence ends the reply when it sees the marker. Max tokens only counts length, and temperature only changes how random the word choice is.

## Can you answer these from memory?

**Flash cards**

- **What does max tokens do?** Caps how long one reply can be. Reaching the cap cuts the reply off.
- **What is a stop sequence?** Exact text you name. The model stops as soon as it writes it.
- **Is max tokens a target?** No. It is a ceiling. The model may stop well short of it.
- **Which stop reason means the cap was hit?** max_tokens in Anthropic's API, length in OpenAI's. The reply was cut, maybe mid-sentence.
- **Are stop sequences case-sensitive?** Google's docs say yes, so Summary and summary are different markers.
- **What should code do with a cut-off reply?** Never save it as finished. Retry, shorten the ask, or send it to a person.

## Likely follow-up questions

- Q008 What is a context window? https://designervenkat.online/interview/ai/what-is-a-context-window
- Q010 What does temperature control? https://designervenkat.online/interview/ai/what-does-temperature-control
- Q030 Why are input and output tokens priced differently? https://designervenkat.online/interview/ai/why-are-input-and-output-tokens-priced-differently
- Q032 What are special tokens (start/end)? (full answer coming)
- Q338 How do you cap output length? (full answer coming)
- Q194 How do you design stop conditions? https://designervenkat.online/interview/ai/how-do-you-design-stop-conditions
