How to Set Max Tokens to Cap Answer Length and Cost
Set max_tokens to the answer length you need, check the answer is not cut off, and keep the cost predictable.
THE FIELD GUIDE
Understand model formats, context windows, quantization, embeddings, and sampling before choosing settings for a real task.
Set max_tokens to the answer length you need, check the answer is not cut off, and keep the cost predictable.
Set frequency and presence penalties when output loops, then check the loop breaks without pushing the text off-topic.
Run the same task at temperature 0 and with sampling, compare the answers, and record which one the task needs.
Set top-k to keep only the k most likely tokens in play, then check the output stays coherent.
Set top-p to 0.9 so the model samples only from the smallest token set that reaches 90 percent probability.
Raise temperature into the 0.7 to 1.0 range for varied creative drafts, then lower it if the text turns incoherent.
Set temperature to 0 for extraction and classification so the output stays predictable and repeatable.
Place a key fact in the middle of a long prompt, ask a question that needs it, and check the answer uses…
Chunk the document, retrieve only the relevant parts, and compress them before they reach the prompt.
Add up system prompt, history, retrieved documents and reserved output, then trim before the call fails.