The best way to control Claude Code, Codex and more from any computer or phone
fix(claude): context meter shows where Claude actually auto-compacts (#22654)
Co-authored-by: maria-rcks <maria@kuuro.net>
GitJarno committed
Oct 31, 2026 at 03:24 UTC
159a914d6b99e6844f2796da44a6dee101f35d59
2 files changed
+10
-6
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+3
-3
@@ -6504,7 +6504,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6504
? [event.providerTurn.tokenUsage.autoCompactThreshold]
6505
: [],
6506
),
6507
- [150_000, 150_000, 150_000, 150_000],
6507
+ [117_000, 117_000, 117_000, 117_000],
6508
);
6509
}).pipe(
6510
Effect.provide(
@@ -6515,10 +6515,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
6515
6516
it.effect.each([
6517
{ autoCompactWindow: "", model: "claude-sonnet-4-6", threshold: undefined },
6518
- { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 150_000 },
6518
+ { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 117_000 },
6519
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
6520
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
6521
- { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 650_000 },
6521
+ { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 },
6522
])(
6523
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
6524
({ autoCompactWindow, model, threshold }) =>
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+7
-3
@@ -234,10 +234,14 @@ function claudePromptCacheTtlMs(
234
}
235
236
// Claude caps the window at the model's context, so a setting at or above it never applies.
237
+// Below that, Claude compacts 33,000 tokens short of the window: Claude Code 2.1.305's
238
+// getContextUsage reports a 150,000 window as a 117,000 threshold, for every catalog model.
239
+const CLAUDE_AUTO_COMPACT_RESERVE_TOKENS = 33_000;
240
+
241
function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
238
- const threshold = Number(autoCompactWindow);
239
- return threshold > 0 && (maxTokens === null || threshold < maxTokens)
240
- ? { autoCompactThreshold: threshold }
242
+ const windowTokens = Number(autoCompactWindow);
243
+ return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens)
244
+ ? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS }
245
: {};
246
}
247