Sub topic 7
Limit and shape the output
This chapter assumes you can call an LLM API, know what max_tokens and temperature do, and have read the earlier chapters on token pricing and latency. Output tokens usually cost several times more than input tokens, and the model produces them one at a time. Trimming the output therefore cuts both cost and latency more than trimming the same number of input tokens.
Why output length matters most
Prefill processes the whole prompt in parallel. Decoding generates one token after another, so generation time grows roughly in proportion to output length. A 400-token answer takes about four times as long to decode as a 100-token answer. It also costs about four times as much at the output price.
Set a hard cap with max_tokens
Every provider lets you set a maximum number of output tokens. The name varies, for example max_tokens or max_output_tokens. The cap is a ceiling, not an instruction to the model. The model does not know about it, so it can be cut off mid-sentence.
Pick the cap from your baseline. Look at the 95th percentile of output lengths for the task, then add a margin. A cap far above what you need protects you only from runaway outputs. A cap below what you need causes truncated answers.
Ask for brevity in the prompt
Use the prompt to shape the answer, and the cap as a safety net. Specific instructions work better than "be brief".
- State a limit in units the model follows well: "Answer in at most two sentences" or "List up to five bullets".
- Say what to leave out: "No introduction, no summary, no apology."
- Ask for the answer first, then reasons only if needed.
- Give one short example of the format you want.
Use structured, compact formats
For machine-read output, short formats save many tokens. Ask for a label instead of a paragraph. Ask for a JSON object with short, fixed keys instead of free text.
A classifier that returns {"label": "refund"} uses a handful of tokens. One that explains its reasoning in prose can use a hundred or more. If you need the explanation only for debugging, request it in a sampled subset of calls.
Many APIs also offer a stop sequence. Generation ends when the model emits it, so you can cut off trailing text you don't need.
def build_request(question: str) -> dict:
return {
"model": "your-model-name",
"messages": [
{
"role": "system",
"content": (
"Reply with JSON only, in the form "
'{"label": "<one of: refund, shipping, other>"}. '
"No other text."
),
},
{"role": "user", "content": question},
],
"max_tokens": 20,
"temperature": 0,
}
print(build_request("Where is my parcel?")["max_tokens"])The cap of 20 fits the format, since the longest valid reply is only a few tokens. Parameter names differ between providers, so check your provider's reference.
Watch for hidden output tokens
Some models produce reasoning tokens before the visible answer. Providers usually bill these as output tokens, and they count toward latency. If your usage numbers show more output tokens than the visible text explains, look for this. Check whether your provider lets you limit reasoning effort for simple tasks.
Check quality after shortening
Shorter outputs can lose detail you need. After each change, run your evaluation set and compare quality against the baseline. Keep the change only if quality stays above your bar.
Exercise
A summarization endpoint has a p95 output length of 180 tokens. A teammate sets max_tokens to 60 to cut cost. What problem should you expect, and how would you detect it?
Show answer
Many summaries will be truncated mid-sentence, because the cap does not make the model write less. You can detect it by checking the finish reason for length or max_tokens in responses, and by running the evaluation set. A better approach is to instruct the model to write shorter summaries, and keep the cap near the p95 length plus a margin.