### Preflight Checklist
- [x] I have searched [existing issues](https://github.…com/anthropics/claude-code/issues?q=is%3Aissue%20state%3Aopen%20label%3Abug) and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
### What's Wrong?
Auto-compact (automatic context compaction) stopped triggering for users of third-party Anthropic-compatible API providers (e.g., Zhipu AI, Azure Bedrock, Google Vertex, any `ANTHROPIC_BASE_URL` pointing to a non-Anthropic endpoint). Manual `/compact` still works fine.
This is a **regression** — auto-compact worked perfectly in v2.1.150 and earlier.
## Regression Window
| Version | Auto-compact | Evidence |
|---------|-------------|----------|
| v2.1.117 (May 7-14) | ✅ Works | Triggers at ~167k tokens consistently |
| v2.1.140 (May 14-24) | ✅ Works | Triggers at ~153k tokens, many sessions verified |
| v2.1.150 (May 24 - Jun 3) | ✅ Works | Last working version |
| v2.1.161 (Jun 3) | ❌ **Broken** | 0 auto-compacts, all manual or hard-limit crashes |
| v2.1.162+ (Jun 4-5) | ❌ **Broken** | Same — zero auto-compacts |
**Regression introduced between v2.1.150 and v2.1.161.**
## Root Cause Analysis
Through binary reverse-engineering of the Claude Code executable, I traced the complete call chain for auto-compact and identified the exact gate that changed.
### v2.1.150 — Auto-compact gate (`xU_`):
```js
// v2.1.150: feature flag resolver does NOT require Anthropic auth
function xU_() {
if (!AT()) return false; // env/config check (passes)
if (G28()) return false; // other check (passes)
return V6("tengu_sepia_moth", false); // feature flag → can resolve
}
// V6() → we() = !process.env.DISABLE_GROWTHBOOK && !OS()
// Does NOT check for Anthropic first-party authentication
// → GrowthBook is available, flag can be fetched/cached
// → tengu_sepia_moth resolves correctly → auto-compact works
```
### v2.1.161+ — New auto-compact gate (`_Y8`):
```js
// v2.1.161+: NEW auth gate added, blocks third-party users
function _Y8() {
if (!AT()) return false; // env/config check (passes)
if (!Os()) return false; // NEW: remote env check
if (vl()) return false; // NEW: another feature flag check
return D6("tengu_sepia_moth", false); // feature flag → CANNOT resolve
}
// D6() → qu() → Au() → !LC() → !(dGL() || ...)
// dGL() = !Z7() where Z7() = (XA() === "firstParty")
// → Z7() returns false for third-party API users
// → dGL() returns true
// → LC() returns true
// → Au() returns false (GrowthBook "unavailable")
// → qu() returns false
// → D6("tengu_sepia_moth", false) returns the DEFAULT value: false
// → _Y8() returns false → Auto-compact NEVER triggers
```
### The complete blocking chain for third-party API users:
```
ANTHROPIC_BASE_URL != api.anthropic.com
→ Z7() = false (not first-party auth)
→ dGL() = true (!Z7())
→ LC() = true (dGL() || ...)
→ Au() = false (!LC())
→ qu() = false (!DISABLE_GROWTHBOOK && Au())
→ D6("tengu_sepia_moth", false) returns false (default, GrowthBook unreachable)
→ _Y8() = false
→ SC_() = false (auto-compact trigger check)
→ Auto-compact NEVER fires ❌
```
### Key functions extracted from v2.1.163 binary:
```js
// Anthropic first-party auth check
function Z7() { return XA() === "firstParty"; }
// GrowthBook availability - now gated on Anthropic auth
function dGL() {
if (eH(process.env.CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST)) return false;
return !Z7(); // true when NOT first-party → blocks GrowthBook
}
// GrowthBook connectivity
function LC() { return dGL() || H3() !== null || wM6(); }
function Au() { return !LC(); }
function qu() { return !T6.DISABLE_GROWTHBOOK && Au(); }
// Feature flag resolver - returns default when GrowthBook unavailable
function D6(H, _) {
let A = Hj6(); // local config override
if (A && H in A) return A[H];
let q = _j6(); // another config
if (q && H in q) return q[H];
if (!qu()) return _; // ← BLOCKED HERE: returns default (false)
// ... GrowthBook lookup (never reached)
}
// Auto-compact gate
function _Y8() {
if (!AT()) return false;
if (!Os()) return false;
if (vl()) return false;
return D6("tengu_sepia_moth", false); // false = auto-compact disabled
}
// Should auto-compact trigger?
function SC_(H) {
if (H.compactionResult !== undefined) return false;
if (H.isPreFirstCompactFork) return false;
if (H.consecutiveFailures !== undefined) return false;
if (H.hasAttemptedReactiveCompact) return false;
if (H.lastTransitionReason === "precomputed_compact_swap") return false;
if (!_Y8()) return false; // ← always false for 3rd party
return ix9(H.contextTokens, H.model, H.autoCompactWindow); // ← never reached
}
```
### What Should Happen?
Auto-compact should trigger automatically when context grows past 85% of the model's context window, regardless of whether the user is authenticated with Anthropic directly or using a third-party API provider.
For third-party API users, auto-compact should work identically to how it worked in v2.1.150 and earlier — triggering at ~153k tokens for a 200k context window model.
### Error Messages/Logs
```shell
## Evidence from Session Transcripts
Analyzed 500+ session transcripts. The `compact_boundary` system events with `compactMetadata` clearly show the regression:
### All sessions with auto-compact (chronological):
v2.1.117 (May 7-14): auto=38, manual=12 ✅ Working
v2.1.140 (May 14-24): auto=89, manual=18 ✅ Working
v2.1.150 (May 24-Jun 3): auto=55, manual=14 ✅ Working
v2.1.161 (Jun 3): auto=0, manual=3 ❌ BROKEN
v2.1.162 (Jun 4-5): auto=0, manual=1 ❌ BROKEN
### Example: 20MB session (v2.1.140, May 22-23) — auto-compact worked:
14 auto-compactions, all `trigger: "auto"`, each with full metadata:
{"trigger": "auto", "preTokens": 153018, "postTokens": 9231, "durationMs": 57363}
{"trigger": "auto", "preTokens": 153032, "postTokens": 9150, "durationMs": 45430}
// ... 12 more, all auto-triggered at ~153k tokens
### Example: Recent sessions (v2.1.162, June 4) — auto-compact broken:
Session 60faa9d3: auto=0, manual=2, hard_limit_hits=5
→ User had to manually /compact, or hit "ran out of context" emergency recovery
Session 4daf00de: auto=0, manual=1, hard_limit_hits=1
→ Same pattern
After upgrade to v2.1.161+: zero auto-compacts. Sessions grow until API returns an error, then emergency recovery kicks in.
```
### Steps to Reproduce
1. Configure Claude Code to use a third-party Anthropic-compatible API:
```json
// .claude/settings.json
{
"env": {
"ANTHROPIC_BASE_URL": "https://open.bigmodel.cn/api/anthropic",
"ANTHROPIC_AUTH_TOKEN": "<third-party-token>",
"ANTHROPIC_MODEL": "glm-5.1"
}
}
```
2. Start a Claude Code session and work long enough for context to grow past 85% of the model's context window
3. **Expected**: Auto-compact triggers automatically, context is summarized
4. **Actual**: Context grows until it hits the API's hard limit → emergency "ran out of context" crash recovery
**Note**: This reproduces with ANY third-party API provider, not just Zhipu AI. Azure Bedrock, Google Vertex, AWS Foundry, Mantle, and any custom `ANTHROPIC_BASE_URL` are all affected.
### Claude Model
Other
### Is this a regression?
Yes, this worked in a previous version
### Last Working Version
2.1.150
### Claude Code Version
2.1.163
### Platform
Other
### Operating System
macOS
### Terminal/Shell
Terminal.app (macOS)
### Additional Information
## Impact
This affects **all users** of third-party Anthropic-compatible API providers:
- Zhipu AI (智谱, open.bigmodel.cn)
- Azure Bedrock (via `CLAUDE_CODE_USE_BEDROCK`)
- Google Vertex (via `CLAUDE_CODE_USE_VERTEX`)
- AWS Foundry (via `CLAUDE_CODE_USE_FOUNDRY`)
- Mantle (via `CLAUDE_CODE_USE_MANTLE`)
- Any custom `ANTHROPIC_BASE_URL`
For these users, every long session will eventually crash with "ran out of context" instead of gracefully auto-compacting.
## Workaround
**Option 1: Downgrade to v2.1.150** (verified working)
**Option 2: Manual `/compact`** — remember to type `/compact` periodically during long sessions.
## Environment Details
- **API provider**: Zhipu AI (`open.bigmodel.cn/api/anthropic`)
- **Model**: glm-5.1 (200k context window)
- **OS**: macOS 12.7.6 (Monterey), x86_64
- **Relevant config**:
- `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=85`
- `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1`
- `ANTHROPIC_SMALL_FAST_MODEL=glm-5.1`
## Related Issues
- #46416 — Context window detection fails for third-party providers (same class of issue)
- #58383 — `DISABLE_TELEMETRY` silently disables GrowthBook, which also disables feature flags (same flag-defaults-to-false mechanism, different trigger)
- #58284 — Agent View hard-disabled for third-party backends via first-party-auth gating (identical pattern, different feature)
- #6012 — Auto-compact not triggering with AWS Bedrock backend (older report, same symptom)
- #63197 — Compaction regression in v2.1.153 on Zhipu (likely same root cause, attributed to context window misreporting)
## Suggested Fixes
1. **Remove the `Z7()` first-party check from the GrowthBook availability chain** for core functionality flags like `tengu_sepia_moth`. GrowthBook should be accessible to all users regardless of authentication method.
2. **Change the default value of `tengu_sepia_moth` from `false` to `true`** — auto-compact is a core feature that should be enabled by default, not a premium feature locked behind Anthropic auth.
3. **Add a `CLAUDE_CODE_ENABLE_AUTO_COMPACT` environment variable override** that bypasses the feature flag entirely, giving third-party users explicit control:
```js
function _Y8() {
if (eH(process.env.CLAUDE_CODE_ENABLE_AUTO_COMPACT)) return true; // explicit override
if (!AT()) return false;
if (!Os()) return false;
if (vl()) return false;
return D6("tengu_sepia_moth", true); // default true instead of false
}
```
Any of these would fix the regression. Option 2 is the simplest and safest.