Commit 1425e24
authored
🤖 fix: correct context usage display for multi-step tool calls (#893)
## Problem
The Context Usage UI was showing inflated `cachedInputTokens` for plan
messages with multi-step tool calls (e.g., ~150k instead of ~50k),
causing the context bar to show >200k tokens which is impossible since
the user can still send messages.
## Root Cause
For multi-step tool calls, `cachedInputTokens` was being summed across
all steps:
- Step 1: reads 50k cached tokens
- Step 2: reads same 50k cached tokens
- Step 3: reads same 50k cached tokens
- Cumulative: 150k (wrong for context display)
The frontend was falling back to cumulative `usage` when `contextUsage`
was undefined, which had the incorrectly summed values.
## Changes
**Backend (`streamManager.ts`)**:
- Refactor `getStreamMetadata()` to fetch both `totalUsage` (sum of all
steps, for costs) and `contextUsage` (last step only, for context
window) from AI SDK
- Add `contextProviderMetadata` from `streamResult.providerMetadata` for
accurate cache creation token display
- Falls back to tracked `lastStepUsage`/`lastStepProviderMetadata` if
streamResult times out
**Frontend (`WorkspaceStore.ts`)**:
- Remove fallback from `contextUsage` to `usage` - only use
`contextUsage` for context window display
## Result
Context window now correctly shows last step's `inputTokens` (actual
context size) while cost calculation still uses cumulative totals.
## Migration Note
1 parent 4e85392 commit 1425e24
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