所谓关于使用非Claude名模型在CC 2.1.150+ 版本无法自动压缩的问题<p>从<a href="https://linux.do/t/topic/2450770">CC接入gpt模型现在不自动压缩了</a>继续讨论:</p>
<p>有人问我说 这么个问题 说不压缩了 还给了个issue<br />
里面还有人又开始扯阴谋论说 故意恶心你的<br />
</p><div class="lightbox-wrapper"><a class="lightbox" href="https://cdn3.ldstatic.com/original/4X/5/9/5/595d78ed7b8618a3114f9b7b5d990405cd53a61a.png" rel="noopener nofollow ugc" title="image"><img alt="image" height="85" src="https://cdn3.ldstatic.com/optimized/4X/5/9/5/595d78ed7b8618a3114f9b7b5d990405cd53a61a_2_690x85.png" width="690" /></a></div><p></p>
<aside class="onebox githubissue">
<header class="source">
<a href="https://github.com/anthropics/claude-code/issues/65585" rel="noopener nofollow ugc" target="_blank">github.com/anthropics/claude-code</a>
</header>
<article class="onebox-body">
<div class="github-row">
<div class="github-icon-container" title="Issue">
<svg class="github-icon" height="60" viewBox="0 0 14 16" width="60" xmlns="http://www.w3.org/2000/svg"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z" fill-rule="evenodd"></path></svg>
</div>
<div class="github-info-container">
<h4>
<a href="https://github.com/anthropics/claude-code/issues/65585" rel="noopener nofollow ugc" target="_blank">Auto-compact stopped working for third-party API providers since v2.1.161</a>
</h4>
<div class="github-info">
<div class="date">
已打开 <span class="discourse-local-date">06:48AM - 05 Jun 26 UTC</span>
</div>
<div class="user">
<a href="https://github.com/deity-z" rel="noopener nofollow ugc" target="_blank">
<img alt="" class="onebox-avatar-inline" height="20" src="https://cdn3.ldstatic.com/original/4X/6/a/e/6ae22de8fd5e3dc4b2bf98f06feb6c744f58b5eb.png" width="20" />
deity-z
</a>
</div>
</div>
<div class="labels">
<span style="display: inline-block; margin-top: 2px; background-color: #B8B8B8; padding: 2px; border-radius: 4px; color: #fff; margin-left: 3px;">
bug
</span>
<span style="display: inline-block; margin-top: 2px; background-color: #B8B8B8; padding: 2px; border-radius: 4px; color: #fff; margin-left: 3px;">
has repro
</span>
<span style="display: inline-block; margin-top: 2px; background-color: #B8B8B8; padding: 2px; border-radius: 4px; color: #fff; margin-left: 3px;">
platform:macos
</span>
<span style="display: inline-block; margin-top: 2px; background-color: #B8B8B8; padding: 2px; border-radius: 4px; color: #fff; margin-left: 3px;">
area:core
</span>
<span style="display: inline-block; margin-top: 2px; background-color: #B8B8B8; padding: 2px; border-radius: 4px; color: #fff; margin-left: 3px;">
regression
</span>
</div>
</div>
</div>
<div class="github-row">
<p class="github-body-container">### Preflight Checklist
- [x] I have searched [existing issues](https://github.<span class="show-more-container"><a class="show-more" href="" rel="noopener">…</a></span><span class="excerpt hidden">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.</span></p>
</div>
</article>
<div class="onebox-metadata">
</div>
<div style="clear: both;"></div>
</aside>
<p>花了点时间看了看 比对149和185看了下<br />
自动压缩从 默认旧式的 87%计算式阈值时压缩 转变为了 新式的 由API来反馈所决定到没到上限<br />
要不要压缩</p>
<p>触发响应应是 <code>prompt_too_long</code> 错误, 这改动的目的本来是好事 也就是更多的用够上下文 要压了再压 而不是像以前一样 预留 13K 再加 Tool 占用 直接开压</p>
<p>第三方模型那肯定不是这样的文本 所以就噶了呗 再就没有后备手段了</p>
<p>所以呢 新版得这么加一个控制属性</p>
<pre><code class="lang-auto">"autoCompactWindow": 870000
</code></pre>
<p>等价 env 中 <code>CLAUDE_CODE_AUTO_COMPACT_WINDOW</code> (优先级最高)</p>
<p>1M 模型就这么写 85%压缩没差</p>
<p>诚然 非1M Claude模型 那肯定就是 fallback 到 200K 了</p>
<p>所以我们简单点的话只要 是超过 200K 的模型么 都路由成 opus[1m] 就ok了</p>
<p><small>27 个帖子 - 20 位参与者</small></p>
<p><a href="https://linux.do/t/topic/2452632">阅读完整话题</a></p>哈雷彗星