Install and use the Anthropic provider for Vercel AI SDK to access Claude models via the Messages API. Includes setup, configuration, and usage examples.
Integrate Anthropic's Claude models into your application using the Vercel AI SDK. This skill guides you through installing and using the `@ai-sdk/anthropic` provider to access the Anthropic Messages API.
This skill helps AI agents install the Anthropic provider for Vercel AI SDK and implement text generation using Claude models. It covers package installation, provider initialization, and basic usage patterns.
Install the Anthropic provider package using npm:
```bash
npm i @ai-sdk/anthropic
```
Verify the package is added to `package.json` dependencies.
Import the default provider instance in your TypeScript/JavaScript file:
```typescript
import { anthropic } from '@ai-sdk/anthropic';
```
Import the necessary functions from the AI SDK core package:
```typescript
import { generateText } from 'ai';
```
Note: Ensure `ai` package is installed. If not, run `npm i ai`.
The Anthropic provider requires an API key. Ensure the `ANTHROPIC_API_KEY` environment variable is set:
```
ANTHROPIC_API_KEY=your_api_key_here
```
Use the `generateText` function with an Anthropic model:
```typescript
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const { text } = await generateText({
model: anthropic('claude-3-haiku-20240307'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(text);
```
Available Claude models (as of package version 3.0.35):
Replace the model ID in the example above with your chosen model.
For projects using coding agents like Claude Code or Cursor, add the AI SDK skill:
```bash
npx skills add vercel/ai
```
This provides the agent with comprehensive AI SDK documentation.
**Basic text generation:**
```typescript
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
async function main() {
const { text } = await generateText({
model: anthropic('claude-3-haiku-20240307'),
prompt: 'Explain quantum computing in simple terms.',
});
console.log(text);
}
main();
```
**With streaming:**
```typescript
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';
async function stream() {
const result = streamText({
model: anthropic('claude-3-sonnet-20240229'),
prompt: 'Write a short story about a time traveler.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
}
stream();
```
Full documentation: [https://ai-sdk.dev/providers/ai-sdk-providers/anthropic](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic)
Anthropic Messages API: [https://docs.anthropic.com/claude/reference/messages_post](https://docs.anthropic.com/claude/reference/messages_post)
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/ai-sdk-anthropic-provider/raw