Commit b9c7f0d
authored
feat: support richer token cost tracking for ask (#1353)
* feat(web): track estimated per-tool output token usage in chat
Estimate the input-token footprint of each tool call's output (the cost
the result imposes when fed back to the model on subsequent steps) using
a local length-based estimator, persist it per tool call in the chat
message metadata, and surface it inline in each tool call row next to
the Details toggle. Estimates are ~-prefixed to keep them distinct from
the authoritative billed token totals.
* feat(web): track per-step token usage and fix tool output estimation
Record the provider-reported input/output token usage of each agent step
in the chat message metadata and display it per step group in the
thinking steps view (joined to UI step groups via the step index now
tagged on each tool token usage entry).
Also fix the tool output estimator to measure the model-visible payload:
tools with a toModelOutput mapping (all builtins) send only their output
text to the model, so estimating the raw ToolResult object was counting
UI-only metadata the model never sees. The bytes-per-token ratio is now
a uniform ~2 chars/token, calibrated against provider-reported per-step
usage of code-heavy tool results.
* improve detailsCard display for the step token usage
* refactor(web): derive token usage post-stream and join steps by position
Collect usage from researchStream.steps and response.messages after the
stream completes (covers approval-gated and failed tool calls, off the
hot path), nest tool estimates under their step in a single
stepTokenUsage array, and join UI steps to entries by stepIndex.
* docs: update changelog for token cost tracking
* fix(web): prevent caller metadata from overwriting derived token fields
Spread caller-supplied metadata before the derived token fields so
stepTokenUsage and the totals can't be clobbered, which would desync
the UI's index-based step join.
* refactor(web): move token estimation into EE chat
---------
Co-authored-by: Jack Minnetian <270441393+BlueBottleLatte@users.noreply.github.com>1 parent 26435a4 commit b9c7f0d
13 files changed
Lines changed: 486 additions & 116 deletions
File tree
- packages/web/src
- ee/features/chat
- components/chatThread
- tools
- features/chat
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
| 141 | + | |
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
190 | 191 | | |
191 | 192 | | |
192 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 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 | + | |
193 | 247 | | |
194 | 248 | | |
195 | 249 | | |
196 | 250 | | |
| 251 | + | |
| 252 | + | |
197 | 253 | | |
198 | 254 | | |
199 | 255 | | |
200 | 256 | | |
201 | 257 | | |
202 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
203 | 262 | | |
204 | 263 | | |
205 | | - | |
206 | 264 | | |
207 | 265 | | |
208 | 266 | | |
| |||
430 | 488 | | |
431 | 489 | | |
432 | 490 | | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
433 | 498 | | |
434 | 499 | | |
435 | 500 | | |
| |||
Lines changed: 51 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
119 | 141 | | |
120 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
121 | 145 | | |
122 | 146 | | |
123 | 147 | | |
| |||
379 | 403 | | |
380 | 404 | | |
381 | 405 | | |
| 406 | + | |
382 | 407 | | |
383 | 408 | | |
384 | 409 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
0 commit comments