max_tokens is not a contract: five runs against a 600 token cap
max_tokens looks like a hard limit. In the OpenAI-compatible world it is a request, and whether it is enforced is a property of the implementation you happen to be talking to. I found this the boring way. I was measurin
max_tokens looks like a hard limit. In the OpenAI-compatible world it is a request, and whether it is enforced is a property of the implementation you happen to be talking to.
I found this the boring way. I was measuring output length on a fixed prompt to decide which model was cheaper per task, and one of the numbers was larger than the cap I had set.
The measurement
One endpoint, one request body, max_tokens 600, temperature 0.7, five runs per model, reading usage.completion_tokens from the response. Same prompt every time.
| Model | completion_tokens across five runs |
|---|---|
doubao-seed-2.1-turbo |
1836, 1171, 897, 762, 1166 |
glm-4.7-flash |
812, 594, 452, 573, 709 |
deepseek-v4-flash |
93, 111, 393, 221, 84 |
qwen3-coder-flash |
32, 32, 32, 32, 32 |
The first model returned above 600 on five out of five runs, once at more than three times the cap. The other three stayed inside it.
Why this matters more than it looks
If max_tokens is not enforced, then any cost estimate built on it is a lower bound, not a calculation. In my sample the overshoot reached 3x. That is the same order of magnitude as the differences people are comparing between models, which means a cost comparison can be dominated by cap behaviour rather than by price.
It also breaks the common design where max_tokens is treated as a safety net. If you set 600 because 600 is what you can afford per call, you have not bounded anything on that model.
What to do about it
Three things, in increasing order of effort.
Treat max_tokens as a hint and enforce the real limit yourself. Count usage.completion_tokens on every response and abort the loop when the running total crosses your line. That costs one integer per call and no latency.
Test the cap before you trust it. One call with a low cap and a prompt that wants to be long tells you which side of the line your model is on. Ten seconds of work, and it is the difference between a budget and a wish.
Log finish_reason alongside the token count. A stop at 1836 tokens against a cap of 600 is a different event from a length at 600. The first means the cap was ignored; the second means it worked. Without finish_reason the two look identical in your logs.
A caveat worth stating
I have not tested whether this is the provider's gateway or the upstream model, and I am not naming a cause. That is the behaviour of one endpoint on one day, and the honest reading is "verify on the model you actually use" rather than "provider X ignores caps".
The point of the numbers is not the specific model. It is that the parameter everyone budgets against can be advisory, and the only way to know which kind you have in front of you is to measure it once.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.