Commit 8c2fe01
authored
🤖 Track usage in stream abort events (#110)
## 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`_1 parent df5673e commit 8c2fe01
File tree
5 files changed
+113
-18
lines changed- src
- services
- types
- utils/messages
- tests/ipcMain
5 files changed
+113
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
1156 | 1157 | | |
1157 | 1158 | | |
1158 | 1159 | | |
1159 | | - | |
1160 | | - | |
1161 | | - | |
1162 | | - | |
1163 | | - | |
1164 | | - | |
1165 | | - | |
1166 | | - | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
1170 | 1164 | | |
1171 | | - | |
| 1165 | + | |
1172 | 1166 | | |
1173 | 1167 | | |
1174 | 1168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
219 | 220 | | |
220 | 221 | | |
221 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
222 | 250 | | |
223 | 251 | | |
224 | 252 | | |
| |||
243 | 271 | | |
244 | 272 | | |
245 | 273 | | |
246 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
247 | 278 | | |
248 | 279 | | |
249 | 280 | | |
250 | 281 | | |
| 282 | + | |
251 | 283 | | |
252 | 284 | | |
253 | 285 | | |
| |||
580 | 612 | | |
581 | 613 | | |
582 | 614 | | |
583 | | - | |
584 | | - | |
| 615 | + | |
| 616 | + | |
585 | 617 | | |
586 | 618 | | |
587 | 619 | | |
| |||
594 | 626 | | |
595 | 627 | | |
596 | 628 | | |
597 | | - | |
| 629 | + | |
598 | 630 | | |
599 | 631 | | |
600 | 632 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
50 | 55 | | |
51 | 56 | | |
52 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
268 | | - | |
| 268 | + | |
269 | 269 | | |
270 | 270 | | |
271 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
272 | 276 | | |
273 | 277 | | |
274 | 278 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
127 | 187 | | |
128 | 188 | | |
129 | 189 | | |
| |||
0 commit comments