Skip to content

fix(providers): accept upstream usage counts that disagree - #164

Merged
deLiseLINO merged 1 commit into
mainfrom
fix/accept-upstream-usage
Dec 3, 2026
Merged

deLiseLINO merged 1 commit into
mainfrom
fix/accept-upstream-usage

Conversation

@deLiseLINO

Copy link
Copy Markdown
Owner

Why

A Chat Completions upstream with reasoning sometimes sends a mid-stream usage snapshot where reasoning_tokens is one above completion_tokens (121 output, 122 reasoning). Prism treated that as malformed and failed the whole turn with usage detail exceeds inclusive count, even though the answer itself was fine. The same strict checks existed for cached vs input tokens and for total_tokens vs input plus output in four providers.

Scope

  • customchat, customresponses, codex: keep reported cached and reasoning counts even when they exceed their parent count; set total_tokens to input plus output instead of failing on a different upstream total. The customchat adjustment for upstreams that count reasoning only in total_tokens stays.
  • antigravity: keep cached counts above the prompt count.
  • Non-numeric, negative, fractional and overflowing counts are still rejected.

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.
  • New TestChatUsagePublishesUpstreamCountsThatDisagree covers streaming and non-streaming responses with cached above input, reasoning above output and a wrong total.
  • Live: the released daemon failed a CLI agent turn on the reasoning model with 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

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.
@deLiseLINO
deLiseLINO merged commit 29aee2e into main Dec 3, 2026
3 checks passed
@deLiseLINO
deLiseLINO deleted the fix/accept-upstream-usage branch December 3, 2026 20:09
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.

1 participant