Documentation Index
Fetch the complete documentation index at: https://mintlify.com/vercel/ai/llms.txt
Use this file to discover all available pages before exploring further.
Anthropic Provider
The Anthropic provider enables you to use Claude models for advanced text generation, reasoning, and tool calling capabilities.
Installation
npm install @ai-sdk/anthropic
Setup
Get your API key from Anthropic’s Console and set it as an environment variable:
ANTHROPIC_API_KEY=your-api-key
Usage
Basic Text Generation
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const { text } = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
prompt: 'Explain quantum computing in simple terms.',
});
Streaming Responses
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';
const result = streamText({
model: anthropic('claude-3-5-sonnet-20241022'),
prompt: 'Write a short story about AI.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
Structured Output
import { anthropic } from '@ai-sdk/anthropic';
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: anthropic('claude-3-5-sonnet-20241022'),
schema: z.object({
ingredients: z.array(z.string()),
instructions: z.array(z.string()),
}),
prompt: 'Generate a recipe for chocolate chip cookies.',
});
Configuration
Custom Provider Instance
import { createAnthropic } from '@ai-sdk/anthropic';
const anthropic = createAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
baseURL: 'https://api.anthropic.com/v1',
});
Provider Options
- apiKey: Your Anthropic API key (defaults to
ANTHROPIC_API_KEY env var)
- baseURL: Custom API endpoint URL
- headers: Custom headers for requests
Available Models
Claude 3.5 Family
claude-3-5-sonnet-20241022 - Most capable Claude 3.5 model
claude-3-5-haiku-20241022 - Fastest Claude 3.5 model
Claude 3 Family
claude-3-opus-20240229 - Most powerful model
claude-3-sonnet-20240229 - Balanced performance
claude-3-haiku-20240307 - Fast and cost-effective
Advanced Features
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import { z } from 'zod';
const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
tools: {
calculator: {
description: 'Perform calculations',
parameters: z.object({
expression: z.string(),
}),
execute: async ({ expression }) => {
return { result: eval(expression) };
},
},
},
prompt: 'What is 15 multiplied by 24?',
});
Vision
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import fs from 'fs';
const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe this image in detail.' },
{ type: 'image', image: fs.readFileSync('./photo.jpg') },
],
},
],
});
PDF Support
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import fs from 'fs';
const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Summarize this document.' },
{
type: 'file',
data: fs.readFileSync('./document.pdf'),
mediaType: 'application/pdf',
},
],
},
],
});
Prompt Caching
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
messages: [
{
role: 'user',
content: 'Long context that should be cached...',
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral' },
},
},
},
],
});
Reasoning Models
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const { text, reasoningText } = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'How many people will live in the world in 2040?',
providerOptions: {
anthropic: {
thinking: { type: 'enabled', budgetTokens: 12000 },
},
},
});
console.log('Reasoning:', reasoningText);
console.log('Answer:', text);
Model Capabilities
| Model | Vision | Tools | Structured Output |
|---|
| claude-3-5-sonnet-20241022 | ✓ | ✓ | ✓ |
| claude-3-5-haiku-20241022 | ✓ | ✓ | ✓ |
| claude-3-opus-20240229 | ✓ | ✓ | ✓ |
| claude-3-sonnet-20240229 | ✓ | ✓ | ✓ |
| claude-3-haiku-20240307 | ✓ | ✓ | ✓ |
Resources