Skip to content

Retry at the ceiling the endpoint states when it refuses ours - #892

Closed
basil-k-aji-dev wants to merge 1 commit into
deeplethe:devfrom
basil-k-aji-dev:fix/max-tokens-respects-endpoint-ceiling
Closed

basil-k-aji-dev wants to merge 1 commit into
deeplethe:devfrom
basil-k-aji-dev:fix/max-tokens-respects-endpoint-ceiling

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown
Contributor

Closes #891

What breaks

MAX_COMPLETION_TOKENS is 65,536, and a model whose own completion limit sits below that refuses the request rather than clamping it. gpt-4o-mini caps at 16,384, so every chunk comes back

LLM request failed (400 Bad Request): max_tokens is too large: 65536.
This model supports at most 16384 completion tokens, whereas you provided 65536.

and extraction produces nothing.

This is fallout from the number I asked for in #776 being raised on review, and the reasoning for raising it was right: the largest completion measured on this path was 45,428 tokens with reasoning on, max_tokens counts reasoning on some endpoints, and 0044 measured what a reasoning cap costs. A ceiling below that becomes a thinking budget. What neither of us accounted for is that some endpoints treat the field as a hard input bound and reject anything above their own limit, so the number cannot be both high enough to never be a reasoning cap and low enough to always be accepted.

What this does

Retries once, and only downwards, at the ceiling the endpoint named in its refusal.

if status == reqwest::StatusCode::BAD_REQUEST && ceiling == MAX_COMPLETION_TOKENS {
    if let Some(lower) = stated_completion_ceiling(&err_detail(&parsed, &raw), ceiling) {
        ceiling = lower;
        continue;
    }
}

The endpoint that refuses our ceiling has told us its own, so using that is strictly better than the two alternatives: lowering the constant reintroduces the reasoning cap for everyone, and dropping the field hands the cut-off point back to the endpoint default that #760 exists to displace, which was measured at exactly 4,096.

stated_completion_ceiling takes every integer in the message and keeps the largest one below what we sent. The wording differs per vendor, and the numbers at or above what we sent are the echo of our own request, so the rest is the limit.

The part I want you to push back on

This is free-text matching, and says_out_of_credit deliberately refuses to do that, for reasons I agree with: "措辞会改、会本地化".

I think it is acceptable here and I want to be explicit about why rather than quietly differ from the file's own rule:

  • there is no structured carrier for the limit, unlike insufficient_quota in error.code
  • it only ever lowers a ceiling, never raises one
  • a reworded message parses to None and the caller gets exactly the error it gets today, so the worst case is the current behaviour rather than a wrong request

If you would still rather not read prose at all, the two alternatives are a per-provider override with 65,536 as the default, which is the other half of what #891 asks for, or a fixed descending ladder. Say which and I will rewrite it.

Tests

a_ceiling_the_endpoint_refuses_is_retried_at_the_one_it_states is red on dev with the real refusal body, and the failure is the issue verbatim:

called `Result::unwrap()` on an `Err` value: LLM request failed (400 Bad Request):
max_tokens is too large: 65536. This model supports at most 16384 completion tokens,
whereas you provided 65536.

Green here, and it asserts both requests: the first still asks for the ceiling that is ours, the second asks for 16,384 rather than a guess. a_refusal_that_names_no_ceiling_is_not_retried pins that an unrelated 400 still reaches the caller in one attempt.

two_http_responses is new because the retry is only observable as a second request, and every existing helper answers once.

cargo test -p utopia-llm      38 passed; 0 failed
cargo clippy -p utopia-llm --all-targets    clean
cargo fmt                     clean

One thing to fix on merge

The comments are in English. The file's are in Chinese and I cannot write it to a standard worth committing, so I would rather say so than submit prose I cannot check. Rewrite them in the file's register and I will not be precious about the wording.

@WaylandYang WaylandYang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mechanism is right and the write-up is careful, so most of this is about landing it.

Blocking: the commit has no Signed-off-by. The DCO check is red for that reason
alone. git commit --amend -s and force-push; nothing else needs to change for it.

Worth doing in this PR: remember the ceiling on the client. ceiling is a local
of chat_at_streaming, so a model below MAX_COMPLETION_TOKENS pays the 400 on
every call. Extraction builds one LlmClient per document and calls it once per
chunk, so gpt-4o-mini now makes two requests per chunk for the life of the process:
the refusal, then the real one. An AtomicU32 (or Cell behind the same borrow the
client already hands out) initialised to MAX_COMPLETION_TOKENS and lowered by the
retry would make it one 400 per client instead of one per chunk, and the "only
downwards" property you argue for in the doc comment holds unchanged. It also
answers the obvious next bug report ("extraction is twice as slow on OpenAI").

Non-blocking, worth a sentence in the doc comment: every integer in the message
is a candidate, so a model name with digits in it is too. gpt-4o-mini contributes a
4; o3-mini-2025-01-31 contributes 3, 2025, 1, 31. max picks the stated
limit whenever the message actually states one, and when it doesn't the fallback is
a low ceiling and one truncated reply rather than a failure, so the design survives
it; but the comment currently says "the largest of the rest is the ceiling" as if the
rest were all candidates for the limit, and the next reader will wonder why 2025
didn't win. A floor (>= 1024, say) would remove the small ones for free.

The two tests are the right shape: the retry is only observable as a second request,
and the negative case pins that an unrelated 400 still surfaces. Checked that
failure/err_detail are the existing helpers and that the loop cannot spin: a second
400 has ceiling != MAX_COMPLETION_TOKENS and returns.

@basil-k-aji-dev
basil-k-aji-dev force-pushed the fix/max-tokens-respects-endpoint-ceiling branch from 51c1408 to 64fdc12 Compare September 23, 2026 19:56

@WaylandYang WaylandYang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the rebase, but the head commit carries the same lib.rs as the one reviewed last time, and it still has no Signed-off-by, so the DCO check is red and this cannot land as is: git commit --amend -s and force-push.

The two substantive points from the last round also still stand.

  1. ceiling is a local of chat_at_streaming (lib.rs:501), so a client built for gpt-4o-mini pays the 400 on every chunk for the life of the process (llm_util::chat_client builds one client per settings load, extraction.rs calls per chunk). Hold it on the client, for example an AtomicU32 initialised to MAX_COMPLETION_TOKENS, so it is one refusal per client.
  2. The "any integer below what we sent" rule (lib.rs:1040) is not as safe as the doc comment says. "max_tokens is too large: 65,536. This model supports at most 16,384" splits on the thousands separators and parses to 536; "max_tokens is too large for model gpt-4o-mini-2024-07-18" parses to 2024. Both then return a truncated reply (finish_reason: length) where today the caller gets the 400, which is the one direction the comment says cannot happen. A floor of >= 1024 removes both cheaply; then reword the comment so it no longer says the largest remaining number "is" the ceiling. A context-window refusal (vLLM's "maximum context length is 32768 tokens") parses to a number still over budget and simply 400s again, which is harmless but worth a sentence.

The mechanism, the bounded loop and the two tests are fine, and the branch merges cleanly onto current dev with the once-per-reply usage logging (38/38 green on the merged tree), so once those three land I am happy to approve.

@WaylandYang

Copy link
Copy Markdown
Contributor

Landed as #914 with your mechanism and tests, carried with co-authorship, after the two review points were applied there (the ceiling kept on the client, and the number read only when the message calls it tokens). Thank you for finding and fixing #891. Closing this one in favour of #914.

pull Bot pushed a commit to nagyist/deeplethe-utopia that referenced this pull request Sep 25, 2026
…member it (deeplethe#914)

Since deeplethe#786 every streamed call sends max_tokens 65,536 so that a cut
reply is ours to detect. An endpoint whose completion limit is lower
refuses instead of clamping, and every chunk fails: gpt-4o-mini caps at
16,384 (deeplethe#891).

When a 400 states the endpoint's own ceiling, the call is retried once
at that number, and the number is kept on the client, shared with its
clones, so a process pays the refusal once rather than once per chunk.
The ceiling only ever goes down. Only a number the message itself calls
tokens counts, with or without thousands separators and no lower than
1,024: the digits of a model name, a status code or our own echoed
request are not limits, and a message that parses to nothing leaves the
caller with exactly the 400 it gets today. A context-window refusal
lowers to the window and may still fail on the retry, as today.

Landed from deeplethe#892 by basil-k-aji-dev with the two review points applied:
the ceiling on the client instead of a local, and the shape rule for the
number instead of "any integer below what we sent".

Closes deeplethe#891
Closes deeplethe#892

Signed-off-by: Wayland Yang <[email protected]>
Co-authored-by: basil-k-aji-dev <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graph extraction fails with OpenAI models in 0.1.0-rc6 (max_tokens too large)

2 participants