Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(context-window): cap CLAUDE_CODE_AUTO_COMPACT_WINDOW at 1,000,000
Jack Rudenko5406c31d9720
diff --git a/ai-docs/architecture/context-window.md b/ai-docs/architecture/context-window.mdindex e5de3f56..54d426c3 100644--- a/ai-docs/architecture/context-window.md+++ b/ai-docs/architecture/context-window.md@@ -6,6 +6,8 @@Claude Code resolves its compaction point as `min(CLAUDE_CODE_AUTO_COMPACT_WINDOW, maxContextTokens(model))`, and `maxContextTokens` falls back to a hardcoded **200,000** for any model name it does not recognise — which is every model claudish proxies. Setting only the first lever therefore accomplishes nothing: the real window is accepted and then thrown away by the `min()`. `resolveContextWindowEnv()` (`claude-runner.ts`) sets **both**, and Claude Code honours `CLAUDE_CODE_MAX_CONTEXT_TOKENS` only for model names not starting with `claude-`, i.e. exactly the pure-proxy case.+**`CLAUDE_CODE_AUTO_COMPACT_WINDOW` has a ceiling of 1,000,000.** Claude Code clamps a larger value and prints `... is too large, so Claude Code treats it as the 1M maximum` on every launch (measured on 2.1.304 with catalog windows of 1,050,000 and 1,007,997). claudish sends `min(window, MAX_AUTO_COMPACT_WINDOW)`, which moves no compaction point. `CLAUDE_CODE_MAX_CONTEXT_TOKENS` raised no warning at the same values and keeps the real window.+**The env is read at SPAWN and never revisited.** A long-running claudish process keeps whatever window it was launched with; upgrading the package on disk does nothing for a live session. Diagnosing this is easy once you know the tell: inside ONE pre-fix process, subagents pinned to `model: "opus"` (a name Claude Code knows as 1M) compact at ~340K while the main thread on the proxied model compacts at ~170K. Ground truth is `compactMetadata.preTokens` in the Claude Code transcript.**The status line reports the ENFORCED window, not the spec window.** `TokenTracker.writeFile` derives `context_left_percent` from `inputTokens` alone — folding in the session-CUMULATIVE `outputTokens` made the value decay with session AGE and pin at 0 forever once lifetime output passed the window (measured: input 94,018 of a 372,000 window reporting 0% left). Both generated status-line variants then recompute against `min(spec window, CLAUDE_CODE_MAX_CONTEXT_TOKENS ?? 200000, CLAUDE_CODE_AUTO_COMPACT_WINDOW)` and render `18% (164k/200k of 372k)` when those disagree, so a clamp is visible on day one instead of costing days of half-context work.diff --git a/packages/cli/src/claude-runner-context-window.test.ts b/packages/cli/src/claude-runner-context-window.test.tsindex f61f6e05..822443a5 100644--- a/packages/cli/src/claude-runner-context-window.test.ts+++ b/packages/cli/src/claude-runner-context-window.test.ts@@ -3,6 +3,7 @@ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import {+ MAX_AUTO_COMPACT_WINDOW,MIN_AUTO_COMPACT_WINDOW,computeMainThreadContextWindow,resolveContextWindowEnv,@@ -157,6 +158,29 @@ describe("computeMainThreadContextWindow", () => {});describe("resolveContextWindowEnv", () => {+ test("exposes the Claude Code auto-compaction ceiling", () => {+ expect(MAX_AUTO_COMPACT_WINDOW).toBe(1_000_000);+ });++ test("clamps a 1,050,000-token auto-compact window while preserving the real max-context cap", () => {+ const { vars } = resolveContextWindowEnv(1_050_000, {});++ expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1000000");+ expect(vars[ENV.CLAUDE_CODE_MAX_CONTEXT_TOKENS]).toBe("1050000");+ });++ test("sets the auto-compact window exactly at the ceiling", () => {+ const { vars } = resolveContextWindowEnv(1_000_000, {});++ expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1000000");+ });++ test("preserves a 372,000-token auto-compact window below the ceiling", () => {+ const { vars } = resolveContextWindowEnv(372_000, {});++ expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("372000");+ });+test("regression: setting the max-context cap fails without the clamp fix", () => {const { vars } = resolveContextWindowEnv(1_048_576, {});@@ -168,7 +192,7 @@ describe("resolveContextWindowEnv", () => {const { vars, notice } = resolveContextWindowEnv(realWindow, {});- expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1048576");+ expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1000000");expect(notice).toContain(`${realWindow.toLocaleString()} tokens`);});@@ -222,7 +246,7 @@ describe("resolveContextWindowEnv", () => {const { vars } = resolveContextWindowEnv(1_048_576, processEnv);expect(vars).not.toHaveProperty(ENV.CLAUDE_CODE_MAX_CONTEXT_TOKENS);- expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1048576");+ expect(vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]).toBe("1000000");});test("returns no variables or notice when both values are user-overridden", () => {diff --git a/packages/cli/src/claude-runner.ts b/packages/cli/src/claude-runner.tsindex a13abe63..0e880327 100644--- a/packages/cli/src/claude-runner.ts+++ b/packages/cli/src/claude-runner.ts@@ -1208,6 +1208,21 @@ export function mergeUserSettingsIfPresent(*/export const MIN_AUTO_COMPACT_WINDOW = 200_000;+/**+ * Claude Code's ceiling for `CLAUDE_CODE_AUTO_COMPACT_WINDOW`. A larger value is+ * clamped to it, but Claude Code also prints a warning on EVERY launch:+ *+ * CLAUDE_CODE_AUTO_COMPACT_WINDOW="1050000" is too large, so Claude Code+ * treats it as the 1M maximum. ...+ *+ * Measured 2026-10-20, Claude Code 2.1.304: `[email protected]` (catalog window+ * 1,050,000) and `[email protected]` (1,007,997). Sending the ceiling changes+ * no compaction point, since Claude Code uses it anyway, and drops the warning.+ * `CLAUDE_CODE_MAX_CONTEXT_TOKENS` printed no warning at the same values, so it+ * keeps the real window.+ */+export const MAX_AUTO_COMPACT_WINDOW = 1_000_000;+/*** How long `computeMainThreadContextWindow` will wait on the cloud catalog before* giving up and launching with today's behaviour (window unknown → env unset).@@ -1334,7 +1349,9 @@ export function resolveContextWindowEnv(if (processEnv[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW]) return { vars };if (realWindow >= MIN_AUTO_COMPACT_WINDOW) {- vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW] = String(realWindow);+ vars[ENV.CLAUDE_CODE_AUTO_COMPACT_WINDOW] = String(+ Math.min(realWindow, MAX_AUTO_COMPACT_WINDOW)+ );return {vars,notice: