You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Overview
Capture token usage and duration when streams are interrupted. The AI
SDK provides usage data even when streams are aborted, so we should
track it for accurate cost accounting.
## Changes
### Backend Changes
- **StreamAbortEvent type**: Added optional `metadata` with `usage` and
`duration` fields
- **streamManager.ts**:
- Extracted `getStreamMetadata()` helper to eliminate duplication
- Used in both `cancelStreamSafely()` (abort case) and stream completion
- Single source of truth for usage/duration extraction
- **StreamingMessageAggregator**: Store usage from abort events using
spread operator (consistent with stream-end handling)
### Testing
- Added integration test: "should include usage data in stream-abort
events"
- Verifies abort events contain usage metadata with token counts
- Tests both OpenAI and Anthropic providers
## Why This Matters
- **Accurate cost tracking**: Even interrupted work consumes tokens and
costs money
- **Budget transparency**: Users can see what they paid for, even for
partial results
- **DRY code**: Single helper method eliminates duplication between
abort and completion paths
## No UI Changes
This PR focuses solely on backend tracking. Usage data is now available
in abort events for future UI consumption.
_Generated with `cmux`_
0 commit comments