fix(ai): Mistral choices guard, Codex non-retryable errors, faux cache-write token count - #1647
Closed
yuzhiyang1 wants to merge 1 commit into
Closed
Conversation
…etryable errors, fix faux cache-write token double count
|
Hi @yuzhiyang1, thanks for your interest in contributing! This project requires that pull request authors are vouched, and you are not in the list of vouched users. This PR will be closed automatically. See https://github.com/PrimeIntellect-ai/prime-agent/blob/main/CONTRIBUTING.md for more details. |
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.
Fixes three provider bugs found by code review.
Mistral: crash on choices-less chunks
packages/ai/src/providers/mistral.tsaccessedchunk.choices[0]without a guard. Endpoints that emit usage-only chunks (choicesmissing, empty, orundefined— common whenstream_options.include_usageis on) threw aTypeErrorand aborted the whole stream. Guard withArray.isArray(chunk.choices), matching the existing pattern inopenai-completions.ts.Codex: non-retryable HTTP errors were retried
In
packages/ai/src/providers/openai-codex-responses.ts, whenisRetryableError()classified an HTTP status (e.g. 400/401/403) as non-retryable, the code threw — but thethrowsat inside the sametryas thefetch, so it was caught by the loop's owncatch, which retried unless the message happened to contain "usage limit". Non-retryable errors now setlastErrorandbreak, and are thrown once after the loop.faux: cache-write tokens double-counted
In
packages/ai/src/providers/faux.ts,inputsubtracted onlycacheRead, so cached-write tokens stayed ininputwhile also being counted incacheWrite, inflatingtotalTokens. Subtract both, matchingopenai-completions.tsusage accounting. First-turn behavior is nowinput: 0, cacheWrite: promptTokensinstead of counting the prompt twice.Note
Fix Mistral
choicesguard, Codex non-retryable errors, and faux cache-write token countconsumeChatStreamin mistral.ts, guardchunk.choices[0]access so usage-only chunks that lack achoicesarray are skipped instead of throwing.streamOpenAICodexResponsesin openai-codex-responses.ts, non-OK HTTP responses now setlastErrorand break the retry loop, so errors like 401/403 fail fast instead of being retried.withUsageEstimatein faux.ts, input tokens are nowmax(0, promptTokens - cacheRead - cacheWrite)in all cases, preventing cache-write tokens from being double-counted as input.fauxproviderusage.inputvalues change for sessions with prompt caching enabled; consumers relying on the old (inflated) input count will see lower numbers.Macroscope summarized c240b2b.