New Tool Radar

AI Concepts

How to Cap Model Output Length with Max Tokens

Cap model output length with max tokens so a reply stops at the length you need instead of running on.

Schematic illustration of cap model output length with max tokens: step one estimate the length needed, step two set the cap, step three check for truncation.

Max tokens caps the length of the model’s reply. Without a cap, a model can produce a long answer when a short one would do, costing time and tokens; with a cap set too low, the answer is cut off mid-sentence. The cap belongs to the output only: it does not limit the prompt. Set it from the length your task needs, then check that the answer is complete.

Prerequisites

  • The output length your task needs, in tokens or words.
  • Access to the max tokens parameter in your API or runtime.
  • A way to see whether the reply was cut short, such as the finish reason.

Steps

Step 1: Estimate the length needed

Estimate the reply length: a short answer needs about 150 tokens, a one-paragraph summary about 300, a one-page answer about 800. Add a margin of about half again, so a 300-token answer gets a 500-token cap.

Step 2: Set the cap

Set the max tokens parameter to the cap for the request. Use the parameter name your API expects: max_tokens, max_output_tokens or maxOutputTokens, depending on the provider.

Step 3: Check for truncation

After the call, check the finish reason. If it says the reply stopped because of the length limit, the answer is incomplete: raise the cap or shorten the request, and run it again.

Worked example: Northgate Clinic appointment reminder

The fictional Northgate Clinic asks a model to write a 2-sentence appointment reminder, about 60 tokens, and sets the cap to 150 tokens.

Setting Cap Reply Complete
No cap None Reminder plus extra advice Yes, but too long
Cap 150 tokens 150 2-sentence reminder Yes
Cap 20 tokens 20 Reminder cut mid-sentence No

The 150-token cap gives a complete 2-sentence reminder, while the 20-token cap cuts it short, so 150 is the working cap for this task.

Expected result. The reply is complete within the cap, and the finish reason confirms it stopped naturally, not at the limit. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • A cap is a maximum, not a target: the model may still write a shorter reply.
  • Parameter names differ by provider; check your API’s reference for the exact name.
  • A truncated reply is not a partial answer; treat it as a failed reply.

Sources

See all