Commit f5e1d6c
authored
🤖 fix: correct context usage display for multi-step tool calls (#893)
## Problem
The Context Usage UI was sometimes showing inflated token counts for
multi-step tool calls (e.g., plan messages showing ~150k
`cachedInputTokens` instead of ~50k).
## Root Cause
Two issues combined:
1. **Backend**: On stream-end, we only used `lastStepUsage` tracked from
`finish-step` events. If no `finish-step` event was received (edge
cases), `contextUsage` would be undefined. We weren't fetching
`streamResult.usage` (which contains last step's usage) as the primary
source.
2. **Frontend**: When `contextUsage` was undefined, it fell back to
cumulative `usage`. For multi-step requests, cumulative usage sums
`cachedInputTokens` across all steps (each step reads from cache),
inflating the context window display.
<img width="297" height="72" alt="image"
src="https://github.com/user-attachments/assets/c8a58e76-df24-4003-9d3e-369417173cd3"
/>
## Fix
**Backend (`streamManager.ts`)**:
- Fetch `contextUsage` from `streamResult.usage` (last step) on
stream-end as the primary source
- Fall back to tracked `lastStepUsage` only if `streamResult.usage`
times out
- Each metadata fetch has independent timeout/error handling so one
failure doesn't mask others
**Frontend (`WorkspaceStore.ts`)**:
- Remove fallback from `contextUsage` to `usage` - only use
`contextUsage` for context window display
**Schema (`message.ts`)**:
- Add `contextUsage` and `contextProviderMetadata` to Zod schema to
match TypeScript types
## Migration Note
1 parent 4e85392 commit f5e1d6c
File tree
6 files changed
+44
-25
lines changed- src
- browser
- stores
- stories
- common/orpc/schemas
- node/services
6 files changed
+44
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
227 | | - | |
| 227 | + | |
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
486 | 486 | | |
487 | 487 | | |
488 | 488 | | |
489 | | - | |
| 489 | + | |
490 | 490 | | |
491 | 491 | | |
492 | 492 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| 202 | + | |
202 | 203 | | |
203 | 204 | | |
204 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
| 81 | + | |
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
345 | 348 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
350 | 360 | | |
351 | 361 | | |
352 | | - | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
353 | 365 | | |
354 | 366 | | |
355 | 367 | | |
| |||
1045 | 1057 | | |
1046 | 1058 | | |
1047 | 1059 | | |
1048 | | - | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
1052 | 1072 | | |
1053 | 1073 | | |
1054 | | - | |
1055 | | - | |
1056 | | - | |
1057 | | - | |
1058 | | - | |
1059 | 1074 | | |
1060 | 1075 | | |
1061 | 1076 | | |
| |||
1064 | 1079 | | |
1065 | 1080 | | |
1066 | 1081 | | |
1067 | | - | |
| 1082 | + | |
1068 | 1083 | | |
1069 | 1084 | | |
1070 | 1085 | | |
| |||
0 commit comments