> ## Documentation Index
> Fetch the complete documentation index at: https://docs.core-ai.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Provider

> Create and configure the Anthropic provider for Claude models with extended thinking

## Overview

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

## Installation

```bash theme={null}
npm install @core-ai/anthropic
```

## createAnthropic()

Create an Anthropic provider instance.

```typescript theme={null}
import { createAnthropic } from '@core-ai/anthropic';

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

### Options

<ParamField path="apiKey" type="string" optional>
  Your Anthropic API key. Defaults to `ANTHROPIC_API_KEY` environment
  variable.
</ParamField>

<ParamField path="baseURL" type="string" optional>
  Custom base URL for API requests.
</ParamField>

<ParamField path="defaultMaxTokens" type="number" optional default="4096">
  Default maximum tokens for completions. Can be overridden per request.
</ParamField>

<ParamField path="client" type="Anthropic" optional>
  Provide your own configured Anthropic client instance.
</ParamField>

### Returns

`AnthropicProvider` with method `chatModel()`.

## Supported models

<AccordionGroup>
  <Accordion title="Claude Fable and Mythos (adaptive thinking)" icon="sparkles">
    Claude Fable and Mythos models use adaptive thinking mode.

    <ul>
      <li><strong>claude-fable-5</strong> - Widely available model for demanding reasoning and agentic work</li>
      <li><strong>claude-mythos-5</strong> - Limited-access model with the same capability handling</li>
      <li><strong>claude-mythos-preview</strong> - Limited-access preview model</li>
    </ul>
  </Accordion>

  <Accordion title="Claude Opus and Sonnet (adaptive thinking)" icon="brain">
    Current Opus and Sonnet models use effort-based adaptive thinking.

    <ul>
      <li><strong>claude-sonnet-5</strong> - Most agentic Sonnet model with near-Opus performance at lower cost</li>
      <li><strong>claude-opus-4-8</strong> - Opus model for complex reasoning and agentic coding</li>
      <li><strong>claude-opus-4-7</strong> - Opus model with max effort support</li>
      <li><strong>claude-opus-4-6</strong> - Opus model with max effort support</li>
      <li><strong>claude-sonnet-4-6</strong> - Balanced performance and speed with max effort support</li>
    </ul>
  </Accordion>

  <Accordion title="Claude 4.5 (manual thinking)" icon="brain">
    Models with manual thinking budget control.

    <ul>
      <li><strong>claude-opus-4-5</strong> - High capability</li>
      <li><strong>claude-sonnet-4-5</strong> - Efficient reasoning</li>
      <li><strong>claude-haiku-4-5</strong> - Fast and lightweight</li>
    </ul>
  </Accordion>

  <Accordion title="Claude 4 and earlier" icon="circle">
    <ul>
      <li><strong>claude-opus-4-1</strong> - Enhanced reasoning</li>
      <li><strong>claude-opus-4</strong> - Strong performance</li>
      <li><strong>claude-sonnet-4</strong> - Balanced model</li>
      <li><strong>claude-sonnet-3-7</strong> - Previous generation</li>
    </ul>
  </Accordion>
</AccordionGroup>

## Capabilities

| Feature          | Support |
| ---------------- | ------- |
| Chat Completion  | Yes     |
| Streaming        | Yes     |
| Function Calling | Yes     |
| Vision           | Yes     |
| Reasoning        | Yes     |
| Prompt Caching   | Yes     |
| Embeddings       | No      |
| Image Generation | No      |

Use `ChatModel.capabilities` or `getAnthropicModelCapabilities(modelId)` to inspect reasoning support at runtime:

```typescript theme={null}
import { clampReasoningEffort, generate } from '@core-ai/core-ai';
import {
    createAnthropic,
    getAnthropicModelCapabilities,
} from '@core-ai/anthropic';

const anthropic = createAnthropic();
const model = anthropic.chatModel('claude-opus-4-8');

if (model.capabilities.reasoning.supported) {
    const effort = clampReasoningEffort(
        'max',
        model.capabilities.reasoning.supportedEfforts
    );

    await generate({
        model,
        messages: [{ role: 'user', content: 'Analyze this carefully.' }],
        reasoning: { effort },
        // Anthropic sets restrictsSamplingParams: true — omit temperature/topP
    });
}

const capabilities = getAnthropicModelCapabilities('claude-opus-4-5');
```

When `restrictsSamplingParams` is `true`, do not set `temperature` (and keep `topP` in the allowed range) while reasoning is enabled.

## Examples

### Basic chat

```typescript theme={null}
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

```typescript theme={null}
const result = await generate({
    model: anthropic.chatModel('claude-opus-4-8'),
    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);
```

The provider requests summarized thinking, matching the OpenAI provider's
reasoning-summary behavior. Every reasoning part also retains Anthropic's
signature so you can safely pass the result into a later tool-result request.

### Streaming

```typescript theme={null}
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

```typescript theme={null}
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

```typescript theme={null}
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',
                    },
                },
            ],
        },
    ],
});
```

<Note>
  Anthropic accepts image parts and PDF file parts. Other file MIME types are
  rejected.
</Note>

## Thinking modes

### Adaptive thinking

The model automatically determines thinking depth based on the effort level.

```typescript theme={null}
const result = await generate({
    model: anthropic.chatModel('claude-opus-4-8'),
    messages: [{ role: 'user', content: 'Complex problem...' }],
    reasoning: {
        effort: 'high',
    },
});
```

<Note>
  `claude-fable-5`, `claude-mythos-5`, `claude-mythos-preview`,
  `claude-opus-4-8`, `claude-opus-4-7`, `claude-opus-4-6`, `claude-sonnet-5`,
  and `claude-sonnet-4-6` support `'max'` effort directly.
</Note>

### 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.
The adapter caps the selected budget at `maxTokens - 1` because Anthropic
requires the thinking budget to be lower than the total output limit.
`maxTokens` must be greater than 1,024.

```typescript theme={null}
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

Claude Fable 5, Mythos 5, Mythos Preview, Opus 4.8, Opus 4.7, and Sonnet 5
also reject non-default sampling parameters when you do not explicitly enable
reasoning. For these models, use `temperature: 1`, `topP: 1`, and omit `topK`.

<Warning>
  Violating these constraints throws a `ValidationError` before the request is
  sent.
</Warning>

<Note>
  Adaptive thinking automatically supports reasoning between tool calls. For
  older compatible manual-thinking models, the adapter sends the
  `interleaved-thinking-2025-05-14` beta as an `anthropic-beta` header.
</Note>

### 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.

```typescript theme={null}
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-8'),
    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`:

```typescript theme={null}
const result = await generate({
    model: anthropic.chatModel('claude-sonnet-5'),
    messages: [{ role: 'user', content: 'Hello' }],
    providerOptions: {
        anthropic: {
            stopSequences: ['\n\n'],
        },
    },
});
```

Available fields: `topK`, `stopSequences`, `betas`, `outputConfig`,
and `cacheControl`.

* `betas` are sent through the `anthropic-beta` request header.

## 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.

```typescript theme={null}
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:

```typescript theme={null}
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 Opus 4.8
* 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

```typescript theme={null}
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);
    }
}
```

## Related

<CardGroup cols={2}>
  <Card title="OpenAI Provider" icon="robot" href="/api/providers/openai">
    GPT models with reasoning capabilities
  </Card>

  <Card title="Google GenAI Provider" icon="sparkles" href="/api/providers/google-genai">
    Gemini models with multimodal support
  </Card>

  <Card title="Chat Completion Guide" icon="message" href="/guides/chat-completion">
    Learn how to use chat completion
  </Card>
</CardGroup>
