vercel/ai · error

The Anthropic model function cannot be called with the new k

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.

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)

Solutions

  1. Remove the `new` keyword: call the provider function directly, e.g. `anthropic('claude-sonnet-4-20250514')`.
  2. 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

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


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/811157ef6f52d9aa. Report an issue: GitHub.