{"record":{"id":"811157ef6f52d9aa","repo":"vercel/ai","slug":"the-anthropic-model-function-cannot-be-called-with","errorCode":null,"errorMessage":"The Anthropic model function cannot be called with the new keyword.","messagePattern":"The Anthropic model function cannot be called with the new keyword\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/anthropic/src/anthropic-provider.ts","lineNumber":181,"sourceCode":"      fetch: options.fetch,\n      generateId: options.generateId ?? generateId,\n      supportedUrls: () => ({\n        'image/*': [/^https?:\\/\\/.*$/],\n        'application/pdf': [/^https?:\\/\\/.*$/],\n      }),\n    });\n\n  const createSkills = () =>\n    new AnthropicSkills({\n      provider: `${providerName.replace('.messages', '')}.skills`,\n      baseURL,\n      headers: getHeaders,\n      fetch: options.fetch,\n    });\n\n  const provider = function (modelId: AnthropicModelId) {\n    if (new.target) {\n      throw new Error(\n        'The Anthropic model function cannot be called with the new keyword.',\n      );\n    }\n\n    return createChatModel(modelId);\n  };\n\n  provider.specificationVersion = 'v4' as const;\n  provider.languageModel = createChatModel;\n  provider.chat = createChatModel;\n  provider.messages = createChatModel;\n\n  provider.embeddingModel = (modelId: string) => {\n    throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n  };\n  provider.textEmbeddingModel = provider.embeddingModel;\n  provider.imageModel = (modelId: string) => {\n    throw new NoSuchModelError({ modelId, modelType: 'imageModel' });","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/anthropic/src/anthropic-provider.ts#L163-L199","documentation":"The provider object returned by createAnthropic (or the default `anthropic` export) is a plain function, not a class constructor. Calling it with `new` (e.g. `new anthropic('claude-...')`) throws this error via a `new.target` check. Model instances must be created by invoking the function normally.","triggerScenarios":"Writing `new anthropic('claude-sonnet-4-20250514')` or `new provider(modelId)` — any `new` invocation of the Anthropic provider function.","commonSituations":"Copy-pasting patterns from class-based OpenAI SDK clients (`new OpenAI()`); TypeScript users assuming the provider factory is a constructor; old migration examples using constructor-style instantiation.","solutions":["Remove the `new` keyword: call the provider function directly, e.g. `anthropic('claude-sonnet-4-20250514')`.","If a constructor-style API is desired, use `createAnthropic()` to build the provider, then call it as a plain function."],"exampleFix":"// before\nconst model = new anthropic('claude-sonnet-4-20250514');\n// after\nconst model = anthropic('claude-sonnet-4-20250514');","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isProviderFunction(x: unknown): x is (modelId: string) => unknown {\n  return typeof x === 'function';\n}\n// call only after narrowing; never with `new`\nif (isProviderFunction(anthropic)) {\n  const model = anthropic('claude-sonnet-4-20250514');\n}","tryCatchPattern":"try {\n  const model = anthropic(modelId);\n} catch (error) {\n  if (error instanceof Error && error.message.includes('new keyword')) {\n    // remove `new` at the call site\n  }\n  throw error;\n}","preventionTips":["Treat @ai-sdk providers as factory functions, never classes — do not use `new`.","Enable TypeScript strict mode; `new` on a non-constructor signature is flagged.","Grep code reviews for `new anthropic(` / `new provider(` patterns."],"tags":["api-misuse","javascript","provider-setup"],"backgroundTag":"constructor-called-as-class","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}