Skip to content

Stop at max_output_tokens, not one past it - #906

Closed
dghosef wants to merge 1 commit into
mainfrom
fix/max-output-tokens-off-by-one
Closed

dghosef wants to merge 1 commit into
mainfrom
fix/max-output-tokens-off-by-one

Conversation

@dghosef

@dghosef dghosef commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

A request with max_output_tokens=N gets N+1 tokens from Whisper, Orpheus, Qwen3.5, Higgs Audio and Qwen3-Omni. For example, max_output_tokens=3 returns 4 tokens.

Prefill samples the first token, then the decode loop ran N more. check_stop reads the decode loop's index before the current step is counted. The index starts at 0, so index + 1 >= N stops on the Nth decode step. get_loop_caps also capped decode at N.

This counts the prefill token too (index + 2) and caps decode at max(1, N - 1), which is what Command A+, BAGEL and Cosmos3 already do. max_output_tokens=1 still returns two tokens, because a loop runs at least once.

Changed: Whisper, Orpheus, Qwen3.5 (both stop paths), Higgs Audio and Qwen3-Omni's thinker. Qwen3-Omni's talker keeps its own codec budget.

How it was tested

  • test/modular/test_model_kv_layouts.py: each model's loop-cap test now also checks that prefill's token plus decode's add up to max_output_tokens. These fail on main and pass with this change.
  • GPU: (to fill in)

🤖 Generated with Claude Code

A request with max_output_tokens=N got N+1 tokens from Whisper, Orpheus,
Qwen3.5, Higgs Audio and Qwen3-Omni's thinker. Prefill samples the first
token, then the decode loop ran N more: check_stop reads the zero-based
loop index from before this iteration is counted, so `index + 1 >= N`
stops on the Nth decode iteration, and get_loop_caps capped decode at N.

Count the prefill token and this iteration's (`index + 2`) and cap decode
at max(1, N - 1), as Command A+, BAGEL and Cosmos3 already do. N=1 still
returns two tokens: a loop runs at least once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gaurav-Shah05

Copy link
Copy Markdown
Collaborator

Is this different than #604?

@dghosef

dghosef commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

Lol you're right

@dghosef dghosef closed this Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants