forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetas.ts
More file actions
434 lines (402 loc) · 15.3 KB
/
betas.ts
File metadata and controls
434 lines (402 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import { feature } from 'bun:bundle'
import memoize from 'lodash-es/memoize.js'
import {
checkStatsigFeatureGate_CACHED_MAY_BE_STALE,
getFeatureValue_CACHED_MAY_BE_STALE,
} from 'src/services/analytics/growthbook.js'
import { getIsNonInteractiveSession, getSdkBetas } from '../bootstrap/state.js'
import {
BEDROCK_EXTRA_PARAMS_HEADERS,
CLAUDE_CODE_20250219_BETA_HEADER,
CLI_INTERNAL_BETA_HEADER,
CONTEXT_1M_BETA_HEADER,
CONTEXT_MANAGEMENT_BETA_HEADER,
INTERLEAVED_THINKING_BETA_HEADER,
PROMPT_CACHING_SCOPE_BETA_HEADER,
REDACT_THINKING_BETA_HEADER,
STRUCTURED_OUTPUTS_BETA_HEADER,
SUMMARIZE_CONNECTOR_TEXT_BETA_HEADER,
TOKEN_EFFICIENT_TOOLS_BETA_HEADER,
TOOL_SEARCH_BETA_HEADER_1P,
TOOL_SEARCH_BETA_HEADER_3P,
WEB_SEARCH_BETA_HEADER,
} from '../constants/betas.js'
import { OAUTH_BETA_HEADER } from '../constants/oauth.js'
import { isClaudeAISubscriber } from './auth.js'
import { has1mContext } from './context.js'
import { isEnvDefinedFalsy, isEnvTruthy } from './envUtils.js'
import { getCanonicalName } from './model/model.js'
import { get3PModelCapabilityOverride } from './model/modelSupportOverrides.js'
import { getAPIProvider } from './model/providers.js'
import { getInitialSettings } from './settings/settings.js'
/**
* SDK-provided betas that are allowed for API key users.
* Only betas in this list can be passed via SDK options.
*/
const ALLOWED_SDK_BETAS = [CONTEXT_1M_BETA_HEADER]
/**
* Filter betas to only include those in the allowlist.
* Returns allowed and disallowed betas separately.
*/
function partitionBetasByAllowlist(betas: string[]): {
allowed: string[]
disallowed: string[]
} {
const allowed: string[] = []
const disallowed: string[] = []
for (const beta of betas) {
if (ALLOWED_SDK_BETAS.includes(beta)) {
allowed.push(beta)
} else {
disallowed.push(beta)
}
}
return { allowed, disallowed }
}
/**
* Filter SDK betas to only include allowed ones.
* Warns about disallowed betas and subscriber restrictions.
* Returns undefined if no valid betas remain or if user is a subscriber.
*/
export function filterAllowedSdkBetas(
sdkBetas: string[] | undefined,
): string[] | undefined {
if (!sdkBetas || sdkBetas.length === 0) {
return undefined
}
if (isClaudeAISubscriber()) {
// biome-ignore lint/suspicious/noConsole: intentional warning
console.warn(
'Warning: Custom betas are only available for API key users. Ignoring provided betas.',
)
return undefined
}
const { allowed, disallowed } = partitionBetasByAllowlist(sdkBetas)
for (const beta of disallowed) {
// biome-ignore lint/suspicious/noConsole: intentional warning
console.warn(
`Warning: Beta header '${beta}' is not allowed. Only the following betas are supported: ${ALLOWED_SDK_BETAS.join(', ')}`,
)
}
return allowed.length > 0 ? allowed : undefined
}
// Generally, foundry supports all 1P features;
// however out of an abundance of caution, we do not enable any which are behind an experiment
export function modelSupportsISP(model: string): boolean {
const supported3P = get3PModelCapabilityOverride(
model,
'interleaved_thinking',
)
if (supported3P !== undefined) {
return supported3P
}
const canonical = getCanonicalName(model)
const provider = getAPIProvider()
// Foundry supports interleaved thinking for all models
if (provider === 'foundry') {
return true
}
if (provider === 'firstParty') {
return !canonical.includes('claude-3-')
}
return (
canonical.includes('claude-opus-4') || canonical.includes('claude-sonnet-4')
)
}
function vertexModelSupportsWebSearch(model: string): boolean {
const canonical = getCanonicalName(model)
// Web search only supported on Claude 4.0+ models on Vertex
return (
canonical.includes('claude-opus-4') ||
canonical.includes('claude-sonnet-4') ||
canonical.includes('claude-haiku-4')
)
}
// Context management is supported on Claude 4+ models
export function modelSupportsContextManagement(model: string): boolean {
const canonical = getCanonicalName(model)
const provider = getAPIProvider()
if (provider === 'foundry') {
return true
}
if (provider === 'firstParty') {
return !canonical.includes('claude-3-')
}
return (
canonical.includes('claude-opus-4') ||
canonical.includes('claude-sonnet-4') ||
canonical.includes('claude-haiku-4')
)
}
// @[MODEL LAUNCH]: Add the new model ID to this list if it supports structured outputs.
export function modelSupportsStructuredOutputs(model: string): boolean {
const canonical = getCanonicalName(model)
const provider = getAPIProvider()
// Structured outputs only supported on firstParty and Foundry (not Bedrock/Vertex yet)
if (provider !== 'firstParty' && provider !== 'foundry') {
return false
}
return (
canonical.includes('claude-sonnet-4-6') ||
canonical.includes('claude-sonnet-4-5') ||
canonical.includes('claude-opus-4-1') ||
canonical.includes('claude-opus-4-5') ||
canonical.includes('claude-opus-4-6') ||
canonical.includes('claude-haiku-4-5')
)
}
// @[MODEL LAUNCH]: Add the new model if it supports auto mode (specifically PI probes) — ask in #proj-claude-code-safety-research.
export function modelSupportsAutoMode(model: string): boolean {
if (feature('TRANSCRIPT_CLASSIFIER')) {
const m = getCanonicalName(model)
// External: firstParty-only at launch (PI probes not wired for
// Bedrock/Vertex/Foundry yet). Checked before allowModels so the GB
// override can't enable auto mode on unsupported providers.
if (process.env.USER_TYPE !== 'ant' && getAPIProvider() !== 'firstParty') {
return false
}
// GrowthBook override: tengu_auto_mode_config.allowModels force-enables
// auto mode for listed models, bypassing the denylist/allowlist below.
// Exact model IDs (e.g. "claude-strudel-v6-p") match only that model;
// canonical names (e.g. "claude-strudel") match the whole family.
const config = getFeatureValue_CACHED_MAY_BE_STALE<{
allowModels?: string[]
}>('tengu_auto_mode_config', {})
const rawLower = model.toLowerCase()
if (
config?.allowModels?.some(
am => am.toLowerCase() === rawLower || am.toLowerCase() === m,
)
) {
return true
}
if (process.env.USER_TYPE === 'ant') {
// Denylist: block known-unsupported claude models, allow everything else (ant-internal models etc.)
if (m.includes('claude-3-')) return false
// claude-*-4 not followed by -[6-9]: blocks bare -4, -4-YYYYMMDD, -4@, -4-0 thru -4-5
if (/claude-(opus|sonnet|haiku)-4(?!-[6-9])/.test(m)) return false
return true
}
// External allowlist (firstParty already checked above).
return /^claude-(opus|sonnet)-4-6/.test(m)
}
return false
}
/**
* Get the correct tool search beta header for the current API provider.
* - Claude API / Foundry: advanced-tool-use-2025-11-20
* - Vertex AI / Bedrock: tool-search-tool-2025-10-19
*/
export function getToolSearchBetaHeader(): string {
const provider = getAPIProvider()
if (provider === 'vertex' || provider === 'bedrock') {
return TOOL_SEARCH_BETA_HEADER_3P
}
return TOOL_SEARCH_BETA_HEADER_1P
}
/**
* Check if experimental betas should be included.
* These are betas that are only available on firstParty provider
* and may not be supported by proxies or other providers.
*/
export function shouldIncludeFirstPartyOnlyBetas(): boolean {
return (
(getAPIProvider() === 'firstParty' || getAPIProvider() === 'foundry') &&
!isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS)
)
}
/**
* Global-scope prompt caching is firstParty only. Foundry is excluded because
* GrowthBook never bucketed Foundry users into the rollout experiment — the
* treatment data is firstParty-only.
*/
export function shouldUseGlobalCacheScope(): boolean {
return (
getAPIProvider() === 'firstParty' &&
!isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS)
)
}
export const getAllModelBetas = memoize((model: string): string[] => {
const betaHeaders = []
const isHaiku = getCanonicalName(model).includes('haiku')
const provider = getAPIProvider()
const includeFirstPartyOnlyBetas = shouldIncludeFirstPartyOnlyBetas()
if (!isHaiku) {
betaHeaders.push(CLAUDE_CODE_20250219_BETA_HEADER)
if (
process.env.USER_TYPE === 'ant' &&
process.env.CLAUDE_CODE_ENTRYPOINT === 'cli'
) {
if (CLI_INTERNAL_BETA_HEADER) {
betaHeaders.push(CLI_INTERNAL_BETA_HEADER)
}
}
}
if (isClaudeAISubscriber()) {
betaHeaders.push(OAUTH_BETA_HEADER)
}
if (has1mContext(model)) {
betaHeaders.push(CONTEXT_1M_BETA_HEADER)
}
if (
!isEnvTruthy(process.env.DISABLE_INTERLEAVED_THINKING) &&
modelSupportsISP(model)
) {
betaHeaders.push(INTERLEAVED_THINKING_BETA_HEADER)
}
// Skip the API-side Haiku thinking summarizer — the summary is only used
// for ctrl+o display, which interactive users rarely open. The API returns
// redacted_thinking blocks instead; AssistantRedactedThinkingMessage already
// renders those as a stub. SDK / print-mode keep summaries because callers
// may iterate over thinking content. Users can opt back in via settings.json
// showThinkingSummaries.
if (
includeFirstPartyOnlyBetas &&
modelSupportsISP(model) &&
!getIsNonInteractiveSession() &&
getInitialSettings().showThinkingSummaries !== true
) {
betaHeaders.push(REDACT_THINKING_BETA_HEADER)
}
// POC: server-side connector-text summarization (anti-distillation). The
// API buffers assistant text between tool calls, summarizes it, and returns
// the summary with a signature so the original can be restored on subsequent
// turns — same mechanism as thinking blocks. Ant-only while we measure
// TTFT/TTLT/capacity; betas already flow to tengu_api_success for splitting.
// Backend independently requires Capability.ANTHROPIC_INTERNAL_RESEARCH.
//
// USE_CONNECTOR_TEXT_SUMMARIZATION is tri-state: =1 forces on (opt-in even
// if GB is off), =0 forces off (opt-out of a GB rollout you were bucketed
// into), unset defers to GB.
if (
SUMMARIZE_CONNECTOR_TEXT_BETA_HEADER &&
process.env.USER_TYPE === 'ant' &&
includeFirstPartyOnlyBetas &&
!isEnvDefinedFalsy(process.env.USE_CONNECTOR_TEXT_SUMMARIZATION) &&
(isEnvTruthy(process.env.USE_CONNECTOR_TEXT_SUMMARIZATION) ||
getFeatureValue_CACHED_MAY_BE_STALE('tengu_slate_prism', false))
) {
betaHeaders.push(SUMMARIZE_CONNECTOR_TEXT_BETA_HEADER)
}
// Add context management beta for tool clearing (ant opt-in) or thinking preservation
const antOptedIntoToolClearing =
isEnvTruthy(process.env.USE_API_CONTEXT_MANAGEMENT) &&
process.env.USER_TYPE === 'ant'
const thinkingPreservationEnabled = modelSupportsContextManagement(model)
if (
shouldIncludeFirstPartyOnlyBetas() &&
(antOptedIntoToolClearing || thinkingPreservationEnabled)
) {
betaHeaders.push(CONTEXT_MANAGEMENT_BETA_HEADER)
}
// Add strict tool use beta if experiment is enabled.
// Gate on includeFirstPartyOnlyBetas: CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS
// already strips schema.strict from tool bodies at api.ts's choke point, but
// this header was escaping that kill switch. Proxy gateways that look like
// firstParty but forward to Vertex reject this header with 400.
// github.com/deshaw/anthropic-issues/issues/5
const strictToolsEnabled =
checkStatsigFeatureGate_CACHED_MAY_BE_STALE('tengu_tool_pear')
// 3P default: false. API rejects strict + token-efficient-tools together
// (tool_use.py:139), so these are mutually exclusive — strict wins.
const tokenEfficientToolsEnabled =
!strictToolsEnabled &&
getFeatureValue_CACHED_MAY_BE_STALE('tengu_amber_json_tools', false)
if (
includeFirstPartyOnlyBetas &&
modelSupportsStructuredOutputs(model) &&
strictToolsEnabled
) {
betaHeaders.push(STRUCTURED_OUTPUTS_BETA_HEADER)
}
// JSON tool_use format (FC v3) — ~4.5% output token reduction vs ANTML.
// Sends the v2 header (2026-03-28) added in anthropics/anthropic#337072 to
// isolate the CC A/B cohort from ~9.2M/week existing v1 senders. Ant-only
// while the restored JsonToolUseOutputParser soaks.
if (
process.env.USER_TYPE === 'ant' &&
includeFirstPartyOnlyBetas &&
tokenEfficientToolsEnabled
) {
betaHeaders.push(TOKEN_EFFICIENT_TOOLS_BETA_HEADER)
}
// Add web search beta for Vertex Claude 4.0+ models only
if (provider === 'vertex' && vertexModelSupportsWebSearch(model)) {
betaHeaders.push(WEB_SEARCH_BETA_HEADER)
}
// Foundry only ships models that already support Web Search
if (provider === 'foundry') {
betaHeaders.push(WEB_SEARCH_BETA_HEADER)
}
// Always send the beta header for 1P. The header is a no-op without a scope field.
if (includeFirstPartyOnlyBetas) {
betaHeaders.push(PROMPT_CACHING_SCOPE_BETA_HEADER)
}
// If ANTHROPIC_BETAS is set, split it by commas and add to betaHeaders.
// This is an explicit user opt-in, so honor it regardless of model.
if (process.env.ANTHROPIC_BETAS) {
betaHeaders.push(
...process.env.ANTHROPIC_BETAS.split(',')
.map(_ => _.trim())
.filter(Boolean),
)
}
return betaHeaders
})
export const getModelBetas = memoize((model: string): string[] => {
const modelBetas = getAllModelBetas(model)
if (getAPIProvider() === 'bedrock') {
return modelBetas.filter(b => !BEDROCK_EXTRA_PARAMS_HEADERS.has(b))
}
return modelBetas
})
export const getBedrockExtraBodyParamsBetas = memoize(
(model: string): string[] => {
const modelBetas = getAllModelBetas(model)
return modelBetas.filter(b => BEDROCK_EXTRA_PARAMS_HEADERS.has(b))
},
)
/**
* Merge SDK-provided betas with auto-detected model betas.
* SDK betas are read from global state (set via setSdkBetas in main.tsx).
* The betas are pre-filtered by filterAllowedSdkBetas which handles
* subscriber checks and allowlist validation with warnings.
*
* @param options.isAgenticQuery - When true, ensures the beta headers needed
* for agentic queries are present. For non-Haiku models these are already
* included by getAllModelBetas(); for Haiku they're excluded since
* non-agentic calls (compaction, classifiers, token estimation) don't need them.
*/
export function getMergedBetas(
model: string,
options?: { isAgenticQuery?: boolean },
): string[] {
const baseBetas = [...getModelBetas(model)]
// Agentic queries always need claude-code and cli-internal beta headers.
// For non-Haiku models these are already in baseBetas; for Haiku they're
// excluded by getAllModelBetas() since non-agentic Haiku calls don't need them.
if (options?.isAgenticQuery) {
if (!baseBetas.includes(CLAUDE_CODE_20250219_BETA_HEADER)) {
baseBetas.push(CLAUDE_CODE_20250219_BETA_HEADER)
}
if (
process.env.USER_TYPE === 'ant' &&
process.env.CLAUDE_CODE_ENTRYPOINT === 'cli' &&
CLI_INTERNAL_BETA_HEADER &&
!baseBetas.includes(CLI_INTERNAL_BETA_HEADER)
) {
baseBetas.push(CLI_INTERNAL_BETA_HEADER)
}
}
const sdkBetas = getSdkBetas()
if (!sdkBetas || sdkBetas.length === 0) {
return baseBetas
}
// Merge SDK betas without duplicates (already filtered by filterAllowedSdkBetas)
return [...baseBetas, ...sdkBetas.filter(b => !baseBetas.includes(b))]
}
export function clearBetasCaches(): void {
getAllModelBetas.cache?.clear?.()
getModelBetas.cache?.clear?.()
getBedrockExtraBodyParamsBetas.cache?.clear?.()
}