Repository navigation
fix(providers): accept upstream usage counts that disagree - #164
Merged
Merged
Conversation
Upstreams sometimes report a detail count above its parent (reasoning above output, cached above input) or a total that differs from input plus output, for example in the middle of a stream. Prism failed the whole turn on those snapshots. Keep the reported counts, recompute the total from input and output, and reject only values that are not valid token counts.
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.
Why
A Chat Completions upstream with reasoning sometimes sends a mid-stream usage snapshot where
reasoning_tokensis one abovecompletion_tokens(121 output, 122 reasoning). Prism treated that as malformed and failed the whole turn withusage detail exceeds inclusive count, even though the answer itself was fine. The same strict checks existed for cached vs input tokens and fortotal_tokensvs input plus output in four providers.Scope
customchat,customresponses,codex: keep reported cached and reasoning counts even when they exceed their parent count; settotal_tokensto input plus output instead of failing on a different upstream total. Thecustomchatadjustment for upstreams that count reasoning only intotal_tokensstays.antigravity: keep cached counts above the prompt count.Blast Radius
Usage reporting for every provider using these four decoders. Turns that used to fail on inconsistent counts now complete and report the upstream numbers. Usage stats can show reasoning above output when an upstream reports it so. The only place that subtracts cached from input already clamps at zero.
Verification
go test ./internal/...: pass.go vet ./internal/providers/...: pass.TestChatUsagePublishesUpstreamCountsThatDisagreecovers streaming and non-streaming responses with cached above input, reasoning above output and a wrong total.usage detail exceeds inclusive count. A daemon built from this branch on a separate port completed 3 of 3 such turns. The upstream glitch is intermittent: a later run on the released daemon also passed.Model: claude-opus-5-5