vercel/ai · error
The Anthropic model function cannot be called with the new…
Error message
The Anthropic model function cannot be called with the new keyword.
What it means
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.
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.
Example fix
// before
const model = new anthropic('claude-sonnet-4-20250514');
// after
const model = anthropic('claude-sonnet-4-20250514'); Defensive patterns
Strategy: type-guard
Type guard
function isProviderFunction(x: unknown): x is (modelId: string) => unknown {
return typeof x === 'function';
}
// call only after narrowing; never with `new`
if (isProviderFunction(anthropic)) {
const model = anthropic('claude-sonnet-4-20250514');
} Try / catch
try {
const model = anthropic(modelId);
} catch (error) {
if (error instanceof Error && error.message.includes('new keyword')) {
// remove `new` at the call site
}
throw error;
} Prevention
- 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.
When it happens
Trigger: Writing `new anthropic('claude-sonnet-4-20250514')` or `new provider(modelId)` — any `new` invocation of the Anthropic provider function.
Common situations: 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.
Related errors
- The Google Vertex AI model function cannot be called with…
- Both apiKey and authToken were provided. Please use only…
- Could not find tool approval request
- 'element streams in enum mode' functionality not supported.
- 'element streams in no-schema mode' functionality not…
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/811157ef6f52d9aa.
Report an issue: GitHub.
Appendix: source
Thrown at packages/anthropic/src/anthropic-provider.ts:181
fetch: options.fetch,
generateId: options.generateId ?? generateId,
supportedUrls: () => ({
'image/*': [/^https?:\/\/.*$/],
'application/pdf': [/^https?:\/\/.*$/],
}),
});
const createSkills = () =>
new AnthropicSkills({
provider: `${providerName.replace('.messages', '')}.skills`,
baseURL,
headers: getHeaders,
fetch: options.fetch,
});
const provider = function (modelId: AnthropicModelId) {
if (new.target) {
throw new Error(
'The Anthropic model function cannot be called with the new keyword.',
);
}
return createChatModel(modelId);
};
provider.specificationVersion = 'v4' as const;
provider.languageModel = createChatModel;
provider.chat = createChatModel;
provider.messages = createChatModel;
provider.embeddingModel = (modelId: string) => {
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
};
provider.textEmbeddingModel = provider.embeddingModel;
provider.imageModel = (modelId: string) => {
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });View on GitHub (pinned to 69428b1f8b)