Skip to content

Commit 2d55610

Browse files
committed
fix(claude): context meter shows where Claude actually auto-compacts
1 parent 61e2747 commit 2d55610

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4577,7 +4577,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
45774577
? [event.providerTurn.tokenUsage.autoCompactThreshold]
45784578
: [],
45794579
),
4580-
[150_000, 150_000, 150_000, 150_000],
4580+
[117_000, 117_000, 117_000, 117_000],
45814581
);
45824582
}).pipe(
45834583
Effect.provide(
@@ -4588,10 +4588,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
45884588

45894589
it.effect.each([
45904590
{ autoCompactWindow: "", model: "claude-sonnet-4-6", threshold: undefined },
4591-
{ autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 150_000 },
4591+
{ autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 117_000 },
45924592
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
45934593
{ autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined },
4594-
{ autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 650_000 },
4594+
{ autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 },
45954595
])(
45964596
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
45974597
({ autoCompactWindow, model, threshold }) =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,14 @@ function claudePromptCacheTtlMs(
213213
}
214214

215215
// Claude caps the window at the model's context, so a setting at or above it never applies.
216+
// Below that, Claude compacts 33,000 tokens short of the window: Claude Code 2.1.305's
217+
// getContextUsage reports a 150,000 window as a 117,000 threshold, for every catalog model.
218+
const CLAUDE_AUTO_COMPACT_RESERVE_TOKENS = 33_000;
219+
216220
function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
217-
const threshold = Number(autoCompactWindow);
218-
return threshold > 0 && (maxTokens === null || threshold < maxTokens)
219-
? { autoCompactThreshold: threshold }
221+
const windowTokens = Number(autoCompactWindow);
222+
return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens)
223+
? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS }
220224
: {};
221225
}
222226

0 commit comments

Comments
 (0)