New Tool Radar

AI Concepts

How to Check Whether max_tokens Truncated the Model Output

Tell a truncated answer from a short answer, so you raise the limit instead of blaming the model for stopping early.

Schematic illustration with three step boxes: read the finish reason, count the output tokens, and raise the limit and retry.

max_tokens caps how many tokens the model may generate, and when the cap is hit the output stops mid-sentence. The finish reason tells you whether that happened: a finish reason of length means the cap was hit, while stop means the model finished on its own. Checking the reason first saves you from rewriting a prompt for a limit problem.

Before you start

An API client that returns the finish reason, and a request that currently returns a suspiciously short answer.

Read the finish reason first

Look at the finish reason on the reply: length means the output was cut by max_tokens, stop means the model chose to end, and tool_calls means it called a function instead of answering.

Log the finish reason with every call. A length finish on a long answer is expected; a length finish on a short answer means the limit is set too low.

Count the output tokens

Count the tokens in the returned text and compare with the max_tokens you set. If the count equals the limit, the cap was hit; if it is well below the limit, the model stopped early for another reason.

Check the prompt size too: a prompt that fills the window leaves no room for output, and the model stops because the window is full, not because max_tokens was hit.

Raise the limit and retry once

Raise max_tokens to a value that fits the window with the prompt included, and retry the same prompt once. If the retry finishes with a stop reason and a complete answer, the limit was the cause.

If the retry still stops at length, the window itself is too small for the answer you want: shorten the prompt or split the answer into parts.

Practice check: fictional Maple Corner summary request, max_tokens 200

The table below is fictional practice material, worked out by hand from the stated limits. It is the expected result for the practice request, not a run of any API.

max_tokens Output tokens Finish reason Verdict
200 200 length Truncated – limit hit
800 640 stop Pass – complete answer
800, long prompt 790 length Fail – window full

At 200 tokens the output stops at exactly 200 with a length reason, so the limit truncated it. At 800 it finishes at 640 with a stop reason, but a long prompt fills the window and truncates again.

Expected result. UNTESTED: the counts in the practice table are worked out by hand, not measured by running an API. Read the finish reason on your own short answer, count the output tokens against the limit, and retry once with a higher limit.

Limits

  • A finish reason of length always means a cap was hit, but the cap may be max_tokens or the context window itself.
  • Raising max_tokens without checking the window can move the truncation from the limit to the window.

Sources

See all