Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6077,7 +6077,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(
Expand All @@ -6088,10 +6088,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 }) =>
Expand Down
10 changes: 7 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,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 }
: {};
}

Expand Down
Loading