max_tokens and Truncated Responses: Why Answers Get Cut Off

JS
Jai Singh
20 Nov, 2026 3 min read 12 views

If an LLM answer ends mid-sentence, the model almost certainly hit your max_tokens limit. The response will show stop_reason equal to max_tokens. The fix is to raise the limit, ask for a shorter answer, or continue the generation in a second call.

What max_tokens really does

max_tokens is a hard cutoff on generated tokens for one response. The model does not plan its answer to fit inside it. It writes as it normally would, and the server stops it when the count reaches the limit. That is why you see half a code block or a list cut at item four.

It is also not a target. Setting it to 4,000 does not make the model write 4,000 tokens. It only allows up to that many. Unused allowance costs nothing, because you pay for tokens actually produced.

Reproducing and detecting it

import anthropic

client = anthropic.Anthropic()

reply = client.messages.create(
    model="claude-haiku-5-5",
    max_tokens=30,
    messages=[{"role": "user", "content": "Explain how TCP handshakes work in detail."}],
)

print(reply.content[0].text)
print(reply.stop_reason)          # "max_tokens"
print(reply.usage.output_tokens)  # about 30

The printed text ends abruptly, and the stop reason confirms why. In production, check this every time.

if reply.stop_reason == "max_tokens":
    # Log it, then retry with a higher limit, or ask for a shorter answer.
    print("WARNING: truncated output")

Why truncation happens in real applications

Developers usually meet this problem in three situations. First, a prompt that works in a playground with a large default limit is moved into code with a small max_tokens copied from a quick example. Second, a user asks for something long, such as a full report or a big code file, and the limit was sized for chat replies. Third, thinking is turned on and the reasoning consumes most of the budget, leaving too little for the final answer. All three have the same symptom, so check the stop reason before you suspect the prompt.

Choosing a value

  • Classification or yes/no: 10 to 50 tokens is plenty.
  • Short answers and chat replies: 500 to 1,500.
  • Documents, code files, long summaries: several thousand, or more.
  • With thinking enabled: reasoning tokens usually count toward the output limit too, so leave generous headroom or the visible answer may be cut.

Models have a maximum output size that varies by model. If you ask for more than the model supports, the API returns an error. For very large limits, SDKs may require streaming so a single HTTP request does not time out.

Better than a huge limit: ask for the right length

A large cap protects you from truncation but does not control verbosity. State the desired length in the prompt, such as "Answer in three bullet points" or "Under 150 words". Combine a clear instruction with a moderate cap that acts as a safety net against runaway output and unexpected cost.

Recovering from truncation

For structured output such as JSON, truncation produces invalid data. Never try to repair half a JSON object. Retry with a larger limit. For prose, you can continue by sending the partial answer back as context with an instruction to carry on, though the newest Claude models do not accept a pre-filled final assistant turn, so put the partial text in a user message and ask the model to continue from the last sentence.

partial = reply.content[0].text
follow_up = client.messages.create(
    model="claude-haiku-5-5",
    max_tokens=800,
    messages=[
        {"role": "user", "content": "Explain how TCP handshakes work in detail."},
        {"role": "assistant", "content": partial},
        {"role": "user", "content": "Your answer was cut off. Continue exactly where you stopped."},
    ],
)
print(follow_up.content[0].text)

Checklist

  • Always set max_tokens, because it is required.
  • Always read stop_reason before using the text.
  • Match the limit to the task, and allow for thinking tokens.
  • Never parse JSON from a truncated reply.
Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.