How max_tokens clamping caps cost
Clamping max_tokens is the mechanism that makes a spend cap hold on a stream. Redline works out how many output tokens the remaining budget can afford and sets the outbound limit to that, so the reply is bounded before it starts.
Why clamp instead of abort
You could kill a stream when it crosses the cap, but that wastes the tokens already spent and leaves the caller with a truncated, unbilled mess. Clamping sets the ceiling up front: the model is told it may write at most N tokens, where N is what the budget covers after the prompt is paid for.
The arithmetic
Redline prices the prompt, subtracts that from what the key can still spend, converts the rest to output tokens at the model's output price, and passes that as max_tokens. A request for a short answer is left alone; only a request that could overrun the cap is clamped down.