Commit 60bd716
fix: use real outputTokens from assistant.message to estimate CLI session tokens
Sessions without session.shutdown events were massively undercounting
tokens because character-based estimation ignores context accumulation
in agent sessions. Investigation found:
- Real input:output ratio in completed sessions: ~100-200x
- Cache reads ≈ input tokens (prompt caching caches full context)
- A session with 387 tool calls showed 92K real output tokens but only
13K estimated, with real total likely 10-25M
Changes:
1. assistant.message events carry real API-reported outputTokens per
response. Capture these directly instead of estimating from text.
2. When no session.shutdown exists but we have real output tokens,
estimate input using observed ratios from completed sessions:
- >20 tool calls: 130x (heavy agent)
- >5 tool calls: 50x (moderate)
- <=5 tool calls: 10x (light/chat)
Cache reads estimated as equal to input (empirically ~50% of total).
3. Also fix tool.result -> tool.execution_complete event type (wrong
event name meant tool outputs were silently skipped in fallback).
4. Count tool.execution_start to drive the ratio selection.
Result: sessions without shutdown now estimate correctly in the right
order of magnitude instead of being off by 100x. Sessions WITH
session.shutdown are unaffected (still use exact API data).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 1ade50a commit 60bd716
3 files changed
Lines changed: 42 additions & 8 deletions
File tree
- .github/skills/copilot-log-analysis
- vscode-extension/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
183 | 185 | | |
184 | 186 | | |
185 | 187 | | |
186 | 188 | | |
187 | 189 | | |
188 | 190 | | |
189 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
190 | 194 | | |
191 | 195 | | |
192 | 196 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | | - | |
| 150 | + | |
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1499 | 1499 | | |
1500 | 1500 | | |
1501 | 1501 | | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
1502 | 1505 | | |
1503 | 1506 | | |
1504 | 1507 | | |
| |||
1579 | 1582 | | |
1580 | 1583 | | |
1581 | 1584 | | |
1582 | | - | |
1583 | | - | |
1584 | | - | |
1585 | | - | |
1586 | | - | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
1587 | 1599 | | |
1588 | 1600 | | |
1589 | 1601 | | |
| |||
1596 | 1608 | | |
1597 | 1609 | | |
1598 | 1610 | | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
1599 | 1629 | | |
1600 | 1630 | | |
1601 | 1631 | | |
| |||
0 commit comments