Skip to content

fix(usage): retain reported usage on canceled or failed streams - #60

Open
rexdotsh wants to merge 1 commit into
mainfrom
fix/stream-usage-on-interruption
Open

rexdotsh wants to merge 1 commit into
mainfrom
fix/stream-usage-on-interruption

Conversation

@rexdotsh

Copy link
Copy Markdown
Owner

Summary

  • Record provider-reported token usage once when a Claude or Codex SSE stream is canceled or fails, not only at a clean EOF.
  • Claude message_start already reports input/cache tokens; preserve those even when a turn ends before message_delta. Codex can preserve any usage already observed in a parsed event before cancellation.
  • Cover Claude cancellation and upstream failure, plus Codex cancellation after a usage-bearing event.

Limit: this cannot recover tokens the upstream has not reported yet. In particular, Codex commonly sends usage only in its terminal event, so a cancellation before that event can still be unmeasured. No estimates are fabricated.

Addresses the recoverable part of audit finding #7 only, independently of #49 and the other audit PRs. bun test (143 passing), bun lint, bun typecheck. No live provider requests. Leave unmerged for review.

This branch has not been deployed

No deployments
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