Repository navigation
Retry at the ceiling the endpoint states when it refuses ours - #892
basil-k-aji-dev wants to merge 1 commit into
Conversation
WaylandYang
left a comment
There was a problem hiding this comment.
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.
51c1408 to
64fdc12
Compare
WaylandYang
left a comment
There was a problem hiding this comment.
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.
ceilingis a local ofchat_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_clientbuilds one client per settings load,extraction.rscalls per chunk). Hold it on the client, for example anAtomicU32initialised toMAX_COMPLETION_TOKENS, so it is one refusal per client.- 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>= 1024removes 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.
64fdc12 to
186cbbf
Compare
…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]>
Closes #891
What breaks
MAX_COMPLETION_TOKENSis 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 backand 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_tokenscounts 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.
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_ceilingtakes 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_creditdeliberately 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:
insufficient_quotainerror.codeNoneand the caller gets exactly the error it gets today, so the worst case is the current behaviour rather than a wrong requestIf 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_statesis red ondevwith the real refusal body, and the failure is the issue verbatim: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_retriedpins that an unrelated 400 still reaches the caller in one attempt.two_http_responsesis new because the retry is only observable as a second request, and every existing helper answers once.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.