esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

11 min readBeginnerCommon · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

Where the reply stops

Your settings

Max tokens: 300

a ceiling on how much it writes

Stop sequence: </summary>

a marker that ends the text

Model

writes one token at a time

Why it stopped

Finished on its own

the reply is complete

Hit max tokens

cut off, maybe mid-sentence

Hit the marker

text ends before the marker

  1. 1Your cap goes into the request with the prompt.
  2. 2Your list of markers is sent the same way.
  3. 3If the model finishes first, the reply ends normally.
  4. 4If the cap is reached first, the reply stops, maybe mid-sentence.
  5. 5If a marker appears, the text ends just before it.
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.

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

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.

What your code does with a stop reason

Reply and stop reason

the text plus why it ended

Read the stop reason

a code check, not a guess

Next step

Save the summary

finished on its own, safe to save

Retry with a bigger cap

cut off once: try again

A person finishes it

cut off twice: a person checks

  1. 1The reply comes back with its reason attached.
  2. 2Finished on its own: the summary is safe to save.
  3. 3Cut off the first time: run it again with more room.
  4. 4Cut off again: a person finishes the summary.
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. 1

    Read the stop reason first

    The API tells you why the reply ended. Save only the replies that finished on their own.

  2. 2

    Retry with more room

    If the reply was cut off, raise the cap for that claim and run it again once.

  3. 3

    Shorten the ask

    Ask for fewer fields, such as the three facts an adjuster needs first.

  4. 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. 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."

    BetterIt 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."

    BetterIt stops only exact matches. A marker written with a different capital letter gets straight past it.

  • Trap"If the reply reads well, it finished."

    BetterA 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?

Quick check

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

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q008What is a context window?11 min
  2. Q010What does temperature control?10 min
  3. Q030Why are input and output tokens priced differently?11 min
  4. Q032What are special tokens (start/end)?Coming soon
  5. Q338How do you cap output length?Coming soon
  6. Q194How do you design stop conditions?12 min

Last updated on 26 November 2026

Building a tool that writes summaries people act on?

I design and build AI products and agents, including the limits and checks around the model. Book a short call and tell me what your output has to do.