Skip to content

Commit 600762c

Browse files
committed
fix(claude): context meter shows the configured auto-compact threshold
The Claude adapter reports usage as provider-turn token usage, which had no threshold field, and the web hardcoded a null threshold for live usage. The meter fell back to "compacts automatically when needed" even with Auto-compact after set. Carry the configured window on the turn usage, omit it when it is not below the model's context window, and read it in the meter. The tooltip says "about" because Claude triggers a little below the window. Fixes #18307
1 parent bed9239 commit 600762c

7 files changed

Lines changed: 39 additions & 5 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,20 @@ describe("ClaudeAdapterV2 context usage", () => {
928928
undefined,
929929
);
930930
});
931+
932+
it("reports the configured auto-compact window when it is below the context window", () => {
933+
const thresholdFor = (autoCompactWindow: string) =>
934+
ClaudeAdapterV2.claudeProviderTurnTokenUsage(
935+
{ input_tokens: 1, output_tokens: 1 },
936+
CLAUDE_TEST_MODEL_SELECTION,
937+
"2026-08-29T00:00:00.000Z",
938+
autoCompactWindow,
939+
).autoCompactThreshold;
940+
941+
assert.strictEqual(thresholdFor("150000"), 150_000);
942+
assert.strictEqual(thresholdFor("650000"), undefined);
943+
assert.strictEqual(thresholdFor(""), undefined);
944+
});
931945
});
932946

933947
describe("ClaudeAdapterV2 session permissions", () => {

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ function claudePromptCacheTtlMs(
189189
return undefined;
190190
}
191191

192+
// Claude caps the window at the model's context, so a setting at or above it never applies.
193+
function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
194+
const threshold = Number(autoCompactWindow);
195+
return threshold > 0 && (maxTokens === null || threshold < maxTokens)
196+
? { autoCompactThreshold: threshold }
197+
: {};
198+
}
199+
192200
export function claudeProviderTurnTokenUsage(
193201
usage: {
194202
readonly input_tokens: number;
@@ -199,21 +207,24 @@ export function claudeProviderTurnTokenUsage(
199207
},
200208
modelSelection: ModelSelection,
201209
updatedAt: string,
210+
autoCompactWindow = "",
202211
) {
203212
const inputTokens =
204213
usage.input_tokens +
205214
(usage.cache_creation_input_tokens ?? 0) +
206215
(usage.cache_read_input_tokens ?? 0);
207216
const outputTokens = usage.output_tokens;
208217
const promptCacheTtlMs = claudePromptCacheTtlMs(usage.cache_creation);
218+
const maxTokens = claudeContextWindow(modelSelection);
209219
return {
210220
usedTokens: inputTokens + outputTokens,
211-
maxTokens: claudeContextWindow(modelSelection),
221+
maxTokens,
212222
inputTokens,
213223
cachedInputTokens: usage.cache_read_input_tokens ?? 0,
214224
outputTokens,
215225
reasoningOutputTokens: 0,
216226
...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }),
227+
...claudeAutoCompactThreshold(autoCompactWindow, maxTokens),
217228
updatedAt,
218229
};
219230
}
@@ -6789,6 +6800,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
67896800
? Math.round(postTokens)
67906801
: undefined;
67916802
if (afterTokenCount !== undefined) {
6803+
const maxTokens = claudeContextWindow(context.input.modelSelection);
67926804
yield* emitProviderEvent({
67936805
type: "provider_turn.updated",
67946806
driver: CLAUDE_PROVIDER,
@@ -6809,7 +6821,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
68096821
completedAt: null,
68106822
tokenUsage: {
68116823
usedTokens: afterTokenCount,
6812-
maxTokens: claudeContextWindow(context.input.modelSelection),
6824+
maxTokens,
6825+
...claudeAutoCompactThreshold(
6826+
adapterOptions.settings.autoCompactWindow,
6827+
maxTokens,
6828+
),
68136829
updatedAt: DateTime.formatIso(now),
68146830
},
68156831
},
@@ -6962,6 +6978,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
69626978
message.message.usage,
69636979
context.input.modelSelection,
69646980
DateTime.formatIso(now),
6981+
adapterOptions.settings.autoCompactWindow,
69656982
),
69666983
},
69676984
});

‎apps/web/src/components/chat/ContextWindowMeter.logic.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe("formatContextWindowCompactionMessage", () => {
135135

136136
it("shows the configured auto-compaction threshold", () => {
137137
expect(formatContextWindowCompactionMessage("Claude Sonnet 5", 300_000)).toBe(
138-
"Compacts automatically at 300,000 tokens.",
138+
"Compacts automatically at about 300,000 tokens.",
139139
);
140140
});
141141
});

‎apps/web/src/components/chat/ContextWindowMeter.logic.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function formatContextWindowCompactionMessage(
106106
autoCompactThreshold?: number | null,
107107
): string {
108108
if (typeof autoCompactThreshold === "number" && autoCompactThreshold > 0) {
109-
return `Compacts automatically at ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
109+
return `Compacts automatically at about ${autoCompactThreshold.toLocaleString("en-US")} tokens.`;
110110
}
111111
return modelDisplayName
112112
? `Context for ${modelDisplayName} compacts automatically when needed.`

‎apps/web/src/lib/contextWindow.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ describe("live provider-turn usage (#8144)", () => {
6767
maxTokens: 200_000,
6868
inputTokens: 40_000,
6969
outputTokens: 2_000,
70+
autoCompactThreshold: 150_000,
7071
updatedAt: "2026-08-27T00:00:00.000Z",
7172
});
7273
expect(snapshot).not.toBeNull();
7374
expect(snapshot?.usedTokens).toBe(42_000);
7475
expect(snapshot?.maxTokens).toBe(200_000);
7576
expect(snapshot?.remainingTokens).toBe(158_000);
7677
expect(snapshot?.usedPercentage).toBe(21);
78+
expect(snapshot?.autoCompactThreshold).toBe(150_000);
7779
});
7880

7981
it("handles a report without a known context window", () => {

‎apps/web/src/lib/contextWindow.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function deriveLatestContextWindowSnapshot(
7575
toolUses: null,
7676
durationMs: null,
7777
compactsAutomatically: true,
78-
autoCompactThreshold: null,
78+
autoCompactThreshold: liveUsage.autoCompactThreshold ?? null,
7979
cost: null,
8080
updatedAt: liveUsage.updatedAt,
8181
};

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ export const OrchestrationV2ProviderTurnTokenUsage = Schema.Struct({
10641064
/** Prompt cache lifetime the provider wrote with this report. Clients treat
10651065
the cache as cold once `updatedAt` is this old. */
10661066
promptCacheTtlMs: Schema.optional(PositiveInt),
1067+
autoCompactThreshold: Schema.optional(PositiveInt),
10671068
/** ISO timestamp of the provider's report; string so wire encoding is stable. */
10681069
updatedAt: Schema.String,
10691070
});

0 commit comments

Comments
 (0)