fix(openai): stop max_tokens streaming retry loop on reasoning models (#9716)#10448
Open
Dennisadira wants to merge 1 commit into
Open
fix(openai): stop max_tokens streaming retry loop on reasoning models (#9716)#10448Dennisadira wants to merge 1 commit into
Dennisadira wants to merge 1 commit into
Conversation
When a thinking model spends its entire max_tokens budget on the reasoning block, the C++ autoparser clears the raw Response and delivers reasoning-only ChatDeltas (no content, no tool calls). ComputeChoices' empty-response retry then fires and regenerates from scratch up to maxRetries times, each re-consuming the whole budget, instead of terminating with finish_reason "length" (issue mudler#9716). Add a reachedTokenBudget helper and suppress both the built-in and caller-driven retries when the completion count has reached the configured max_tokens ceiling. Report finish_reason "length" instead of "stop" in the streaming and non-streaming chat paths when the budget was exhausted. Adds a deterministic regression test that counts backend invocations (previously 6, now 1) plus boundary tests for the helper. Assisted-by: Claude:claude-opus-4-8 [Claude Code] Signed-off-by: Dennisadira <dennisadira@gmail.com>
mudler
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes the
max_tokensstreaming loop reported in #9716.When a thinking model spends its entire
max_tokensbudget on the reasoning block, the C++ autoparser clears the rawResponseand delivers reasoning-onlyChatDeltas(no content, no tool calls).ComputeChoices' empty-response retry then sees an "empty" generation and regenerates from scratch — up tomaxRetries(5) times — each attempt re-consuming the whole budget. So actual token consumption becomes a multiple ofmax_tokens, and the stream never cleanly terminates withfinish_reason: "length".Fix
reachedTokenBudget(completion, maxTokens)helper.max_tokensceiling — regenerating would just hit the same ceiling again.finish_reason: "length"(newFinishReasonLengthconstant) instead of"stop"in both the streaming final chunk and the non-streaming response when the budget was exhausted. Tool/function finish reasons are left untouched.Tests
Adds a deterministic regression test that stubs
ModelInferenceFuncand counts backend invocations for the budget-exhausted reasoning case:Plus boundary unit tests for
reachedTokenBudget(no limit / below / at / above). Fullcore/http/endpoints/openaiandcore/backendsuites pass;gofmt+go vetclean.Scope / follow-up
This is a focused Go-side fix: budget exhaustion is derived from the completion-token count vs
max_tokens, so it needs no backend/proto changes and resolves the user-facing loop today.A natural follow-up — matching the "plumb the backend's
lengthstop reason through" suggestion in the issue — would be to surface an authoritativestopped_limitfrom the llama.cpp gRPC server (and other backends) rather than inferring it. Happy to do that as a separate PR if you'd prefer the backend as the source of truth; I'd just want to confirm the proto shape (bool vs. a stop-reason enum) and default for backends that don't report it before writing it. The retry-suppression logic here is unchanged either way.Assisted-by: Claude:claude-opus-4-8 [Claude Code]