Commit 9975b99
authored
🤖 fix: exclude reasoning tokens from context threshold calculation (#1275)
## Summary
Fixes compaction loop with Anthropic Extended Thinking models.
## Problem
The `getTotalTokens()` function included `reasoning.tokens` in context
usage calculation, but reasoning tokens are **OUTPUT** from the current
response, not part of the input context window.
Extended Thinking can generate 50k+ reasoning tokens per response,
incorrectly inflating context percentage and triggering force-compaction
even with low actual context (e.g., 20k input + 50k reasoning = 70k
"context" = 35% on 200k window, but 75k would trigger at 37.5%).
Per Anthropic docs:
> "The Anthropic API automatically ignores thinking blocks from previous
turns and they are not included when calculating context usage."
So `inputTokens` already correctly reflects context size.
## Changes
1. **Primary fix**: Rename `getTotalTokens` → `getContextTokens`,
exclude output/reasoning tokens
2. **Secondary fix**: Preserve signatures when coalescing consecutive
reasoning parts (signatures arrive at end of streaming, were being lost
during merge)
3. **Tests**: Add test case verifying reasoning tokens don't trigger
compaction
## Testing
- `bun test src/browser/utils/compaction/autoCompactionCheck.test.ts` -
34 pass
- `bun test src/browser/utils/messages/modelMessageTransform.test.ts` -
46 pass
- `make typecheck` - pass
---
_Generated with `mux` • Model: `anthropic:claude-opus-4-5` • Thinking:
`high`_1 parent db460aa commit 9975b99
File tree
3 files changed
+64
-19
lines changed- src/browser/utils
- compaction
- messages
3 files changed
+64
-19
lines changedLines changed: 35 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | | - | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
147 | | - | |
148 | | - | |
| 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 | + | |
149 | 176 | | |
150 | 177 | | |
151 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
582 | 582 | | |
583 | 583 | | |
584 | 584 | | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
585 | 603 | | |
586 | 604 | | |
587 | 605 | | |
| |||
0 commit comments