Anthropic Provider

The Anthropic provider contains language model support for the Anthropic Messages API.

Setup

The Anthropic provider is available in the @ai-sdk/anthropic module. You can install it with

pnpm add @ai-sdk/anthropic

Provider Instance

You can import the default provider instance anthropic from @ai-sdk/anthropic:

import { anthropic } from '@ai-sdk/anthropic';

If you need a customized setup, you can import createAnthropic from @ai-sdk/anthropic and create a provider instance with your settings:

import { createAnthropic } from '@ai-sdk/anthropic';
const anthropic = createAnthropic({
// custom settings
});

You can use the following optional settings to customize the Anthropic provider instance:

  • baseURL string

    Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is https://api.anthropic.com/v1.

  • apiKey string

    API key that is being sent using the x-api-key header. It defaults to the ANTHROPIC_API_KEY environment variable.

  • headers Record<string,string>

    Custom headers to include in the requests.

  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>

    Custom fetch implementation. Defaults to the global fetch function. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.

Language Models

You can create models that call the Anthropic Messages API using the provider instance. The first argument is the model id, e.g. claude-3-haiku-20240307. Some models have multi-modal capabilities.

const model = anthropic('claude-3-haiku-20240307');

You can use Anthropic language models to generate text with the generateText function:

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.',
});

Anthropic language models can also be used in the streamText, generateObject, and streamObject functions (see AI SDK Core).

The following optional provider options are available for Anthropic models:

  • disableParallelToolUse boolean

    Optional. Disables the use of parallel tool calls. Defaults to false.

    When set to true, the model will only call one tool at a time instead of potentially calling multiple tools in parallel.

  • sendReasoning boolean

    Optional. Include reasoning content in requests sent to the model. Defaults to true.

    If you are experiencing issues with the model handling requests involving reasoning content, you can set this to false to omit them from the request.

  • effort "high" | "medium" | "low"

    Optional. See Effort section for more details.

  • thinking object

    Optional. See Reasoning section for more details.

  • toolStreaming boolean

    Whether to enable tool streaming (and structured output streaming). Default to true.

  • structuredOutputMode "outputFormat" | "jsonTool" | "auto"

    Determines how structured outputs are generated. Optional.

    • "outputFormat": Use the output_format parameter to specify the structured output format.
    • "jsonTool": Use a special "json" tool to specify the structured output format.
    • "auto": Use "outputFormat" when supported, otherwise fall back to "jsonTool" (default).

Structured Outputs and Tool Input Streaming

Tool call streaming is enabled by default. You can opt out by setting the toolStreaming provider option to false.

import { anthropic } from '@ai-sdk/anthropic';
import { streamText, tool } from 'ai';
import { z } from 'zod';
const result = streamText({
model: anthropic('claude-sonnet-4-20250514'),
tools: {
writeFile: tool({
description: 'Write content to a file',
inputSchema: z.object({
path: z.string(),
content: z.string(),
}),
execute: async ({ path, content }) => {
// Implementation
return { success: true };
},
}),
},
prompt: 'Write a short story to story.txt',
});

Effort

Anthropic introduced an effort option with claude-opus-4-5 that affects thinking, text responses, and function calls. Effort defaults to high and you can set it to medium or low to save tokens and to lower time-to-last-token latency (TTLT).

import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const { text, usage } = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'How many people will live in the world in 2040?',
providerOptions: {
anthropic: {
effort: 'low',
} satisfies AnthropicProviderOptions,
},
});
console.log(text); // resulting text
console.log(usage); // token usage

Reasoning

Anthropic has reasoning support for claude-opus-4-20250514, claude-sonnet-4-20250514, and claude-3-7-sonnet-20250219 models.

You can enable it using the thinking provider option and specifying a thinking budget in tokens.

import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const { text, reasoningText, reasoning } = 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 },
} satisfies AnthropicProviderOptions,
},
});
console.log(reasoningText); // reasoning text
console.log(reasoning); // reasoning details including redacted reasoning
console.log(text); // text response

See AI SDK UI: Chatbot for more details on how to integrate reasoning into your chatbot.

Context Management

Anthropic's Context Management feature allows you to automatically manage conversation context by clearing tool uses or thinking content when certain conditions are met. This helps optimize token usage and manage long conversations more efficiently.

You can configure context management using the contextManagement provider option:

import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const result = await generateText({
model: anthropic('claude-3-7-sonnet-20250219'),
prompt: 'Continue our conversation...',
providerOptions: {
anthropic: {
contextManagement: {
edits: [
{
type: 'clear_tool_uses_20250919',
trigger: { type: 'input_tokens', value: 10000 },
keep: { type: 'tool_uses', value: 5 },
clearAtLeast: { type: 'input_tokens', value: 1000 },
clearToolInputs: true,
excludeTools: ['important_tool'],
},
],
},
} satisfies AnthropicProviderOptions,
},
});
// Check what was cleared
console.log(result.providerMetadata?.anthropic?.contextManagement);

Clear Tool Uses

The clear_tool_uses_20250919 edit type removes old tool calls from the conversation history:

  • trigger - Condition that triggers the clearing (e.g., { type: 'input_tokens', value: 10000 })
  • keep - How many recent tool uses to preserve (e.g., { type: 'tool_uses', value: 5 })
  • clearAtLeast - Minimum amount to clear (e.g., { type: 'input_tokens', value: 1000 })
  • clearToolInputs - Whether to clear tool input parameters (boolean)
  • excludeTools - Array of tool names to never clear

Clear Thinking

The clear_thinking_20251015 edit type removes thinking/reasoning content:

const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Continue reasoning...',
providerOptions: {
anthropic: {
thinking: { type: 'enabled', budgetTokens: 12000 },
contextManagement: {
edits: [
{
type: 'clear_thinking_20251015',
keep: { type: 'thinking_turns', value: 2 },
},
],
},
} satisfies AnthropicProviderOptions,
},
});

Applied Edits Metadata

After generation, you can check which edits were applied in the provider metadata:

const metadata = result.providerMetadata?.anthropic?.contextManagement;
if (metadata?.appliedEdits) {
metadata.appliedEdits.forEach(edit => {
if (edit.type === 'clear_tool_uses_20250919') {
console.log(`Cleared ${edit.clearedToolUses} tool uses`);
console.log(`Freed ${edit.clearedInputTokens} tokens`);
} else if (edit.type === 'clear_thinking_20251015') {
console.log(`Cleared ${edit.clearedThinkingTurns} thinking turns`);
console.log(`Freed ${edit.clearedInputTokens} tokens`);
}
});
}

For more details, see Anthropic's Context Management documentation.

Cache Control

In the messages and message parts, you can use the providerOptions property to set cache control breakpoints. You need to set the anthropic property in the providerOptions object to { cacheControl: { type: 'ephemeral' } } to set a cache control breakpoint.

The cache creation input tokens are then returned in the providerMetadata object for generateText and generateObject, again under the anthropic property. When you use streamText or streamObject, the response contains a promise that resolves to the metadata. Alternatively you can receive it in the onFinish callback.

import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const errorMessage = '... long error message ...';
const result = await generateText({
model: anthropic('claude-3-5-sonnet-20240620'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'You are a JavaScript expert.' },
{
type: 'text',
text: `Error message: ${errorMessage}`,
providerOptions: {
anthropic: { cacheControl: { type: 'ephemeral' } },
},
},
{ type: 'text', text: 'Explain the error message.' },
],
},
],
});
console.log(result.text);
console.log(result.providerMetadata?.anthropic);
// e.g. { cacheCreationInputTokens: 2118 }

You can also use cache control on system messages by providing multiple system messages at the head of your messages array:

const result = await generateText({
model: anthropic('claude-3-5-sonnet-20240620'),
messages: [
{
role: 'system',
content: 'Cached system message part',
providerOptions: {
anthropic: { cacheControl: { type: 'ephemeral' } },
},
},
{
role: 'system',
content: 'Uncached system message part',
},
{
role: 'user',
content: 'User prompt',
},
],
});

Cache control for tools:

const result = await generateText({
model: anthropic('claude-3-5-haiku-latest'),
tools: {
cityAttractions: tool({
inputSchema: z.object({ city: z.string() }),
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral' },
},
},
}),
},
messages: [
{
role: 'user',
content: 'User prompt',
},
],
});

Longer cache TTL

Anthropic also supports a longer 1-hour cache duration.

Here's an example:

const result = await generateText({
model: anthropic('claude-3-5-haiku-latest'),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'Long cached message',
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral', ttl: '1h' },
},
},
},
],
},
],
});

Limitations

The minimum cacheable prompt length is:

  • 4096 tokens for Claude Opus 4.5
  • 1024 tokens for Claude Opus 4.1, Claude Opus 4, Claude Sonnet 4.5, Claude Sonnet 4, Claude Sonnet 3.7, and Claude Opus 3
  • 4096 tokens for Claude Haiku 4.5
  • 2048 tokens for Claude Haiku 3.5 and Claude Haiku 3

Shorter prompts cannot be cached, even if marked with cacheControl. Any requests to cache fewer than this number of tokens will be processed without caching.

For more on prompt caching with Anthropic, see Anthropic's Cache Control documentation.

Because the UIMessage type (used by AI SDK UI hooks like useChat) does not support the providerOptions property, you can use convertToModelMessages first before passing the messages to functions like generateText or streamText. For more details on providerOptions usage, see here.

Bash Tool

The Bash Tool allows running bash commands. Here's how to create and use it:

const bashTool = anthropic.tools.bash_20241022({
execute: async ({ command, restart }) => {
// Implement your bash command execution logic here
// Return the result of the command execution
},
});

Parameters:

  • command (string): The bash command to run. Required unless the tool is being restarted.
  • restart (boolean, optional): Specifying true will restart this tool.
Only certain Claude versions are supported.

Memory Tool

The Memory Tool allows Claude to use a local memory, e.g. in the filesystem. Here's how to create it:

const memory = anthropic.tools.memory_20250818({
execute: async action => {
// Implement your memory command execution logic here
// Return the result of the command execution
},
});
Only certain Claude versions are supported.

Text Editor Tool

The Text Editor Tool provides functionality for viewing and editing text files.

const tools = {
str_replace_based_edit_tool: anthropic.tools.textEditor_20250728({
maxCharacters: 10000, // optional
async execute({ command, path, old_str, new_str }) {
// ...
},
}),
} satisfies ToolSet;

Different models support different versions of the tool. For Claude Sonnet 3.5 and 3.7 you need to use older tool versions.

Parameters:

  • command ('view' | 'create' | 'str_replace' | 'insert' | 'undo_edit'): The command to run. Note: undo_edit is only available in Claude 3.5 Sonnet and earlier models.
  • path (string): Absolute path to file or directory, e.g. /repo/file.py or /repo.
  • file_text (string, optional): Required for create command, with the content of the file to be created.
  • insert_line (number, optional): Required for insert command. The line number after which to insert the new string.
  • new_str (string, optional): New string for str_replace or insert commands.
  • old_str (string, optional): Required for str_replace command, containing the string to replace.
  • view_range (number[], optional): Optional for view command to specify line range to show.

Computer Tool

The Computer Tool enables control of keyboard and mouse actions on a computer:

const computerTool = anthropic.tools.computer_20241022({
displayWidthPx: 1920,
displayHeightPx: 1080,
displayNumber: 0, // Optional, for X11 environments
execute: async ({ action, coordinate, text }) => {
// Implement your computer control logic here
// Return the result of the action
// Example code:
switch (action) {
case 'screenshot': {
// multipart result:
return {
type: 'image',
data: fs
.readFileSync('./data/screenshot-editor.png')
.toString('base64'),
};
}
default: {
console.log('Action:', action);
console.log('Coordinate:', coordinate);
console.log('Text:', text);
return `executed ${action}`;
}
}
},
// map to tool result content for LLM consumption:
toModelOutput(result) {
return typeof result === 'string'
? [{ type: 'text', text: result }]
: [{ type: 'image', data: result.data, mediaType: 'image/png' }];
},
});

Parameters:

  • action ('key' | 'type' | 'mouse_move' | 'left_click' | 'left_click_drag' | 'right_click' | 'middle_click' | 'double_click' | 'screenshot' | 'cursor_position'): The action to perform.
  • coordinate (number[], optional): Required for mouse_move and left_click_drag actions. Specifies the (x, y) coordinates.
  • text (string, optional): Required for type and key actions.

These tools can be used in conjunction with the sonnet-3-5-sonnet-20240620 model to enable more complex interactions and tasks.

Web Search Tool

Anthropic provides a provider-defined web search tool that gives Claude direct access to real-time web content, allowing it to answer questions with up-to-date information beyond its knowledge cutoff.

You can enable web search using the provider-defined web search tool:

import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const webSearchTool = anthropic.tools.webSearch_20250305({
maxUses: 5,
});
const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'What are the latest developments in AI?',
tools: {
web_search: webSearchTool,
},
});

Web search must be enabled in your organization's Console settings.

Configuration Options

The web search tool supports several configuration options:

  • maxUses number

    Maximum number of web searches Claude can perform during the conversation.

  • allowedDomains string[]

    Optional list of domains that Claude is allowed to search. If provided, searches will be restricted to these domains.

  • blockedDomains string[]

    Optional list of domains that Claude should avoid when searching.

  • userLocation object

    Optional user location information to provide geographically relevant search results.

const webSearchTool = anthropic.tools.webSearch_20250305({
maxUses: 3,
allowedDomains: ['techcrunch.com', 'wired.com'],
blockedDomains: ['example-spam-site.com'],
userLocation: {
type: 'approximate',
country: 'US',
region: 'California',
city: 'San Francisco',
timezone: 'America/Los_Angeles',
},
});
const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Find local news about technology',
tools: {
web_search: webSearchTool,
},
});

Web Fetch Tool

Anthropic provides a provider-defined web fetch tool that allows Claude to retrieve content from specific URLs. This is useful when you want Claude to analyze or reference content from a particular webpage or document.

You can enable web fetch using the provider-defined web fetch tool:

import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const result = await generateText({
model: anthropic('claude-sonnet-4-0'),
prompt:
'What is this page about? https://en.wikipedia.org/wiki/Maglemosian_culture',
tools: {
web_fetch: anthropic.tools.webFetch_20250910({ maxUses: 1 }),
},
});

Anthropic provides provider-defined tool search tools that enable Claude to work with hundreds or thousands of tools by dynamically discovering and loading them on-demand. Instead of loading all tool definitions into the context window upfront, Claude searches your tool catalog and loads only the tools it needs.

There are two variants:

  • BM25 Search - Uses natural language queries to find tools
  • Regex Search - Uses regex patterns (Python re.search() syntax) to find tools

Basic Usage

import { anthropic } from '@ai-sdk/anthropic';
import { generateText, tool } from 'ai';
import { z } from 'zod';
const result = await generateText({
model: anthropic('claude-sonnet-4-5'),
prompt: 'What is the weather in San Francisco?',
tools: {
toolSearch: anthropic.tools.toolSearchBm25_20251119(),
get_weather: tool({
description: 'Get the current weather at a specific location',
inputSchema: z.object({
location: z.string().describe('The city and state'),
}),
execute: async ({ location }) => ({
location,
temperature: 72,
condition: 'Sunny',
}),
// Defer tool here - Claude discovers these via the tool search tool
providerOptions: {
anthropic: { deferLoading: true },
},
}),
},
});

For more precise tool matching, you can use the regex variant:

const result = await generateText({
model: anthropic('claude-sonnet-4-5'),
prompt: 'Get the weather data',
tools: {
toolSearch: anthropic.tools.toolSearchRegex_20251119(),
// ... deferred tools
},
});

Claude will construct regex patterns like weather|temperature|forecast to find matching tools.

MCP Connectors

Anthropic supports connecting to MCP servers as part of their execution.

You can enable this feature with the mcpServers provider option:

import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const result = await generateText({
model: anthropic('claude-sonnet-4-5'),
prompt: `Call the echo tool with "hello world". what does it respond with back?`,
providerOptions: {
anthropic: {
mcpServers: [
{
type: 'url',
name: 'echo',
url: 'https://echo.mcp.inevitable.fyi/mcp',
// optional: authorization token
authorizationToken: mcpAuthToken,
// optional: tool configuration
toolConfiguration: {
enabled: true,
allowedTools: ['echo'],
},
},
],
} satisfies AnthropicProviderOptions,
},
});

The tool calls and results are dynamic, i.e. the input and output schemas are not known.

Configuration Options

The web fetch tool supports several configuration options:

  • maxUses number

    The maxUses parameter limits the number of web fetches performed.

  • allowedDomains string[]

    Only fetch from these domains.

  • blockedDomains string[]

    Never fetch from these domains.

  • citations object

    Unlike web search where citations are always enabled, citations are optional for web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages from fetched documents.

  • maxContentTokens number

    The maxContentTokens parameter limits the amount of content that will be included in the context.

Error Handling

Web search errors are handled differently depending on whether you're using streaming or non-streaming:

Non-streaming (generateText, generateObject): Web search errors throw exceptions that you can catch:

try {
const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Search for something',
tools: {
web_search: webSearchTool,
},
});
} catch (error) {
if (error.message.includes('Web search failed')) {
console.log('Search error:', error.message);
// Handle search error appropriately
}
}

Streaming (streamText, streamObject): Web search errors are delivered as error parts in the stream:

const result = await streamText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Search for something',
tools: {
web_search: webSearchTool,
},
});
for await (const part of result.textStream) {
if (part.type === 'error') {
console.log('Search error:', part.error);
// Handle search error appropriately
}
}

Code Execution

Anthropic provides a provider-defined code execution tool that gives Claude direct access to a real Python environment allowing it to execute code to inform its responses.

You can enable code execution using the provider-defined code execution tool:

import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const codeExecutionTool = anthropic.tools.codeExecution_20250825();
const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt:
'Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]',
tools: {
code_execution: codeExecutionTool,
},
});

Error Handling

Code execution errors are handled differently depending on whether you're using streaming or non-streaming:

Non-streaming (generateText, generateObject): Code execution errors are delivered as tool result parts in the response:

const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Execute some Python script',
tools: {
code_execution: codeExecutionTool,
},
});
const toolErrors = result.content?.filter(
content => content.type === 'tool-error',
);
toolErrors?.forEach(error => {
console.error('Tool execution error:', {
toolName: error.toolName,
toolCallId: error.toolCallId,
error: error.error,
});
});

Streaming (streamText, streamObject): Code execution errors are delivered as error parts in the stream:

const result = await streamText({
model: anthropic('claude-opus-4-20250514'),
prompt: 'Execute some Python script',
tools: {
code_execution: codeExecutionTool,
},
});
for await (const part of result.textStream) {
if (part.type === 'error') {
console.log('Code execution error:', part.error);
// Handle code execution error appropriately
}
}

Agent Skills

Anthropic Agent Skills enable Claude to perform specialized tasks like document processing (PPTX, DOCX, PDF, XLSX) and data analysis. Skills run in a sandboxed container and require the code execution tool to be enabled.

Using Built-in Skills

Anthropic provides several built-in skills:

  • pptx - Create and edit PowerPoint presentations
  • docx - Create and edit Word documents
  • pdf - Process and analyze PDF files
  • xlsx - Work with Excel spreadsheets

To use skills, you need to:

  1. Enable the code execution tool
  2. Specify the container with skills in providerOptions
import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const result = await generateText({
model: anthropic('claude-sonnet-4-5'),
tools: {
code_execution: anthropic.tools.codeExecution_20250825(),
},
prompt: 'Create a presentation about renewable energy with 5 slides',
providerOptions: {
anthropic: {
container: {
skills: [
{
type: 'anthropic',
skillId: 'pptx',
version: 'latest', // optional
},
],
},
} satisfies AnthropicProviderOptions,
},
});

Custom Skills

You can also use custom skills by specifying type: 'custom':

const result = await generateText({
model: anthropic('claude-sonnet-4-5'),
tools: {
code_execution: anthropic.tools.codeExecution_20250825(),
},
prompt: 'Use my custom skill to process this data',
providerOptions: {
anthropic: {
container: {
skills: [
{
type: 'custom',
skillId: 'my-custom-skill-id',
version: '1.0', // optional
},
],
},
} satisfies AnthropicProviderOptions,
},
});

Skills use progressive context loading and execute within a sandboxed container with code execution capabilities.

PDF support

Anthropic Sonnet claude-3-5-sonnet-20241022 supports reading PDF files. You can pass PDF files as part of the message content using the file type:

Option 1: URL-based PDF document

const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'What is an embedding model according to this document?',
},
{
type: 'file',
data: new URL(
'https://github.com/vercel/ai/blob/main/examples/ai-core/data/ai.pdf?raw=true',
),
mimeType: 'application/pdf',
},
],
},
],
});

Option 2: Base64-encoded PDF document

const result = await generateText({
model: anthropic('claude-3-5-sonnet-20241022'),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: 'What is an embedding model according to this document?',
},
{
type: 'file',
data: fs.readFileSync('./data/ai.pdf'),
mediaType: 'application/pdf',
},
],
},
],
});

The model will have access to the contents of the PDF file and respond to questions about it. The PDF file should be passed using the data field, and the mediaType should be set to 'application/pdf'.

Model Capabilities

ModelImage InputObject GenerationTool UsageComputer UseWeb SearchTool Search
claude-opus-4-5
claude-haiku-4-5
claude-sonnet-4-5
claude-opus-4-1
claude-opus-4-0
claude-sonnet-4-0
claude-3-7-sonnet-latest
claude-3-5-haiku-latest

The table above lists popular models. Please see the Anthropic docs for a full list of available models. The table above lists popular models. You can also pass any available provider model ID as a string if needed.