Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor: parse Claude model ids for runtime checks (#15601)

YuTengjingcb769534d3bb
diff --git a/packages/model-runtime/src/const/models.ts b/packages/model-runtime/src/const/models.ts
index 79b404acdec..cebbc419878 100644
--- a/packages/model-runtime/src/const/models.ts
+++ b/packages/model-runtime/src/const/models.ts
@@ -56,95 +56,3 @@ export const responsesAPIModels = new Set([
export const isGPT5ProResponsesModel = (model: string): boolean =>
/(?:^|\/)gpt-5(?:\.\d+)?-pro(?:-|$)/.test(model);
-
-/**
- * Regex patterns for models that support context caching (3.5+)
- */
-export const contextCachingModelPatterns: RegExp[] = [
- // Claude 4.5 series - Anthropic API
- /^claude-(opus|sonnet|haiku)-4-5-/,
- // Claude 4 series - Anthropic API
- /^claude-(opus|sonnet)-4-/,
- // Claude 3.7 - Anthropic API
- /^claude-3-7-sonnet-/,
- // Claude 3.5 series - Anthropic API
- /^claude-3-5-(sonnet|haiku)-/,
- // OpenRouter format (3.5+)
- /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4|3\.7|3\.5)/,
- /^anthropic\/claude-(4\.5|4|3\.7|3\.5)-(opus|sonnet|haiku)/,
- // AWS Bedrock format: [region.]anthropic.claude-xxx
- /anthropic\.claude-(opus|sonnet|haiku)-(4-5|4|3-7|3-5)-/,
-];
-
-export const isContextCachingModel = (model: string): boolean => {
- return contextCachingModelPatterns.some((pattern) => pattern.test(model));
-};
-
-/**
- * Regex patterns for Claude models that support thinking with tools (3.7+)
- */
-export const thinkingWithToolClaudeModelPatterns: RegExp[] = [
- // Claude 4.5 series - Anthropic API
- /^claude-(opus|sonnet|haiku)-4-5-/,
- // Claude 4 series - Anthropic API
- /^claude-(opus|sonnet)-4-/,
- // Claude 3.7 - Anthropic API
- /^claude-3-7-sonnet-/,
- // OpenRouter format (3.7+)
- /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4|3\.7)/,
- /^anthropic\/claude-(4\.5|4|3\.7)-(opus|sonnet|haiku)/,
- // AWS Bedrock format: [region.]anthropic.claude-xxx
- /anthropic\.claude-(opus|sonnet|haiku)-(4-5|4|3-7)-/,
-];
-
-export const isThinkingWithToolClaudeModel = (model: string): boolean => {
- return thinkingWithToolClaudeModelPatterns.some((pattern) => pattern.test(model));
-};
-
-/**
- * Regex patterns for Claude 4+ models that have temperature/top_p parameter conflict
- * (cannot set both temperature and top_p at the same time)
- */
-export const temperatureTopPConflictModelPatterns: RegExp[] = [
- // Claude 4+ series - Anthropic API (4, 4.1, 4.5)
- /^claude-(opus|sonnet|haiku)-4/,
- // OpenRouter format
- /^anthropic\/claude-(opus|sonnet|haiku)-(4\.5|4\.1|4)/,
- /^anthropic\/claude-(4\.5|4\.1|4)-(opus|sonnet|haiku)/,
- // AWS Bedrock format: [region.]anthropic.claude-xxx
- /anthropic\.claude-(opus|sonnet|haiku)-4/,
-];
-
-export const hasTemperatureTopPConflict = (model: string): boolean => {
- return temperatureTopPConflictModelPatterns.some((pattern) => pattern.test(model));
-};
-
-/**
- * Regex patterns for models that reject any non-default temperature / top_p / top_k.
- * These sampling parameters must be omitted from the request payload entirely;
- * otherwise the API returns a 400 error.
- *
- * Ref: https://platform.claude.com/docs/en/about-claude/models/migration-guide
- */
-export const omitSamplingParamsModelPatterns: RegExp[] = [
- // Claude Opus 4.7+ - Anthropic API (also LobeHub provider pass-through)
- /^claude-opus-4-[78]/,
- // OpenRouter formats use dot notation (e.g. anthropic/claude-opus-4.7)
- /^anthropic\/claude-opus-4\.[78]/,
- /^anthropic\/claude-4\.[78]-opus/,
- // AWS Bedrock formats (e.g. anthropic.claude-opus-4-7, us.anthropic.claude-opus-4-7-v1)
- /anthropic\.claude-opus-4-[78]/,
-];
-
-export const shouldOmitSamplingParams = (model: string): boolean => {
- return omitSamplingParamsModelPatterns.some((pattern) => pattern.test(model));
-};
-
-export const assistantPrefillUnsupportedModelPatterns: RegExp[] = [
- /^claude-(opus|sonnet)-4-[678](\b|-)/,
- /anthropic\.claude-(opus|sonnet)-4-[678](\b|-)/,
-];
-
-export const shouldDropUnsupportedClaudeAssistantPrefill = (model: string): boolean => {
- return assistantPrefillUnsupportedModelPatterns.some((pattern) => pattern.test(model));
-};
diff --git a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
index 93797d04f8a..4ce2dd566b4 100644
--- a/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
+++ b/packages/model-runtime/src/core/anthropicCompatibleFactory/index.ts
@@ -5,7 +5,6 @@ import type { ChatModelCard } from '@lobechat/types';
import debug from 'debug';
import type { Pricing } from 'model-bank';
-import { shouldDropUnsupportedClaudeAssistantPrefill } from '../../const/models';
import { ErrorClassifier } from '../../errors';
import type {
ChatCompletionErrorPayload,
@@ -17,6 +16,7 @@ import type {
} from '../../types';
import type { ILobeAgentRuntimeErrorType } from '../../types/error';
import { AgentRuntimeErrorType } from '../../types/error';
+import { shouldDropUnsupportedClaudeAssistantPrefill } from '../../utils/claudeModelId';
import { AgentRuntimeError } from '../../utils/createError';
import { debugStream } from '../../utils/debugStream';
import { desensitizeUrl } from '../../utils/desensitizeUrl';
diff --git a/packages/model-runtime/src/core/parameterResolver.test.ts b/packages/model-runtime/src/core/parameterResolver.test.ts
index 09934b40fbc..aa3fe48ccce 100644
--- a/packages/model-runtime/src/core/parameterResolver.test.ts
+++ b/packages/model-runtime/src/core/parameterResolver.test.ts
@@ -1,10 +1,5 @@
import { describe, expect, it } from 'vitest';
-import {
- hasTemperatureTopPConflict,
- shouldDropUnsupportedClaudeAssistantPrefill,
- shouldOmitSamplingParams,
-} from '../const/models';
import {
createParameterResolver,
resolveModelSamplingParameters,
@@ -349,97 +344,3 @@ describe('createParameterResolver', () => {
expect(result).toEqual({ temperature: 0.01, top_p: 0.01 });
});
});
-
-describe('hasTemperatureTopPConflict', () => {
- describe('Anthropic Claude 4+ models', () => {
- it('should return true for Claude 4+ models', () => {
- expect(hasTemperatureTopPConflict('claude-opus-4-1-20250805')).toBe(true);
- expect(hasTemperatureTopPConflict('claude-sonnet-4-5-20250929')).toBe(true);
- expect(hasTemperatureTopPConflict('claude-haiku-4-5-20251001')).toBe(true);
- });
-
- it('should return false for Claude 3.x models', () => {
- expect(hasTemperatureTopPConflict('claude-3-opus-20240229')).toBe(false);
- expect(hasTemperatureTopPConflict('claude-3-5-sonnet-20240620')).toBe(false);
- });
- });
-
- describe('OpenRouter Claude 4+ models', () => {
- it('should return true for OpenRouter Claude 4+ models', () => {
- expect(hasTemperatureTopPConflict('anthropic/claude-opus-4.5')).toBe(true);
- expect(hasTemperatureTopPConflict('anthropic/claude-sonnet-4.1')).toBe(true);
- expect(hasTemperatureTopPConflict('anthropic/claude-4.5-opus')).toBe(true);
- });
-
- it('should return false for OpenRouter Claude 3.x models', () => {
- expect(hasTemperatureTopPConflict('anthropic/claude-3.5-sonnet')).toBe(false);
- expect(hasTemperatureTopPConflict('anthropic/claude-3.7-sonnet')).toBe(false);
- });
- });
-
- describe('Bedrock Claude 4+ models', () => {
- it('should return true for Bedrock Claude 4+ models', () => {
- expect(hasTemperatureTopPConflict('anthropic.claude-opus-4-1-20250805-v1:0')).toBe(true);
- expect(hasTemperatureTopPConflict('us.anthropic.claude-sonnet-4-5-20250929-v1:0')).toBe(true);
- });
-
- it('should return false for Bedrock Claude 3.x models', () => {
- expect(hasTemperatureTopPConflict('anthropic.claude-3-5-sonnet-20240620-v1:0')).toBe(false);
- });
- });
-});
-
-describe('shouldOmitSamplingParams', () => {
- it('should return true for Claude Opus 4.7 (Anthropic API id)', () => {
- expect(shouldOmitSamplingParams('claude-opus-4-7')).toBe(true);
- });
-
- it('should return true for Claude Opus 4.8 (Anthropic API id)', () => {
- expect(shouldOmitSamplingParams('claude-opus-4-8')).toBe(true);
- });
-
- it('should return true for Claude Opus 4.7 on Bedrock (with and without region prefix)', () => {
- expect(shouldOmitSamplingParams('anthropic.claude-opus-4-7')).toBe(true);
- expect(shouldOmitSamplingParams('us.anthropic.claude-opus-4-7-v1')).toBe(true);
- });
-
- it('should return true for Claude Opus 4.8 on Bedrock (with and without region prefix)', () => {
- expect(shouldOmitSamplingParams('anthropic.claude-opus-4-8')).toBe(true);
- expect(shouldOmitSamplingParams('us.anthropic.claude-opus-4-8')).toBe(true);
- });
-
- it('should return true for Claude Opus 4.7 on OpenRouter (dot notation)', () => {
- expect(shouldOmitSamplingParams('anthropic/claude-opus-4.7')).toBe(true);
- expect(shouldOmitSamplingParams('anthropic/claude-4.7-opus')).toBe(true);
- });
-
- it('should return true for Claude Opus 4.8 on OpenRouter (dot notation)', () => {
- expect(shouldOmitSamplingParams('anthropic/claude-opus-4.8')).toBe(true);
- expect(shouldOmitSamplingParams('anthropic/claude-4.8-opus')).toBe(true);
- });
-
- it('should return false for hypothetical dash-form OpenRouter id', () => {
- // OpenRouter uses dot notation; dash form is not a real id and must not match
- // to avoid accidentally stripping params from unrelated future model ids.
- expect(shouldOmitSamplingParams('anthropic/claude-opus-4-7')).toBe(false);
- });
-
- it('should return false for Claude Opus 4.6 and earlier', () => {
- expect(shouldOmitSamplingParams('claude-opus-4-6')).toBe(false);
- expect(shouldOmitSamplingParams('claude-sonnet-4-5-20250929')).toBe(false);
- expect(shouldOmitSamplingParams('anthropic.claude-opus-4-6-v1')).toBe(false);
- });
-
- it('should return false for non-Claude models', () => {
- expect(shouldOmitSamplingParams('gpt-4o')).toBe(false);
- expect(shouldOmitSamplingParams('gemini-2.5-pro')).toBe(false);
- });
-});
-
-describe('shouldDropUnsupportedClaudeAssistantPrefill', () => {
- it('should return true for Claude Opus 4.8 API and Bedrock ids', () => {
- expect(shouldDropUnsupportedClaudeAssistantPrefill('claude-opus-4-8')).toBe(true);
- expect(shouldDropUnsupportedClaudeAssistantPrefill('anthropic.claude-opus-4-8')).toBe(true);
- expect(shouldDropUnsupportedClaudeAssistantPrefill('us.anthropic.claude-opus-4-8')).toBe(true);
- });
-});
diff --git a/packages/model-runtime/src/core/parameterResolver.ts b/packages/model-runtime/src/core/parameterResolver.ts
index 40c37848299..4483a9aeba5 100644
--- a/packages/model-runtime/src/core/parameterResolver.ts
+++ b/packages/model-runtime/src/core/parameterResolver.ts
@@ -1,4 +1,4 @@
-import { hasTemperatureTopPConflict, shouldOmitSamplingParams } from '../const/models';
+import { hasTemperatureTopPConflict, shouldOmitSamplingParams } from '../utils/claudeModelId';
/**
* Chat completion parameter configuration
diff --git a/packages/model-runtime/src/index.ts b/packages/model-runtime/src/index.ts
index 8947a6d9806..d4a79ba20a9 100644
--- a/packages/model-runtime/src/index.ts
+++ b/packages/model-runtime/src/index.ts
@@ -73,6 +73,7 @@ export { LobeZeroOneAI } from './providers/zeroone';
export { LobeZhipuAI } from './providers/zhipu';
export * from './types';
export * from './types/error';
+export * from './utils/claudeModelId';
export { consumeStreamUntilDone } from './utils/consumeStream';
export { AgentRuntimeError } from './utils/createError';
export { getModelPropertyWithFallback } from './utils/getFallbackModelProperty';
diff --git a/packages/model-runtime/src/providers/anthropic/claudeThinkingHistory.ts b/packages/model-runtime/src/providers/anthropic/claudeThinkingHistory.ts
index 7cfefcf9fb0..a1a9eabb0ce 100644
--- a/packages/model-runtime/src/providers/anthropic/claudeThinkingHistory.ts
+++ b/packages/model-runtime/src/providers/anthropic/claudeThinkingHistory.ts
@@ -1,5 +1,5 @@
-import { isThinkingWithToolClaudeModel } from '../../const/models';
import type { ChatStreamPayload, OpenAIChatMessage, UserMessageContentPart } from '../../types';
+import { isThinkingWithToolClaudeModel } from '../../utils/claudeModelId';
interface ClaudeThinkingHistoryMessageMetadata {
model?: null | string;
diff --git a/packages/model-runtime/src/providers/bedrock/index.ts b/packages/model-runtime/src/providers/bedrock/index.ts
index b90ce47ab7f..662d992adc4 100644
--- a/packages/model-runtime/src/providers/bedrock/index.ts
+++ b/packages/model-runtime/src/providers/bedrock/index.ts
@@ -6,7 +6,6 @@ import {
} from '@aws-sdk/client-bedrock-runtime';
import { ModelProvider } from 'model-bank';
-import { shouldDropUnsupportedClaudeAssistantPrefill } from '../../const/models';
import type { AnthropicGenerateObjectResponse } from '../../core/anthropicCompatibleFactory/generateObject';
import {
buildAnthropicGenerateObjectRequest,
@@ -34,6 +33,7 @@ import type {
GenerateObjectPayload,
} from '../../types';
import { AgentRuntimeErrorType } from '../../types/error';
+import { shouldDropUnsupportedClaudeAssistantPrefill } from '../../utils/claudeModelId';
import { AgentRuntimeError } from '../../utils/createError';
import { debugStream } from '../../utils/debugStream';
import { getModelPricing } from '../../utils/getModelPricing';
diff --git a/packages/model-runtime/src/utils/claudeModelId.test.ts b/packages/model-runtime/src/utils/claudeModelId.test.ts
new file mode 100644
index 00000000000..2cee4319a94
--- /dev/null
+++ b/packages/model-runtime/src/utils/claudeModelId.test.ts
@@ -0,0 +1,249 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ hasTemperatureTopPConflict,
+ isContextCachingModel,
+ isThinkingWithToolClaudeModel,
+ parseClaudeModelId,
+ shouldDropUnsupportedClaudeAssistantPrefill,
+ shouldOmitSamplingParams,
+} from './claudeModelId';
+
+describe('parseClaudeModelId', () => {
+ it('should parse native family-first Claude ids', () => {
+ expect(parseClaudeModelId('claude-opus-4-8')).toEqual({
+ family: 'opus',
+ majorVersion: 4,
+ minorSeparator: '-',
+ minorVersion: 8,
+ normalizedModelId: 'claude-opus-4-8',
+ source: 'anthropic',
+ });
+ });
+
+ it('should parse native version-first Claude ids', () => {
+ expect(parseClaudeModelId('claude-3-5-sonnet-20240620')).toEqual({
+ family: 'sonnet',
+ majorVersion: 3,
+ minorSeparator: '-',
+ minorVersion: 5,
+ normalizedModelId: 'claude-3-5-sonnet-20240620',
+ source: 'anthropic',
+ });
+ });
+
+ it('should parse OpenRouter Claude ids with dot minor versions', () => {
+ expect(parseClaudeModelId('anthropic/claude-4.7-opus')).toEqual({
+ family: 'opus',
+ majorVersion: 4,
+ minorSeparator: '.',
+ minorVersion: 7,
+ normalizedModelId: 'claude-4.7-opus',
+ source: 'openRouter',
+ });
+ });
+
+ it('should parse Bedrock Claude ids with region prefixes', () => {
+ expect(parseClaudeModelId('us.anthropic.claude-sonnet-4-5-20250929-v1:0')).toEqual({
+ family: 'sonnet',
+ majorVersion: 4,
+ minorSeparator: '-',
+ minorVersion: 5,
+ normalizedModelId: 'claude-sonnet-4-5-20250929-v1:0',
+ source: 'bedrock',
+ });
+ });
+
+ it('should not treat release dates as minor versions', () => {
+ expect(parseClaudeModelId('claude-opus-4-20250514')).toEqual({
+ family: 'opus',
+ majorVersion: 4,
+ normalizedModelId: 'claude-opus-4-20250514',
+ source: 'anthropic',
+ });
+ });
+
+ it('should parse Claude 5 family ids', () => {
+ expect(parseClaudeModelId('claude-fable-5')).toEqual({
+ family: 'fable',
+ majorVersion: 5,
+ normalizedModelId: 'claude-fable-5',
+ source: 'anthropic',
+ });
+
+ expect(parseClaudeModelId('claude-mythos-5-preview')).toEqual({
+ family: 'mythos',
+ majorVersion: 5,
+ normalizedModelId: 'claude-mythos-5-preview',
+ source: 'anthropic',
+ });
+ });
+
+ it('should return undefined for non-Claude ids', () => {
+ expect(parseClaudeModelId('gpt-5.4')).toBeUndefined();
+ });
+});
+
+describe('isContextCachingModel', () => {
+ it('should return true for current Claude context-caching families', () => {
+ expect(isContextCachingModel('claude-opus-4-20250514')).toBe(true);
+ expect(isContextCachingModel('claude-sonnet-4-5-20250929')).toBe(true);
+ expect(isContextCachingModel('claude-haiku-4-5-20251001')).toBe(true);
+ expect(isContextCachingModel('claude-3-7-sonnet-20250219')).toBe(true);
+ expect(isContextCachingModel('claude-3-5-haiku-20241022')).toBe(true);
+ });
+
+ it('should return true for Claude 5 ids', () => {
+ expect(isContextCachingModel('claude-fable-5')).toBe(true);
+ expect(isContextCachingModel('claude-mythos-5-preview')).toBe(true);
+ expect(isContextCachingModel('anthropic/claude-5-mythos')).toBe(true);
+ expect(isContextCachingModel('us.anthropic.claude-mythos-5-v1:0')).toBe(true);
+ });
+
+ it('should preserve existing false cases', () => {
+ expect(isContextCachingModel('claude-3-opus-20240229')).toBe(false);
+ expect(isContextCachingModel('gpt-4o')).toBe(false);
+ });
+});
+
+describe('isThinkingWithToolClaudeModel', () => {
+ it('should return true for current Claude thinking-with-tools families', () => {
+ expect(isThinkingWithToolClaudeModel('claude-opus-4-20250514')).toBe(true);
+ expect(isThinkingWithToolClaudeModel('claude-haiku-4-5-20251001')).toBe(true);
+ expect(isThinkingWithToolClaudeModel('claude-3-7-sonnet-20250219')).toBe(true);
+ });
+
+ it('should return true for Claude 5 ids', () => {
+ expect(isThinkingWithToolClaudeModel('claude-fable-5')).toBe(true);
+ expect(isThinkingWithToolClaudeModel('claude-mythos-5-preview')).toBe(true);
+ expect(isThinkingWithToolClaudeModel('anthropic/claude-5-mythos')).toBe(true);
+ expect(isThinkingWithToolClaudeModel('us.anthropic.claude-mythos-5-v1:0')).toBe(true);
+ });
+
+ it('should preserve existing false cases', () => {
+ expect(isThinkingWithToolClaudeModel('claude-3-5-sonnet-20240620')).toBe(false);
+ expect(isThinkingWithToolClaudeModel('gpt-4o')).toBe(false);
+ });
+});
+
+describe('hasTemperatureTopPConflict', () => {
+ describe('Anthropic Claude 4+ models', () => {
+ it('should return true for Claude 4+ models', () => {
+ expect(hasTemperatureTopPConflict('claude-opus-4-1-20250805')).toBe(true);
+ expect(hasTemperatureTopPConflict('claude-sonnet-4-5-20250929')).toBe(true);
+ expect(hasTemperatureTopPConflict('claude-haiku-4-5-20251001')).toBe(true);
+ });
+
+ it('should return false for Claude 3.x models', () => {
+ expect(hasTemperatureTopPConflict('claude-3-opus-20240229')).toBe(false);
+ expect(hasTemperatureTopPConflict('claude-3-5-sonnet-20240620')).toBe(false);
+ });
+ });
+
+ describe('OpenRouter Claude 4+ models', () => {
+ it('should return true for OpenRouter Claude 4+ models', () => {
+ expect(hasTemperatureTopPConflict('anthropic/claude-opus-4.5')).toBe(true);
+ expect(hasTemperatureTopPConflict('anthropic/claude-sonnet-4.1')).toBe(true);
+ expect(hasTemperatureTopPConflict('anthropic/claude-4.5-opus')).toBe(true);
+ });
+
+ it('should return false for OpenRouter Claude 3.x models', () => {
+ expect(hasTemperatureTopPConflict('anthropic/claude-3.5-sonnet')).toBe(false);
+ expect(hasTemperatureTopPConflict('anthropic/claude-3.7-sonnet')).toBe(false);
+ });
+ });
+
+ describe('Bedrock Claude 4+ models', () => {
+ it('should return true for Bedrock Claude 4+ models', () => {
+ expect(hasTemperatureTopPConflict('anthropic.claude-opus-4-1-20250805-v1:0')).toBe(true);
+ expect(hasTemperatureTopPConflict('us.anthropic.claude-sonnet-4-5-20250929-v1:0')).toBe(true);
+ });
+
+ it('should return false for Bedrock Claude 3.x models', () => {
+ expect(hasTemperatureTopPConflict('anthropic.claude-3-5-sonnet-20240620-v1:0')).toBe(false);
+ });
+ });
+
+ describe('Claude 5 models', () => {
+ it('should return true for Claude 5 ids across providers', () => {
+ expect(hasTemperatureTopPConflict('claude-fable-5')).toBe(true);
+ expect(hasTemperatureTopPConflict('claude-mythos-5-preview')).toBe(true);
+ expect(hasTemperatureTopPConflict('anthropic/claude-5-mythos')).toBe(true);
+ expect(hasTemperatureTopPConflict('us.anthropic.claude-mythos-5-v1:0')).toBe(true);
+ });
+ });
+});
+
+describe('shouldOmitSamplingParams', () => {
+ it('should return true for Claude Opus 4.7 (Anthropic API id)', () => {
+ expect(shouldOmitSamplingParams('claude-opus-4-7')).toBe(true);
+ });
+
+ it('should return true for Claude Opus 4.8 (Anthropic API id)', () => {
+ expect(shouldOmitSamplingParams('claude-opus-4-8')).toBe(true);
+ });
+
+ it('should return true for Claude Opus 4.7 on Bedrock (with and without region prefix)', () => {
+ expect(shouldOmitSamplingParams('anthropic.claude-opus-4-7')).toBe(true);
+ expect(shouldOmitSamplingParams('us.anthropic.claude-opus-4-7-v1')).toBe(true);
+ });
+
+ it('should return true for Claude Opus 4.8 on Bedrock (with and without region prefix)', () => {
+ expect(shouldOmitSamplingParams('anthropic.claude-opus-4-8')).toBe(true);
+ expect(shouldOmitSamplingParams('us.anthropic.claude-opus-4-8')).toBe(true);
+ });
+
+ it('should return true for Claude Opus 4.7 on OpenRouter (dot notation)', () => {
+ expect(shouldOmitSamplingParams('anthropic/claude-opus-4.7')).toBe(true);
+ expect(shouldOmitSamplingParams('anthropic/claude-4.7-opus')).toBe(true);
+ });
+
+ it('should return true for Claude Opus 4.8 on OpenRouter (dot notation)', () => {
+ expect(shouldOmitSamplingParams('anthropic/claude-opus-4.8')).toBe(true);
+ expect(shouldOmitSamplingParams('anthropic/claude-4.8-opus')).toBe(true);
+ });
+
+ it('should return false for hypothetical dash-form OpenRouter id', () => {
+ // OpenRouter uses dot notation; dash form is not a real id and must not match
+ // to avoid accidentally stripping params from unrelated future model ids.
+ expect(shouldOmitSamplingParams('anthropic/claude-opus-4-7')).toBe(false);
+ });
+
+ it('should return false for Claude Opus 4.6 and earlier', () => {
+ expect(shouldOmitSamplingParams('claude-opus-4-6')).toBe(false);
+ expect(shouldOmitSamplingParams('claude-sonnet-4-5-20250929')).toBe(false);
+ expect(shouldOmitSamplingParams('anthropic.claude-opus-4-6-v1')).toBe(false);
+ });
+
+ it('should return false for non-Claude models', () => {
+ expect(shouldOmitSamplingParams('gpt-4o')).toBe(false);
+ expect(shouldOmitSamplingParams('gemini-2.5-pro')).toBe(false);
+ });
+
+ it('should return true for Claude 5 ids', () => {
+ expect(shouldOmitSamplingParams('claude-fable-5')).toBe(true);
+ expect(shouldOmitSamplingParams('claude-mythos-5-preview')).toBe(true);
+ expect(shouldOmitSamplingParams('anthropic/claude-5-mythos')).toBe(true);
+ expect(shouldOmitSamplingParams('us.anthropic.claude-mythos-5-v1:0')).toBe(true);
+ });
+});
+
+describe('shouldDropUnsupportedClaudeAssistantPrefill', () => {
+ it('should return true for Claude Opus 4.8 API and Bedrock ids', () => {
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('claude-opus-4-8')).toBe(true);
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('anthropic.claude-opus-4-8')).toBe(true);
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('us.anthropic.claude-opus-4-8')).toBe(true);
+ });
+
+ it('should return true for Claude 5 API and Bedrock ids', () => {
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('claude-fable-5')).toBe(true);
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('claude-mythos-5-preview')).toBe(true);
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('us.anthropic.claude-mythos-5-v1:0')).toBe(
+ true,
+ );
+ });
+
+ it('should preserve existing OpenRouter behavior', () => {
+ expect(shouldDropUnsupportedClaudeAssistantPrefill('anthropic/claude-5-mythos')).toBe(false);
+ });
+});
diff --git a/packages/model-runtime/src/utils/claudeModelId.ts b/packages/model-runtime/src/utils/claudeModelId.ts
new file mode 100644
index 00000000000..429fea65a19
--- /dev/null
+++ b/packages/model-runtime/src/utils/claudeModelId.ts
@@ -0,0 +1,159 @@
+export type ClaudeModelIdSource = 'anthropic' | 'bedrock' | 'openRouter';
+export type ClaudeModelVersionSeparator = '-' | '.';
+
+export interface ParsedClaudeModelId {
+ family: string;
+ majorVersion: number;
+ minorSeparator?: ClaudeModelVersionSeparator;
+ minorVersion?: number;
+ normalizedModelId: string;
+ source: ClaudeModelIdSource;
+}
+
+interface ExtractedClaudeModelId {
+ normalizedModelId: string;
+ source: ClaudeModelIdSource;
+}
+
+const CLAUDE_FAMILY_FIRST_PATTERN = /^claude-([a-z][a-z0-9]*)-(\d+)(?:([-.])(\d+))?(?:\b|[-.:])/;
+const CLAUDE_VERSION_FIRST_PATTERN = /^claude-(\d+)(?:([-.])(\d+))?-([a-z][a-z0-9]*)(?:\b|[-.:])/;
+
+const extractClaudeModelId = (model: string): ExtractedClaudeModelId | undefined => {
+ const normalized = model.trim().toLowerCase();
+ if (!normalized) return;
+
+ if (normalized.startsWith('anthropic/')) {
+ return { normalizedModelId: normalized.slice('anthropic/'.length), source: 'openRouter' };
+ }
+
+ const bedrockPrefixIndex = normalized.lastIndexOf('anthropic.');
+ if (bedrockPrefixIndex >= 0) {
+ return {
+ normalizedModelId: normalized.slice(bedrockPrefixIndex + 'anthropic.'.length),
+ source: 'bedrock',
+ };
+ }
+
+ if (normalized.startsWith('claude-')) {
+ return { normalizedModelId: normalized, source: 'anthropic' };
+ }
+};
+
+const parseMinorVersion = (
+ value: string | undefined,
+ separator: string | undefined,
+): Pick<ParsedClaudeModelId, 'minorSeparator' | 'minorVersion'> => {
+ if (!value || !separator || !/^\d{1,2}$/.test(value)) return {};
+
+ return {
+ minorSeparator: separator as ClaudeModelVersionSeparator,
+ minorVersion: Number(value),
+ };
+};
+
+export const parseClaudeModelId = (model: string): ParsedClaudeModelId | undefined => {
+ const extracted = extractClaudeModelId(model);
+ if (!extracted) return;
+
+ const familyFirstMatch = CLAUDE_FAMILY_FIRST_PATTERN.exec(extracted.normalizedModelId);
+ if (familyFirstMatch) {
+ const [, family, majorVersion, minorSeparator, minorVersion] = familyFirstMatch;
+
+ return {
+ family,
+ majorVersion: Number(majorVersion),
+ normalizedModelId: extracted.normalizedModelId,
+ source: extracted.source,
+ ...parseMinorVersion(minorVersion, minorSeparator),
+ };
+ }
+
+ const versionFirstMatch = CLAUDE_VERSION_FIRST_PATTERN.exec(extracted.normalizedModelId);
+ if (versionFirstMatch) {
+ const [, majorVersion, minorSeparator, minorVersion, family] = versionFirstMatch;
+
+ return {
+ family,
+ majorVersion: Number(majorVersion),
+ normalizedModelId: extracted.normalizedModelId,
+ source: extracted.source,
+ ...parseMinorVersion(minorVersion, minorSeparator),
+ };
+ }
+};
+
+const hasMinorVersionAtLeast = (parsed: ParsedClaudeModelId, minorVersion: number): boolean =>
+ parsed.minorVersion !== undefined && parsed.minorVersion >= minorVersion;
+
+const isClaudeFamily = (parsed: ParsedClaudeModelId, families: readonly string[]): boolean =>
+ families.includes(parsed.family);
+
+export const isContextCachingModel = (model: string): boolean => {
+ const parsed = parseClaudeModelId(model);
+ if (!parsed) return false;
+
+ if (parsed.majorVersion >= 5) return true;
+
+ if (parsed.majorVersion === 4) {
+ return (
+ isClaudeFamily(parsed, ['opus', 'sonnet']) ||
+ (parsed.family === 'haiku' && hasMinorVersionAtLeast(parsed, 5))
+ );
+ }
+
+ if (parsed.majorVersion === 3) {
+ return (
+ (parsed.family === 'sonnet' && hasMinorVersionAtLeast(parsed, 7)) ||
+ (isClaudeFamily(parsed, ['sonnet', 'haiku']) && parsed.minorVersion === 5)
+ );
+ }
+
+ return false;
+};
+
+export const isThinkingWithToolClaudeModel = (model: string): boolean => {
+ const parsed = parseClaudeModelId(model);
+ if (!parsed) return false;
+
+ if (parsed.majorVersion >= 5) return true;
+
+ if (parsed.majorVersion === 4) {
+ return (
+ isClaudeFamily(parsed, ['opus', 'sonnet']) ||
+ (parsed.family === 'haiku' && hasMinorVersionAtLeast(parsed, 5))
+ );
+ }
+
+ return (
+ parsed.majorVersion === 3 && parsed.family === 'sonnet' && hasMinorVersionAtLeast(parsed, 7)
+ );
+};
+
+export const hasTemperatureTopPConflict = (model: string): boolean => {
+ const parsed = parseClaudeModelId(model);
+ return !!parsed && parsed.majorVersion >= 4;
+};
+
+export const shouldOmitSamplingParams = (model: string): boolean => {
+ const parsed = parseClaudeModelId(model);
+ if (!parsed) return false;
+ if (parsed.majorVersion >= 5) return true;
+ if (parsed.family !== 'opus' || parsed.majorVersion !== 4) return false;
+ if (parsed.minorVersion !== 7 && parsed.minorVersion !== 8) return false;
+
+ return parsed.source !== 'openRouter' || parsed.minorSeparator === '.';
+};
+
+export const shouldDropUnsupportedClaudeAssistantPrefill = (model: string): boolean => {
+ const parsed = parseClaudeModelId(model);
+ if (!parsed || parsed.source === 'openRouter') return false;
+ if (parsed.majorVersion >= 5) return true;
+
+ return (
+ parsed.majorVersion === 4 &&
+ isClaudeFamily(parsed, ['opus', 'sonnet']) &&
+ parsed.minorVersion !== undefined &&
+ parsed.minorVersion >= 6 &&
+ parsed.minorVersion <= 8
+ );
+};