Skip to content
TypeScript

fix(claude): context meter shows where Claude actually auto-compacts (#22654)

GitJarno159a914d6b99
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
index d758984f6a5..d2d41831f8c 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
@@ -6504,7 +6504,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
? [event.providerTurn.tokenUsage.autoCompactThreshold]
: [],
),
- [150_000, 150_000, 150_000, 150_000],
+ [117_000, 117_000, 117_000, 117_000],
);
}).pipe(
Effect.provide(
@@ -6515,10 +6515,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
it.effect.each([
{ autoCompactWindow: "", model: "claude-sonnet-4-6", threshold: undefined },
- { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 150_000 },
+ { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 117_000 },
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
- { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 650_000 },
+ { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 },
])(
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
({ autoCompactWindow, model, threshold }) =>
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
index 1d51bd7ef0c..08a103b3868 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
@@ -234,10 +234,14 @@ function claudePromptCacheTtlMs(
}
// Claude caps the window at the model's context, so a setting at or above it never applies.
+// Below that, Claude compacts 33,000 tokens short of the window: Claude Code 2.1.305's
+// getContextUsage reports a 150,000 window as a 117,000 threshold, for every catalog model.
+const CLAUDE_AUTO_COMPACT_RESERVE_TOKENS = 33_000;
+
function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
- const threshold = Number(autoCompactWindow);
- return threshold > 0 && (maxTokens === null || threshold < maxTokens)
- ? { autoCompactThreshold: threshold }
+ const windowTokens = Number(autoCompactWindow);
+ return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens)
+ ? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS }
: {};
}