Commit 28b8f2b
authored
Read max context from litellm + compact at 90% (#54)
* Read max context from litellm.get_model_info + compact at 90%
The local _MAX_TOKENS_MAP had Claude Opus 4.6 pinned at 200k, triggering
compaction at ~20% of its real 1M window. Swap the hand-maintained table
for litellm.get_model_info()['max_input_tokens'], which LiteLLM keeps
in sync upstream (Opus 4.6=1M, GPT-5=272k, Sonnet 4.5=200k, etc.). HF
router-only ids (MiniMax, Kimi, GLM) aren't in litellm's catalog and
fall through to the 200k default — close enough to their advertised
ranges and safe if the model lies.
Also shifts compaction to fire at 90% of max_context instead of >100%.
The old condition waited until context had already overshot, risking
the next LLM call hitting ContextWindowExceededError before the compact
finished. 90% gives headroom for the summary call + one more turn.
* Drop the legacy -10k buffer — the 90% ratio is the headroom now
ContextManager was subtracting a fixed 10k tokens from max_context on top
of the new 90% compaction threshold, so a 1M-window Opus was triggering
at 891k instead of the intended 900k. Keep max_context == the real model
ceiling; _COMPACT_THRESHOLD_RATIO is the single source of headroom.
* Rename max_context -> model_max_tokens
'max_context' read ambiguously — some reviewers assumed it meant the
compaction threshold. Rename to 'model_max_tokens' so it's unmistakably
the model's real input-token ceiling (what litellm.get_model_info
reports), distinct from the internally-computed compaction threshold.
Touches the ContextManager attribute + ctor param, Session's constructor
kwarg and update_model setter, and the agent loop's debug/warning logs.
No behavior change.
* Simplify compaction gate + rename context_length -> running_context_usage
- ContextManager gains compaction_threshold and needs_compaction
properties so callers stop recomputing "90% of model_max_tokens" by
hand. compact() becomes `if not self.needs_compaction: return`.
- Rename self.context_length to self.running_context_usage. The old
name read like a second ceiling value; the new name says what it is —
the last-reported total_tokens from usage. add_message(), the
ContextWindowExceededError handler, and the compact-finish recompute
all updated accordingly.
- Collapsed _compact_and_notify's local-alias dance (old_length /
max_ctx / threshold) into a single `cm = session.context_manager`.1 parent 5ab7c4e commit 28b8f2b
3 files changed
Lines changed: 69 additions & 72 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
93 | 99 | | |
94 | 100 | | |
95 | 101 | | |
| |||
149 | 155 | | |
150 | 156 | | |
151 | 157 | | |
152 | | - | |
| 158 | + | |
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| |||
262 | 268 | | |
263 | 269 | | |
264 | 270 | | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
265 | 284 | | |
266 | 285 | | |
267 | 286 | | |
268 | 287 | | |
269 | 288 | | |
270 | 289 | | |
271 | 290 | | |
272 | | - | |
| 291 | + | |
273 | 292 | | |
274 | 293 | | |
275 | 294 | | |
| |||
325 | 344 | | |
326 | 345 | | |
327 | 346 | | |
328 | | - | |
| 347 | + | |
329 | 348 | | |
330 | 349 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
184 | | - | |
| 183 | + | |
| 184 | + | |
185 | 185 | | |
186 | | - | |
187 | | - | |
| 186 | + | |
| 187 | + | |
188 | 188 | | |
189 | | - | |
190 | | - | |
| 189 | + | |
191 | 190 | | |
192 | | - | |
| 191 | + | |
193 | 192 | | |
194 | 193 | | |
195 | | - | |
196 | | - | |
| 194 | + | |
| 195 | + | |
197 | 196 | | |
198 | 197 | | |
199 | | - | |
200 | | - | |
| 198 | + | |
201 | 199 | | |
202 | 200 | | |
203 | 201 | | |
204 | 202 | | |
205 | | - | |
| 203 | + | |
206 | 204 | | |
207 | 205 | | |
208 | 206 | | |
| |||
576 | 574 | | |
577 | 575 | | |
578 | 576 | | |
579 | | - | |
| 577 | + | |
580 | 578 | | |
581 | 579 | | |
582 | 580 | | |
583 | 581 | | |
584 | | - | |
585 | | - | |
| 582 | + | |
| 583 | + | |
586 | 584 | | |
587 | 585 | | |
588 | 586 | | |
| |||
785 | 783 | | |
786 | 784 | | |
787 | 785 | | |
| 786 | + | |
788 | 787 | | |
789 | 788 | | |
790 | | - | |
791 | | - | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
| 789 | + | |
| 790 | + | |
798 | 791 | | |
| 792 | + | |
799 | 793 | | |
800 | 794 | | |
801 | 795 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | 18 | | |
30 | 19 | | |
31 | 20 | | |
32 | 21 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
38 | 30 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 31 | + | |
42 | 32 | | |
43 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
44 | 38 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
65 | 49 | | |
66 | 50 | | |
67 | 51 | | |
| |||
101 | 85 | | |
102 | 86 | | |
103 | 87 | | |
104 | | - | |
| 88 | + | |
105 | 89 | | |
106 | 90 | | |
107 | 91 | | |
| |||
153 | 137 | | |
154 | 138 | | |
155 | 139 | | |
156 | | - | |
| 140 | + | |
157 | 141 | | |
158 | 142 | | |
159 | 143 | | |
| |||
0 commit comments