Skip to content

fix: preserve measured streaming chat token usage - #448

Merged
devSejung merged 1 commit into
mainfrom
fix/openai-stream-usage-provenance
Nov 6, 2026
Merged

devSejung merged 1 commit into
mainfrom
fix/openai-stream-usage-provenance

Conversation

@devSejung

Copy link
Copy Markdown
Owner

Closes #447. Follow-up to #446.

What Problem This Solves

A successful OpenAI-compatible Chat Completions stream can return a provider token aggregate that the report records as zero. For example, a final usage-only event with total_tokens=42 was dropped when it did not include the input and output split. An empty trailing usage object could also erase a measurement received earlier in the stream.

Why This Change Was Made

Both stream implementations now use the shared usage updater to retain valid provider totals, preserve measured data when a weaker sample arrives, and distinguish absent or partial usage from a measured zero. The existing retained-session report consumes the provenance marker and retains authoritative billed cost separately from token coverage.

User Impact

The usage report keeps the measured total supplied by the model endpoint. A successful response without measured tokens still contributes one retained usage record and reports unknown tokens. A genuine zero is reported as zero. Historical tokens discarded before this change require a separate source to recover.

AI-assisted change.

Evidence

Validation ran in a task-owned Ubuntu 22.04 Docker container with Node 24.18.0 and pnpm 11.15.1. Commit 09eabc0 is based on 0a5f031. The full-index patch SHA-256 was 5907b65953aaad8755a2cd416cb5a949972a125b46283b1941bed3346b0509ae in both Linux and Windows.

  • The baseline failed eight regression assertions across the package and managed streaming implementations, including 42 measured tokens becoming 0 and a measured 30 becoming 0 after an empty chunk. The final parity test passed 26 cases. The 15 sibling suites passed 489 tests across the provider, stream, Gateway, report, and SQLite paths.
  • The AI package build passed. A plain Node consumer resolved the built public and internal entries and verified that the existing exported parser still accepts the same arguments and returns flat usage.
  • The built provider and managed transport each made a real loopback HTTP request and consumed SSE. Their output was stored through the canonical SQLite persistence layer, then queried over the real report HTTP route with in-process Gateway RPC dispatch. Both returned one call and 42 tokens for a measured aggregate, one call and null for missing/partial coverage, one call and zero for explicit zero, and one call and 30 for an empty trailing sample. Partial CSV returned 503. These were deterministic local provider fixtures.
  • Fresh autoreview was scoped-clean through P2. The only subsequent edits were the two canonical import-order moves; the final formatted patch passed the transport, package, and report checks.
  • PlatformClaw quick and full changed-surface checks, targeted format, and git diff --check all exited 0. Net production growth is 80 lines to preserve provider telemetry and usage provenance while keeping the package parser compatible, plus 57 test lines.

@devSejung
devSejung marked this pull request as ready for review November 5, 2026 16:18
@devSejung
devSejung merged commit a97b830 into main Nov 6, 2026
106 checks passed
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.

[Bug]: Chat Completions streams can drop measured token totals

1 participant