Skip to main content

Overview

The Anthropic provider gives you access to Claude models with advanced reasoning capabilities through adaptive and manual thinking modes.

Installation

npm install @core-ai/anthropic

createAnthropic()

Create an Anthropic provider instance.
import { createAnthropic } from '@core-ai/anthropic';

const anthropic = createAnthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  defaultMaxTokens: 4096,
});

Options

apiKey
string
Your Anthropic API key. Defaults to ANTHROPIC_API_KEY environment variable.
baseURL
string
Custom base URL for API requests.
defaultMaxTokens
number
default:"4096"
Default maximum tokens for completions. Can be overridden per request.
client
Anthropic
Provide your own configured Anthropic client instance.

Returns

AnthropicProvider with method chatModel().

Supported models

Latest generation with adaptive thinking mode.
  • claude-opus-4-6 - Most capable, supports max effort
  • claude-sonnet-4-6 - Balanced performance and speed
Previous generation with manual thinking budget control.
  • claude-opus-4-5 - High capability
  • claude-sonnet-4-5 - Efficient reasoning
  • claude-haiku-4-5 - Fast and lightweight
  • claude-opus-4-1 - Enhanced reasoning
  • claude-opus-4 - Strong performance
  • claude-sonnet-4 - Balanced model
  • claude-sonnet-3-7 - Previous generation

Capabilities

FeatureSupport
Chat CompletionYes
StreamingYes
Function CallingYes
VisionYes
ReasoningYes
Prompt CachingYes
EmbeddingsNo
Image GenerationNo

Examples

Basic chat

import { createAnthropic } from '@core-ai/anthropic';
import { generate } from '@core-ai/core-ai';

const anthropic = createAnthropic();

const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [
    { role: 'user', content: 'Explain the theory of relativity' },
  ],
});

console.log(result.content);

Extended thinking

const result = await generate({
  model: anthropic.chatModel('claude-opus-4-6'),
  messages: [
    { role: 'user', content: 'Analyze this complex dataset and provide insights...' },
  ],
  reasoning: {
    effort: 'max',
  },
  maxTokens: 8192,
});

if (result.reasoning) {
  console.log('Reasoning:', result.reasoning);
}
console.log('Answer:', result.content);

Streaming

import { stream } from '@core-ai/core-ai';

const chatStream = await stream({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [
    { role: 'user', content: 'Write a detailed analysis of...' },
  ],
  reasoning: {
    effort: 'high',
  },
});

for await (const event of chatStream) {
  if (event.type === 'reasoning-delta') {
    process.stdout.write(event.text);
  } else if (event.type === 'text-delta') {
    process.stdout.write(event.text);
  }
}

Tool calling

import { generate, defineTool } from '@core-ai/core-ai';
import { z } from 'zod';

const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [
    { role: 'user', content: 'What is the weather in San Francisco?' },
  ],
  tools: {
    getWeather: defineTool({
      name: 'getWeather',
      description: 'Get weather for a location',
      parameters: z.object({
        location: z.string(),
      }),
    }),
  },
});

Vision and file inputs

const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'What is in this image?' },
        {
          type: 'image',
          source: {
            type: 'url',
            url: 'https://example.com/image.jpg',
          },
        },
      ],
    },
  ],
});
Anthropic accepts image parts and PDF file parts. Other file MIME types are rejected.

Thinking modes

Adaptive thinking (Claude 4.6)

The model automatically determines thinking depth based on the effort level.
const result = await generate({
  model: anthropic.chatModel('claude-opus-4-6'),
  messages: [{ role: 'user', content: 'Complex problem...' }],
  reasoning: {
    effort: 'high',
  },
});
Only claude-opus-4-6 supports 'max' effort directly. On claude-sonnet-4-6, 'max' is clamped to 'high'.

Manual thinking budget (Claude 4.5 and earlier)

Token budgets: minimal -> 1,024, low -> 2,048, medium -> 8,192, high -> 32,768, max -> 65,536.
const result = await generate({
  model: anthropic.chatModel('claude-opus-4-5'),
  messages: [{ role: 'user', content: 'Complex reasoning task...' }],
  reasoning: {
    effort: 'high',
  },
});

Reasoning restrictions

When reasoning is enabled, Anthropic enforces additional request constraints:
  • temperature must be omitted
  • topP must be between 0.95 and 1
  • toolChoice must be 'auto' or 'none'
  • providerOptions.anthropic.topK must be omitted
Violating these constraints throws a ProviderError before the request is sent.
When you combine reasoning with tools, the adapter automatically adds the interleaved-thinking-2025-05-14 beta.

Reasoning metadata

When reasoning is enabled, Anthropic reasoning parts include provider metadata with signature and redacted data fields. Use getProviderMetadata to access them in a type-safe way.
import { generate, getProviderMetadata } from '@core-ai/core-ai';
import type { AnthropicReasoningMetadata } from '@core-ai/anthropic';

const result = await generate({
  model: anthropic.chatModel('claude-opus-4-6'),
  messages: [{ role: 'user', content: 'Think step by step.' }],
  reasoning: { effort: 'high' },
});

for (const part of result.parts) {
  if (part.type !== 'reasoning') continue;

  const metadata = getProviderMetadata<AnthropicReasoningMetadata>(
    part.providerMetadata,
    'anthropic'
  );

  console.log(metadata?.signature);
  console.log(metadata?.redactedData);
}
The AnthropicReasoningMetadata type contains:
  • signature — cryptographic signature for multi-turn reasoning fidelity
  • redactedData — redacted thinking content, if present

Provider-specific options

Options are namespaced under anthropic in providerOptions:
const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [{ role: 'user', content: 'Hello' }],
  providerOptions: {
    anthropic: {
      stopSequences: ['\n\n'],
      betas: ['interleaved-thinking-2025-05-14'],
    },
  },
});
Available fields: topK, stopSequences, betas, outputConfig, cacheControl.

Prompt caching

Use cacheControl to enable Anthropic prompt caching. This maps to Anthropic’s top-level cache_control request field and uses automatic caching for the reusable prompt prefix.
const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [
    { role: 'system', content: 'You are a careful technical editor.' },
    { role: 'user', content: 'Summarize this document and keep all key dates.' },
  ],
  providerOptions: {
    anthropic: {
      cacheControl: { type: 'ephemeral' },
    },
  },
});

console.log(result.usage.inputTokenDetails.cacheReadTokens);
console.log(result.usage.inputTokenDetails.cacheWriteTokens);
You can request a 1-hour cache TTL when you expect follow-up requests to arrive later than Anthropic’s default 5-minute cache window:
const result = await generate({
  model: anthropic.chatModel('claude-sonnet-4-6'),
  messages: [{ role: 'user', content: 'Summarize this large document.' }],
  providerOptions: {
    anthropic: {
      cacheControl: { type: 'ephemeral', ttl: '1h' },
    },
  },
});
Anthropic reports cache usage in result.usage.inputTokenDetails:
  • cacheReadTokens: Tokens served from an existing cache entry
  • cacheWriteTokens: Tokens written to the cache for future requests
Anthropic only caches prompts above the model’s minimum cacheable length:
  • 1024 tokens for Claude Sonnet 4.5, Claude Opus 4.1, Claude Opus 4, Claude Sonnet 4, and Claude Sonnet 3.7
  • 2048 tokens for Claude Sonnet 4.6
  • 4096 tokens for Claude Opus 4.6, Claude Opus 4.5, and Claude Haiku 4.5

Error handling

import { ProviderError } from '@core-ai/core-ai';

try {
  const result = await generate({
    model: anthropic.chatModel('claude-sonnet-4-6'),
    messages: [{ role: 'user', content: 'Hello!' }],
  });
} catch (error) {
  if (error instanceof ProviderError) {
    console.error('Anthropic API error:', error.message);
    console.error('Status:', error.statusCode);
  }
}

OpenAI Provider

GPT models with reasoning capabilities

Google GenAI Provider

Gemini models with multimodal support

Chat Completion Guide

Learn how to use chat completion