Anthropic
Learn how to use the Anthropic provider for the AI SDK.
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
$ npm install @ai-sdk/anthropic
$ yarn add @ai-sdk/anthropic
$ bun add @ai-sdk/anthropic
Provider Instance
You can import the default provider instance 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:
You can use the following optional settings to customize the Anthropic provider instance:
-
batchResultDownloads object
Settings for downloading JSON Lines batch results.
-
maxLineBytes number
Maximum UTF-8 bytes per row, excluding the LF delimiter. Defaults to 64 MiB (67,108,864 bytes). Must be a positive safe integer. Oversized rows throw a
DownloadErrorand cancel the download.
-
-
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-keyheader. It defaults to theANTHROPIC_API_KEYenvironment variable. Only one ofapiKeyorauthTokenis required. -
authToken string
Auth token that is being sent using the
Authorization: Bearerheader. It defaults to theANTHROPIC_AUTH_TOKENenvironment variable. Only one ofapiKeyorauthTokenis required. -
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
fetchfunction. 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-haiku-5-5.
Some models have multi-modal capabilities.
You can also use the following aliases for model creation:
anthropic.languageModel('claude-haiku-5-5')- Creates a language modelanthropic.chat('claude-haiku-5-5')- Alias forlanguageModelanthropic.messages('claude-haiku-5-5')- Alias forlanguageModel
You can use Anthropic language models to generate text with the generateText function:
Anthropic language models can also be used in the streamText function
and support structured data generation with Output
(see AI SDK Core).
Message-start events
During streaming, each Anthropic message_start event is exposed as an
anthropic.message_start custom stream part containing the message ID, model,
and raw initial token usage. No provider option is required.
Initial usage may be incomplete; use result.usage for final token counts.
Like other custom parts, this event is included in response content and forwarded
to UI message streams. It is ignored when replaying conversation history to Anthropic.
Provider options
The following optional provider options are available for Anthropic models:
-
disableParallelToolUsebooleanOptional. 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. -
sendReasoningbooleanOptional. 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
falseto omit them from the request. -
effort"low" | "medium" | "high" | "xhigh" | "max"Optional. See Effort section for more details.
-
taskBudgetobjectOptional. See Task Budgets section for more details.
-
speed"fast" | "standard"Optional. See Fast Mode section for more details.
-
inferenceGeo"us" | "global"Optional. See Data Residency section for more details.
-
thinkingobjectOptional. See Reasoning section for more details.
-
toolStreamingbooleanWhether to enable tool streaming (and structured output streaming). Default to
true. -
structuredOutputMode"outputFormat" | "jsonTool" | "auto"Determines how structured outputs are generated. Optional.
"outputFormat": Use theoutput_formatparameter 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).
-
metadataobjectOptional. Metadata to include with the request. See the Anthropic API documentation for details.
userIdstring - An external identifier for the end-user. Should be a UUID, hash, or other opaque identifier. Must not contain PII.
Batch
Batch support is experimental and the API may change in patch releases.
Anthropic language models support asynchronous text generation through the Message Batches API. Pass the Anthropic provider to the AI SDK's Batch API for the complete workflow, including polling, persistence, and result handling.
Each request specifies its type and model. Anthropic supports using
different text models within the same batch.
Batch Limitations
Anthropic Message Batches do not support the speed option or completion
webhooks. Explicit anthropicBeta values must be configured when starting the
batch rather than on an individual request. When you provide a webhookUrl,
the provider returns an unsupported warning and starts the batch without a
webhook.
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.
For claude-sonnet-5-5, claude-opus-5-5, and claude-fable-5-1, the
default "auto" mode uses native structured outputs through
output_config.format. These models reject forced tool use, so the
"jsonTool" mode cannot be used with them. If you
request "jsonTool" anyway, the provider falls back to "outputFormat" and
emits a warning. Likewise, a required or named toolChoice is downgraded to
auto with a warning; instruct the model to use the tool in your prompt and
verify that a tool call was made.
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). claude-opus-4-7, claude-opus-4-8, claude-opus-5, claude-opus-5-5, claude-fable-5, claude-fable-5-1, claude-sonnet-5, claude-sonnet-5-5, and claude-haiku-5-5 additionally support xhigh for maximum reasoning effort.
On claude-opus-5 and claude-haiku-5-5, thinking can only be disabled at effort levels up to and including high. When you combine thinking: { type: 'disabled' } with effort: 'xhigh' or effort: 'max', the AI SDK lowers the effort to high and emits a warning instead of sending a request that the API would reject.
claude-haiku-5-5 is the first Haiku model with effort levels and supports all
five of them. It uses adaptive thinking and does not support thinking token
budgets: the AI SDK converts thinking: { type: 'enabled', budgetTokens } to
{ type: 'adaptive' } and emits a warning. Use effort to control how much it
thinks. Unlike claude-sonnet-5-5, thinking can still be turned off with
thinking: { type: 'disabled' }, but only at low, medium, and high effort.
Start at medium effort and test other levels rather than carrying over the
settings you used on claude-haiku-4-5.
On claude-opus-5-5, thinking is always adaptive and cannot be disabled, so
effort is the main control for latency and cost. The API default effort for
claude-opus-5-5 is medium. Start there and test other levels rather than
carrying over the setting you used on claude-opus-5. Set maxOutputTokens
with room for thinking as well as the reply; thinking counts toward the limit
even when its content is not returned.
On claude-sonnet-5-5, thinking cannot be turned off either, and the API
default effort is high. Its effort levels are not equivalent to those of
claude-sonnet-5, so test several levels rather than carrying over your
current setting. For agentic coding and other multi-step tool use, start at
medium for well-specified tasks and move to high for harder ones. The
lowest thinking setting, between_tools, is only
accepted at low, medium, and high effort. When you combine it with
effort: 'xhigh' or effort: 'max', the AI SDK lowers the effort to high
and emits a warning.
Fast Mode
Anthropic supports a speed option for claude-opus-4-6 that enables faster inference with approximately 2.5x faster output token speeds.
The speed option accepts 'fast' or 'standard' (default behavior).
Task Budgets
claude-opus-4-7 supports a taskBudget option that informs the model of the total token budget available for an agentic turn. The model uses this information to prioritize work, plan ahead, and wind down gracefully as the budget is consumed.
Task budgets are advisory — they do not enforce a hard token limit. The model will attempt to stay within budget, but actual usage may vary.
For long-running agents that compact and restart context, you can carry the remaining budget forward using the remaining field:
The taskBudget object accepts:
type"tokens" - Budget type. Currently only"tokens"is supported.totalnumber - Total task budget for the agentic turn. Minimum 20,000.remainingnumber - Budget left after prior compacted-away contexts. Must be between 0 andtotal. Defaults tototalif omitted.
Data Residency
Anthropic supports an inferenceGeo option that controls where model inference runs for a request.
The inferenceGeo option accepts 'us' (US-only infrastructure) or 'global' (default, any available geography).
Safety Classifiers and Fallbacks
Claude Fable 5 has safeguards that limit its performance in certain areas like cybersecurity, biology, and chemistry, and automated safety checks are run on every request.
Claude Haiku 5.5 has the same biology safeguards as Claude Sonnet 5 and 5.5, plus safeguards that prevent cyber misuse.
Claude Sonnet 5.5 also has safety classifiers enabled, including for cybersecurity, biology, and reasoning extraction. Requests that ask the model to reproduce its internal reasoning in the response text can be declined with the reasoning_extraction category; use thinking: { type: 'adaptive', display: 'summarized' } and read the reasoning parts instead.
When one of these checks blocks a request, the API does not answer it. Instead it returns a classifier block: a 200 response with a refusal stop reason and an optional stop_details object describing the category that triggered the block. The AI SDK surfaces this as a content-filter finish reason, with the details available on providerMetadata.anthropic.stopDetails.
A classifier block looks like this:
Branch on the finish reason rather than on the presence of stop_details — the API may return a refusal with no details at all.
To avoid receiving a classifier block, pass the fallbacks option. When the primary model's classifiers block a turn, the API automatically retries it server-side on a fallback model and returns that model's answer. The required beta header is added for you.
The recommended configuration is fallbacks: 'default', which lets the API route the retry to Anthropic's recommended fallback model based on the refusal category and removes the need to migrate when a fallback model is deprecated:
You can also pin an explicit fallback chain:
Each fallback entry requires a model and may additionally override max_tokens, thinking, output_config, and speed for that attempt only.
When a fallback serves the turn, it is recorded in the per-model usage breakdown (usage.iterations) as a fallback_message entry. The AI SDK exposes this breakdown on providerMetadata.anthropic.iterations, so you can detect whether a fallback ran:
Dangerous Tool Use Safeguard
The safeguards option asks the API to run additional server-side checks as part of the request. The dangerous_tool_use safeguard classifies every tool_use block in the response for dangerous actions, such as destructive shell commands or data exfiltration, and returns a verdict per tool call. This is the check that Claude Code's auto mode relies on. The dangerous-tool-use-2026-09-03 beta header is added for you.
The verdicts are available on providerMetadata.anthropic.safeguardResults, one entry per requested safeguard, in the API's wire shape:
status.type is available when the classifier ran; unsupported means the API key is not enabled for the beta. Inside status.tool_uses, each tool call id maps to evaluated (with an outcome of not_flagged or flagged, the latter with an explanation such as [Data Exfiltration]), skipped, or unavailable (a transient classifier failure). When streaming, the verdicts arrive on the final message_delta event and are exposed on the finish part's provider metadata.
Anthropic's platform reference does not document the beta yet; the feature is described from the client side in the Claude Code auto mode classifier and gateway compatibility guides.
Reasoning
Anthropic models support extended thinking, where Claude shows its reasoning process before providing a final answer.
Adaptive Thinking
For newer models (claude-sonnet-4-6, claude-opus-4-6, and later), use adaptive thinking.
Claude automatically determines how much reasoning to use based on the complexity of the prompt.
You can combine adaptive thinking with the effort option to control how much reasoning Claude uses:
Adaptive-Only Models
claude-sonnet-5-5, claude-opus-5-5, claude-fable-5, and
claude-fable-5-1 always run adaptive thinking. The API rejects
thinking: { type: 'disabled' } and budget-based
thinking: { type: 'enabled' } for these models. To keep existing code
working, the provider:
- removes
thinking: { type: 'disabled' }and emits a warning, - converts
thinking: { type: 'enabled', budgetTokens }to{ type: 'adaptive' }and emits a warning, - maps the top-level
reasoning: 'none'option toeffort: 'low'and emits a warning.
On claude-sonnet-5-5, which supports
between_tools thinking, the provider instead
replaces thinking: { type: 'disabled' } with thinking: { type: 'between_tools' }
(with a warning) and maps reasoning: 'none' to between_tools thinking.
Use effort to control how much these models think. On claude-opus-5-5, set
effort: 'low' when time to first token matters, and move to medium if
quality drops:
Responses from adaptive-only models can begin with a thinking block. Read the
response by content type (text, reasoning, tool-call) rather than
assuming the first part is text. Under the default display: 'omitted'
setting, thinking blocks are returned with empty text.
Between-Tools Thinking
claude-sonnet-5-5 supports thinking: { type: 'between_tools' }, its lowest
thinking setting. The model does no upfront thinking, but the short progress
notes it writes between tool calls are still returned as thinking blocks, each
with a short summary. They are available as reasoning parts and are sent back
unchanged on the next step. between_tools accepts no other thinking options
(such as display or blockBinding) and is only accepted at low, medium,
and high effort. Per-message effort changes via
mid-conversation system messages are not
supported with between_tools.
Thinking Display (Opus 4.7+)
Starting with claude-opus-4-7, thinking content is omitted from the response by default — thinking blocks are present in the stream but their text is empty. To receive reasoning output, set display: 'summarized':
If you stream reasoning to users with claude-opus-4-7, the default
"omitted" display will cause a long pause before output begins. Set
display: "summarized" to restore visible progress during thinking.
Thinking Updates
Use display: 'updates' with claude-sonnet-5-5, claude-opus-5-5, or
claude-fable-5-1 to stream thinking summaries between tool calls. These models
return progress notes between tool calls as thinking blocks rather than text, so
without this setting a long tool-calling turn can look silent. The provider adds
the required thinking-display-updates-2026-08-18 beta header automatically:
Thinking Binding Controls
Fable 5.1, Opus 5.5, and Sonnet 5.5 can recover from a thinking-block prefix
mismatch by dropping the mismatched block. Set
blockBinding.prefixMismatchBehavior to drop_block, or set it to error to
reject the request. You can provide block binding by itself to preserve the
model's default thinking mode, or combine it with adaptive thinking. Block
binding is not available with between_tools thinking; keep the message
history append-only instead.
Dropped blocks are reported in
providerMetadata.anthropic.inputTransformations with their type, path, and
reason.
Budget-Based Thinking
For earlier models (claude-opus-4-20250514, claude-sonnet-4-20250514, claude-sonnet-4-5-20250929),
use type: 'enabled' with an explicit token budget:
Anthropic's max_tokens limit includes both thinking and final text. To reserve
room for both, the provider adds budgetTokens to maxOutputTokens when
setting max_tokens. For known models, the combined value is capped at the
model's maximum output token limit.
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:
Context Editing
Context editing strategies selectively remove specific content types from earlier in the conversation to reduce token usage without losing the overall conversation flow.
Clear Tool Uses
The clear_tool_uses_20250919 edit type removes old tool call/result pairs from the conversation history:
- trigger - Condition that triggers the clearing (e.g.,
{ type: 'input_tokens', value: 10000 }or{ type: 'tool_uses', value: 10 }) - 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 blocks from earlier turns, keeping only the most recent ones:
- keep - How many recent thinking turns to preserve (e.g.,
{ type: 'thinking_turns', value: 2 }) or'all'to keep everything
On-Demand Compaction
Use the compaction provider option to ask Anthropic to summarize the supplied
conversation immediately. The provider automatically adds the
compact-2026-09-04 beta header.
Anthropic returns the summary as a text part whose
providerMetadata.anthropic.type is compaction. Its opaque signature is
available as providerMetadata.anthropic.signature. Response messages copy
that metadata to providerOptions, so passing
summary.finalStep.response.messages back to the AI SDK preserves the signed
block and automatically adds the beta header on the continuation request.
Check rawFinishReason before reading the response block. Anthropic can return
a successful HTTP response without a summary when summarization ends for
another reason, such as max_tokens, model_context_window_exceeded,
tool_use, refusal, or end_turn. In that case, keep the existing history
and retry later; the unsuccessful summarization is still included in
providerMetadata.anthropic.iterations.
Send exactly one signed compaction block on continuation requests. It must be
the first content block in messages and must be replayed without changing its
text or signature. Remove the messages it summarizes before continuing. Keep
the same system prompt and tool definitions on the compaction request that you
use for the conversation, especially when preserving later thinking blocks.
Before requesting compaction, resolve any unfinished tool call from the last
assistant turn. Do not combine compaction with contextManagement,
stopSequences, structured output, a required or named toolChoice, or a
taskBudget.remaining value. Also omit taskBudget.remaining from later
requests that carry the signed block.
On-demand compaction usage is reported in
providerMetadata.anthropic.iterations. Sum the iteration usage when
calculating the tokens consumed by the summarization request.
On-demand compaction can also be initiated in a Message Batch. Configure the
compaction provider option on the individual batch request; the provider
automatically adds compact-2026-09-04 to the batch beta header.
For more details, see Anthropic's on-demand compaction documentation.
Threshold Compaction
The compact_20260112 edit type automatically summarizes earlier conversation context when token limits are reached. This is useful for long-running conversations where you want to preserve the essence of earlier exchanges while staying within token limits.
Configuration:
- trigger - Condition that triggers compaction (e.g.,
{ type: 'input_tokens', value: 50000 }) - instructions - Custom instructions for how the model should summarize the conversation. Use this to guide the compaction summary towards specific aspects of the conversation you want to preserve.
- pauseAfterCompaction - When
true, the model will pause after generating the compaction summary, allowing you to inspect or process it before continuing. Defaults tofalse.
When compaction occurs, the model generates a summary of the earlier context. This summary appears as a text block with special provider metadata.
Detecting Compaction in Streams
When using streamText, you can detect compaction summaries by checking the providerMetadata on text-start events:
Compaction in UI Applications
When using useChat or other UI hooks, compaction summaries appear as regular text parts with providerMetadata. You can style them differently in your UI:
Applied Edits Metadata
After generation, you can check which edits were applied in the provider metadata:
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.
Cache read and cache write (creation) token counts are returned on the standard
usage object for both generateText and streamText. You can access them at
result.usage.inputTokenDetails.cacheReadTokens and
result.usage.inputTokenDetails.cacheWriteTokens.
You can also use cache control on system messages by providing multiple system messages at the head of your messages array:
Cache control for tools:
Longer cache TTL
Anthropic also supports a longer 1-hour cache duration.
Here's an example:
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. By default, the tool executes
commands through the experimental_sandbox that you pass to generateText or streamText:
Parameters:
command(string): The bash command to run. Required unless the tool is being restarted.restart(boolean, optional): Specifying true will restart this tool.
Two versions are available: bash_20250124 (recommended) and bash_20241022.
Only certain Claude versions are supported.
You can also provide a custom execute function when you want to handle bash
execution directly instead of using the request experimental sandbox.
Memory Tool
The Memory Tool allows Claude to use a local memory, e.g. in the filesystem. Here's how to create it:
Text Editor Tool
The Text Editor Tool provides functionality for viewing and editing text files.
Different models support different versions of the tool:
textEditor_20250728- For Claude Sonnet 4, Opus 4, and Opus 4.1 (recommended)textEditor_20250124- For Claude Sonnet 3.7textEditor_20241022- For Claude Sonnet 3.5
Note: textEditor_20250429 is deprecated. Use textEditor_20250728 instead.
Parameters:
command('view' | 'create' | 'str_replace' | 'insert' | 'undo_edit'): The command to run. Note:undo_editis only available in Claude 3.5 Sonnet and earlier models.path(string): Absolute path to file or directory, e.g./repo/file.pyor/repo.file_text(string, optional): Required forcreatecommand, with the content of the file to be created.insert_line(number, optional): Required forinsertcommand. The line number after which to insert the new string.new_str(string, optional): New string forstr_replacecommand.insert_text(string, optional): Required forinsertcommand, containing the text to insert.old_str(string, optional): Required forstr_replacecommand, containing the string to replace.view_range(number[], optional): Optional forviewcommand to specify line range to show.
Computer Tool
The Computer Tool enables control of keyboard and mouse actions on a computer:
Use computerToolset_20260801 for Claude Sonnet 5.5 and Opus 5.5 (required),
Opus 5, Sonnet 5, Fable 5, Fable 5.1, and Opus 4.8. Use computer_20251124
for Claude Opus 4.5 through Opus 4.7, which support the zoom action. Use
computer_20250124 for Claude Sonnet 4.5, Haiku 4.5, Opus 4.1, Sonnet 4, Opus
4, and Sonnet 3.7.
Computer Toolset
Newer models use the computer toolset instead of a versioned computer tool.
claude-sonnet-5-5 and claude-opus-5-5 accept computer use only through the
toolset and reject the older computer_* tool types with a 400. The toolset
does not require a beta header and has no display size parameters: coordinates
are always in the pixel space of the screenshots you return. Zoom is enabled by default, and Claude can
return several actions in one turn, each as its own tool call.
The API returns each action as a separate tool_use block with
toolset_name: 'computer'. The AI SDK maps every action to the toolset tool and
passes the action name as action, so execute receives the same input shape
as the older computer tools:
Parameters:
action('screenshot' | 'zoom' | 'left_click' | 'right_click' | 'middle_click' | 'double_click' | 'triple_click' | 'left_click_drag' | 'mouse_move' | 'left_mouse_down' | 'left_mouse_up' | 'cursor_position' | 'scroll' | 'type' | 'key' | 'hold_key' | 'wait'): The member tool that Claude invoked.coordinate(number[], optional): The (x, y) pixel coordinates for click, move, and scroll actions.start_coordinate(number[], optional): Where aleft_click_dragstarts.region(number[], optional):[x1, y1, x2, y2]for thezoomaction.text(string, optional): Text to type, the key combination forkeyandhold_key, or modifier keys to hold during click and scroll actions.repeat(number, optional): How many times to press the key forkey.duration(number, optional): Seconds forhold_keyandwait.scroll_direction('up' | 'down' | 'left' | 'right', optional) andscroll_amount(number, optional): For thescrollaction.configs(object, optional): Per-action settings keyed by action name. Each entry acceptsenabled(defaulttrue) anddeferLoading(defaultfalse, for tool search).
Tool calls and results of the toolset are serialized with toolset_name when
you pass the message history back. Keep the toolset tool in tools on
follow-up requests, or the provider falls back to the toolsetName provider
metadata on the tool calls of previous responses. Do not declare the toolset
together with an older computer_* tool.
Parameters:
action('key' | 'type' | 'mouse_move' | 'left_click' | 'left_click_drag' | 'right_click' | 'middle_click' | 'double_click' | 'screenshot' | 'cursor_position' | 'zoom'): The action to perform. For versioned computer tools, thezoomaction is only available withcomputer_20251124. It is also available withcomputerToolset_20260801, where it is enabled by default.coordinate(number[], optional): Required formouse_moveandleft_click_dragactions. Specifies the (x, y) coordinates.text(string, optional): Required fortypeandkeyactions.region(number[], optional): Required forzoomaction. Specifies[x1, y1, x2, y2]coordinates for the area to inspect.displayWidthPx(number): The width of the display in pixels.displayHeightPx(number): The height of the display in pixels.displayNumber(number, optional): The display number for X11 environments.enableZoom(boolean, optional): Enable the zoom action. Only available withcomputer_20251124. Default:false.
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:
Web search must be enabled in your organization's Console settings.
Sources and Citations
result.sources and source stream events expose web-search results. Inline
references are attached separately to generated text as part.citations, and
to streamed text as text-end.citations. This shared citation representation
includes the referenced URL or document and its supporting passage. If web-search
results are unavailable, web citations also provide source references for
compatibility. Indexed document references retain their existing document source
parts when the original prompt supplies document information.
Raw citations remain available as
text.providerMetadata.anthropic.citations, including on streamed text-end
events. Import AnthropicTextProviderMetadata and AnthropicCitation for typed
access. AnthropicCitation is a union of web_search_result_location,
search_result_location, page_location, char_location, and
content_block_location. Document page, character, and block locations refer to
the source document, rather than the generated answer.
Configuration Options
Use webSearch_20260318 for dynamic filtering and response inclusion control.
The older webSearch_20260209 and webSearch_20250305 versions remain
available for applications that need their earlier behavior.
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.
-
responseInclusion 'full' | 'excluded'
Available with
webSearch_20260318. Controls whether search result blocks consumed by a completed code execution call in the same turn are included in the API response. The default is'full'. Use'excluded'to omit those nested call/result pairs and reduce output token usage.
responseInclusion: 'excluded' only omits results consumed by code execution
calls that complete in the same turn. Results from direct calls and from code
execution calls that pause before completing are always returned in full so
they can be sent back on the next turn.
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:
Advisor Tool
Anthropic provides a provider-executed advisor tool that lets an executor model consult a stronger advisor model during generation. This is useful for long-running coding, research, and agentic tasks where a stronger model can provide planning or course correction while the main response is generated by a faster or lower-cost model.
You can enable the advisor tool using anthropic.tools.advisor_20260301:
The AI SDK automatically sends the required advisor-tool-2026-03-01 beta
header when you include this tool.
Configuration Options
The advisor tool supports the following configuration options:
-
model string
Required. The advisor model ID, such as
claude-opus-4-8. The advisor model must be at least as capable as the executor model. -
maxUses number
Optional. Maximum number of advisor calls allowed in a single request. Once the executor reaches this cap, further advisor calls return an
advisor_tool_result_errorwitherrorCode: 'max_uses_exceeded', and the executor continues without further advice. -
maxTokens number
Optional. Maximum number of tokens the advisor can generate per call, including thinking and text. The minimum is
1024, and Anthropic recommends starting with2048. This limit applies independently to each advisor call and is separate from the request-levelmaxOutputTokens, which controls executor output. Values above the selected advisor model's output limit result in an Anthropic400error. When configured, successful advisor results includestopReason; check forstopReason === 'max_tokens'to detect truncated advice. The field is available on both plaintext and redacted advisor result variants and is preserved across follow-up turns. -
caching object
Optional. Enables prompt caching for the advisor's own transcript across calls within a conversation. Use
{ type: 'ephemeral', ttl: '5m' | '1h' }. This is most useful for longer agent loops where you expect at least three advisor calls.
How It Works
The advisor tool is provider-executed, so you do not provide an execute function. When the executor model calls advisor, Anthropic runs a separate server-side inference with the advisor model. The advisor sees the transcript and returns guidance to the executor, which then continues generating within the same request.
The advisor tool input is always empty. Anthropic constructs the advisor's view from the full transcript automatically.
For multi-turn conversations, pass the full assistant content, including advisor tool results, back on subsequent turns. If you remove the advisor tool from a later request, also remove previous advisor tool results from the message history; otherwise the Anthropic API returns an invalid request error.
Streaming
Advisor sub-inferences do not stream. When using streamText, the executor stream pauses while the advisor runs, then the advisor result arrives as a single tool result before executor output resumes.
Tool Search
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
Using Regex Search
For more precise tool matching, you can use the regex variant:
Claude will construct regex patterns like weather|temperature|forecast to find matching tools.
Custom Tool Search
You can implement your own tool search logic (e.g., using embeddings or semantic search) by returning tool-reference content blocks via toModelOutput:
This sends tool_reference blocks to Anthropic, which loads the corresponding deferred tool schemas into Claude's context.
Mid-Conversation System Controls
With claude-sonnet-5-5, claude-opus-5-5, and claude-fable-5-1, use an
empty system message to change effort from the next user turn. A separate
system message containing text can provide a turn-scoped reminder with
clearAt: 'next_user_message': its text applies until the next user message. Keep the reminder after a user turn and
before an assistant turn, or at the end of the messages array.
The provider adds the required
mid-conversation-system-clear-at-2026-08-21 and
mid-conversation-output-config-2026-07-01 beta headers automatically.
An effort-only system message can use an empty content array at the API level;
in an AI SDK prompt, set content: ''. The provider serializes it as
content: []. These effort-only messages can appear first or later in the
conversation, including consecutively. The provider preserves their order. The new
level applies from the next user turn until a later message changes it.
Initial instruction text is sent in the top-level system field.
Consecutive inline system messages are evaluated as a group. If an effort-only message is adjacent to a system message containing text, the whole group must follow Anthropic's content placement rules.
Turn-scoped messages with clearAt support text only. Put effort updates in a
separate message, as in the example above.
clearAt applies only to mid-conversation system messages. On initial system
messages, the provider ignores clearAt and effort attached to non-empty
content and emits a warning. Use an empty system message for an initial
per-message effort setting, or the top-level effort provider option to
configure effort for the full request.
Mid-Conversation Tool Changes
With claude-opus-4-8, you can add or remove tools between turns of a conversation without invalidating the prompt cache. Attach toolChanges to a system message that appears mid-conversation (right before an assistant message or at the end of the messages). The required mid-conversation-tool-changes-2026-07-01 beta header is added automatically.
Tools referenced by a tool_addition must be declared in the tools option — typically with deferLoading: true, so they are not loaded into context until the addition surfaces them. A tool_removal removes a previously available tool from the conversation going forward.
To change a tool's definition, remove the old definition at the end of one request, then carry the conversation forward with the updated definition in tools on the next request.
Tool changes are not supported on the initial system message. The AI SDK
ignores them there and emits a warning — configure the initial tool set via
the tools option instead.
MCP Connectors
Anthropic supports connecting to MCP servers as part of their execution.
You can enable this feature with the mcpServers provider option:
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.
-
useCache boolean
Available with
webFetch_20260318. Set tofalseto bypass cached content and fetch fresh content. The default istrue; disabling the cache can increase latency. -
responseInclusion 'full' | 'excluded'
Available with
webFetch_20260318. Controls whether fetch result blocks consumed by a completed code execution call in the same turn are included in the API response. The default is'full'. Direct results and results from code execution calls that pause before completing are always returned in full.
Error Handling
Web search errors are handled differently depending on whether you're using streaming or non-streaming:
Non-streaming (generateText):
Web search errors throw exceptions that you can catch:
Streaming (streamText):
Web search errors are delivered as error parts in the stream:
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:
Three versions are available: codeExecution_20260120 (recommended, does not
require a beta header, supports Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, and
Opus 4.5), codeExecution_20250825 (supports Python and Bash with enhanced
file operations), and codeExecution_20250522 (supports Bash only).
Uploading Files for Code Execution
You can upload files via the Files API and make them available inside the code execution sandbox. First upload the file, then reference it in your message using containerUpload: true in the provider options:
Supported file types include CSV, Excel, JSON, XML, images (JPEG, PNG, GIF, WebP), and text files.
Error Handling
Code execution errors are handled differently depending on whether you're using streaming or non-streaming:
Non-streaming (generateText):
Code execution errors are delivered as tool result parts in the response:
Streaming (streamText):
Code execution errors are delivered as error parts in the stream:
Programmatic Tool Calling
Programmatic Tool Calling allows Claude to write code that calls your tools programmatically within a code execution container, rather than requiring round trips through the model for each tool invocation. This reduces latency for multi-tool workflows and decreases token consumption.
To enable programmatic tool calling, use the allowedCallers provider option on tools that you want to be callable from within code execution:
In this flow:
- Claude writes Python code that calls your
getWeathertool multiple times in parallel - The SDK automatically executes your tool and returns results to the code execution container
- Claude processes the results in code and generates the final response
Programmatic tool calling requires claude-sonnet-4-6, claude-sonnet-4-5,
claude-opus-4-6, or claude-opus-4-5 models and uses the
code_execution_20260120 or code_execution_20250825 tool.
Pruning Programmatic Tool History
When pruning removes a code execution call but keeps a tool call that references
it through caller.toolId, the provider omits the dangling caller metadata from
history before a subsequent user message and emits a warning. The retained tool
calls and results are still sent to Anthropic; the removed code execution history
is not restored.
Tool-result messages do not end a turn. Caller metadata in an active tool continuation is preserved, so keep the source code execution call when returning programmatic tool results.
Container Persistence
When using programmatic tool calling across multiple steps, you need to preserve the container ID between steps using prepareStep. You can use the forwardAnthropicContainerIdFromLastStep helper function to do this automatically. The container ID is available in providerMetadata.anthropic.container.id after each step completes.
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:
- Enable the code execution tool
- Specify the container with skills in
providerOptions
Custom Skills
You can also use custom skills by specifying type: 'custom':
Skills use progressive context loading and execute within a sandboxed container with code execution capabilities.
PDF support
Anthropic Claude models support reading PDF files.
You can pass PDF files as part of the message content using the file type:
Option 1: URL-based PDF document
Option 2: Base64-encoded PDF document
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
| Model | Image Input | Object Generation | Tool Usage | Computer Use | Web Search | Tool Search | Compaction |
|---|---|---|---|---|---|---|---|
claude-opus-5-5 | |||||||
claude-opus-5 | |||||||
claude-sonnet-5-5 | |||||||
claude-sonnet-5 | |||||||
claude-haiku-5-5 | |||||||
claude-fable-5-1 | |||||||
claude-fable-5 | |||||||
claude-opus-4-8 | |||||||
claude-opus-4-7 | |||||||
claude-opus-4-6 | |||||||
claude-sonnet-4-6 | |||||||
claude-opus-4-5 | |||||||
claude-haiku-4-5 | |||||||
claude-sonnet-4-5 | |||||||
claude-opus-4-1 | |||||||
claude-opus-4-0 | |||||||
claude-sonnet-4-0 |
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.
Decision Models
Create an experimental decision model with anthropic.decisionModel(modelId).
It uses the Messages API's structured-output support for Choice, Score, and Boolean
questions. claude-haiku-4-5-20251001 is one supported model.
Choice labels are preserved exactly, and Scores are finite fractional positions on the ordered rubric. Anthropic does not support numeric bounds in its native output schema, so the adapter describes them in the prompt and validates them after parsing. Invalid answers, refusals, and truncation fail the entire call.
The adapter returns no probability distributions for Choice or Score. Boolean
answers contain prompted estimates of P(true), validated to be finite and in
[0, 1]. These estimates are not guaranteed to be calibrated. Apply thresholds
in application code, for example answers.requestsRefund.probability >= 0.5.
It respects
createAnthropic configuration and forwards providerOptions.anthropic, including
structuredOutputMode. Supported models use native structured output by default;
jsonTool uses the existing Messages JSON-tool fallback. Usage, warnings,
response information, and provider metadata are preserved.
See Decisions.
Decision models can also be accessed through customProvider aliases or
createProviderRegistry().decisionModel('provider:model'). Direct string IDs
use Gateway by default, or an explicitly configured default provider with an
decisionModel method. See
model aliases and registries.