Skip to content

Commit 159a914

Browse files
GitJarnomaria-rcks
andauthored
fix(claude): context meter shows where Claude actually auto-compacts (#22654)
Co-authored-by: maria-rcks <[email protected]>
1 parent c856e00 commit 159a914

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
@@ -6504,7 +6504,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
65046504
? [event.providerTurn.tokenUsage.autoCompactThreshold]
65056505
: [],
65066506
),
6507-
[150_000, 150_000, 150_000, 150_000],
6507+
[117_000, 117_000, 117_000, 117_000],
65086508
);
65096509
}).pipe(
65106510
Effect.provide(
@@ -6515,10 +6515,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
65156515

65166516
it.effect.each([
65176517
{ 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 },
65196519
{ autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined },
65206520
{ 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 },
65226522
])(
65236523
"ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model",
65246524
({ 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
@@ -234,10 +234,14 @@ function claudePromptCacheTtlMs(
234234
}
235235

236236
// 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+
237241
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 }
241245
: {};
242246
}
243247

0 commit comments

Comments
 (0)