UNPKG

@ai-sdk/anthropic

Version:

The **[Anthropic provider](https://ai-sdk.dev/providers/ai-sdk-providers/anthropic)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post).

1,497 lines (1,483 loc) 217 kB
// src/anthropic-provider.ts import { InvalidArgumentError, NoSuchModelError } from "@ai-sdk/provider"; import { generateId as generateId2, loadApiKey, loadOptionalSetting, withoutTrailingSlash, withUserAgentSuffix } from "@ai-sdk/provider-utils"; // src/version.ts var VERSION = true ? "3.0.78" : "0.0.0-test"; // src/anthropic-messages-language-model.ts import { APICallError } from "@ai-sdk/provider"; import { combineHeaders, createEventSourceResponseHandler, createJsonResponseHandler, createToolNameMapping, generateId, parseProviderOptions as parseProviderOptions2, postJsonToApi, resolve } from "@ai-sdk/provider-utils"; // src/anthropic-error.ts import { createJsonErrorResponseHandler, lazySchema, zodSchema } from "@ai-sdk/provider-utils"; import { z } from "zod/v4"; var anthropicErrorDataSchema = lazySchema( () => zodSchema( z.object({ type: z.literal("error"), error: z.object({ type: z.string(), message: z.string() }) }) ) ); var anthropicFailedResponseHandler = createJsonErrorResponseHandler({ errorSchema: anthropicErrorDataSchema, errorToMessage: (data) => data.error.message }); // src/anthropic-messages-api.ts import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils"; import { z as z2 } from "zod/v4"; var anthropicMessagesResponseSchema = lazySchema2( () => zodSchema2( z2.object({ type: z2.literal("message"), id: z2.string().nullish(), model: z2.string().nullish(), content: z2.array( z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("text"), text: z2.string(), citations: z2.array( z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("web_search_result_location"), cited_text: z2.string(), url: z2.string(), title: z2.string(), encrypted_index: z2.string() }), z2.object({ type: z2.literal("page_location"), cited_text: z2.string(), document_index: z2.number(), document_title: z2.string().nullable(), start_page_number: z2.number(), end_page_number: z2.number() }), z2.object({ type: z2.literal("char_location"), cited_text: z2.string(), document_index: z2.number(), document_title: z2.string().nullable(), start_char_index: z2.number(), end_char_index: z2.number() }) ]) ).optional() }), z2.object({ type: z2.literal("thinking"), thinking: z2.string(), signature: z2.string() }), z2.object({ type: z2.literal("redacted_thinking"), data: z2.string() }), z2.object({ type: z2.literal("compaction"), content: z2.string() }), z2.object({ type: z2.literal("tool_use"), id: z2.string(), name: z2.string(), input: z2.unknown(), // Programmatic tool calling: caller info when triggered from code execution caller: z2.union([ z2.object({ type: z2.literal("code_execution_20250825"), tool_id: z2.string() }), z2.object({ type: z2.literal("code_execution_20260120"), tool_id: z2.string() }), z2.object({ type: z2.literal("direct") }) ]).optional() }), z2.object({ type: z2.literal("server_tool_use"), id: z2.string(), name: z2.string(), input: z2.record(z2.string(), z2.unknown()).nullish(), caller: z2.union([ z2.object({ type: z2.literal("code_execution_20260120"), tool_id: z2.string() }), z2.object({ type: z2.literal("direct") }) ]).optional() }), z2.object({ type: z2.literal("mcp_tool_use"), id: z2.string(), name: z2.string(), input: z2.unknown(), server_name: z2.string() }), z2.object({ type: z2.literal("mcp_tool_result"), tool_use_id: z2.string(), is_error: z2.boolean(), content: z2.array( z2.union([ z2.string(), z2.object({ type: z2.literal("text"), text: z2.string() }) ]) ) }), z2.object({ type: z2.literal("web_fetch_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("web_fetch_result"), url: z2.string(), retrieved_at: z2.string(), content: z2.object({ type: z2.literal("document"), title: z2.string().nullable(), citations: z2.object({ enabled: z2.boolean() }).optional(), source: z2.union([ z2.object({ type: z2.literal("base64"), media_type: z2.literal("application/pdf"), data: z2.string() }), z2.object({ type: z2.literal("text"), media_type: z2.literal("text/plain"), data: z2.string() }) ]) }) }), z2.object({ type: z2.literal("web_fetch_tool_result_error"), error_code: z2.string() }) ]) }), z2.object({ type: z2.literal("web_search_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.array( z2.object({ type: z2.literal("web_search_result"), url: z2.string(), title: z2.string(), encrypted_content: z2.string(), page_age: z2.string().nullish() }) ), z2.object({ type: z2.literal("web_search_tool_result_error"), error_code: z2.string() }) ]) }), // code execution results for code_execution_20250522 tool: z2.object({ type: z2.literal("code_execution_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("code_execution_result"), stdout: z2.string(), stderr: z2.string(), return_code: z2.number(), content: z2.array( z2.object({ type: z2.literal("code_execution_output"), file_id: z2.string() }) ).optional().default([]) }), z2.object({ type: z2.literal("encrypted_code_execution_result"), encrypted_stdout: z2.string(), stderr: z2.string(), return_code: z2.number(), content: z2.array( z2.object({ type: z2.literal("code_execution_output"), file_id: z2.string() }) ).optional().default([]) }), z2.object({ type: z2.literal("code_execution_tool_result_error"), error_code: z2.string() }) ]) }), // bash code execution results for code_execution_20250825 tool: z2.object({ type: z2.literal("bash_code_execution_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("bash_code_execution_result"), content: z2.array( z2.object({ type: z2.literal("bash_code_execution_output"), file_id: z2.string() }) ), stdout: z2.string(), stderr: z2.string(), return_code: z2.number() }), z2.object({ type: z2.literal("bash_code_execution_tool_result_error"), error_code: z2.string() }) ]) }), // text editor code execution results for code_execution_20250825 tool: z2.object({ type: z2.literal("text_editor_code_execution_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("text_editor_code_execution_tool_result_error"), error_code: z2.string() }), z2.object({ type: z2.literal("text_editor_code_execution_view_result"), content: z2.string(), file_type: z2.string(), num_lines: z2.number().nullable(), start_line: z2.number().nullable(), total_lines: z2.number().nullable() }), z2.object({ type: z2.literal("text_editor_code_execution_create_result"), is_file_update: z2.boolean() }), z2.object({ type: z2.literal( "text_editor_code_execution_str_replace_result" ), lines: z2.array(z2.string()).nullable(), new_lines: z2.number().nullable(), new_start: z2.number().nullable(), old_lines: z2.number().nullable(), old_start: z2.number().nullable() }) ]) }), // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119: z2.object({ type: z2.literal("tool_search_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("tool_search_tool_search_result"), tool_references: z2.array( z2.object({ type: z2.literal("tool_reference"), tool_name: z2.string() }) ) }), z2.object({ type: z2.literal("tool_search_tool_result_error"), error_code: z2.string() }) ]) }), // advisor results for advisor_20260301: z2.object({ type: z2.literal("advisor_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("advisor_result"), text: z2.string() }), z2.object({ type: z2.literal("advisor_redacted_result"), encrypted_content: z2.string() }), z2.object({ type: z2.literal("advisor_tool_result_error"), error_code: z2.string() }) ]) }) ]) ), stop_reason: z2.string().nullish(), stop_sequence: z2.string().nullish(), usage: z2.looseObject({ input_tokens: z2.number(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish(), iterations: z2.array( z2.union([ z2.object({ type: z2.union([z2.literal("compaction"), z2.literal("message")]), input_tokens: z2.number(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish() }), z2.object({ type: z2.literal("advisor_message"), model: z2.string(), input_tokens: z2.number(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish() }) ]) ).nullish() }), container: z2.object({ expires_at: z2.string(), id: z2.string(), skills: z2.array( z2.object({ type: z2.union([z2.literal("anthropic"), z2.literal("custom")]), skill_id: z2.string(), version: z2.string() }) ).nullish() }).nullish(), context_management: z2.object({ applied_edits: z2.array( z2.union([ z2.object({ type: z2.literal("clear_tool_uses_20250919"), cleared_tool_uses: z2.number(), cleared_input_tokens: z2.number() }), z2.object({ type: z2.literal("clear_thinking_20251015"), cleared_thinking_turns: z2.number(), cleared_input_tokens: z2.number() }), z2.object({ type: z2.literal("compact_20260112") }) ]) ) }).nullish() }) ) ); var anthropicMessagesChunkSchema = lazySchema2( () => zodSchema2( z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("message_start"), message: z2.object({ id: z2.string().nullish(), model: z2.string().nullish(), role: z2.string().nullish(), usage: z2.looseObject({ input_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish() }), // Programmatic tool calling: content may be pre-populated for deferred tool calls content: z2.array( z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("tool_use"), id: z2.string(), name: z2.string(), input: z2.unknown(), caller: z2.union([ z2.object({ type: z2.literal("code_execution_20250825"), tool_id: z2.string() }), z2.object({ type: z2.literal("code_execution_20260120"), tool_id: z2.string() }), z2.object({ type: z2.literal("direct") }) ]).optional() }) ]) ).nullish(), stop_reason: z2.string().nullish(), container: z2.object({ expires_at: z2.string(), id: z2.string() }).nullish() }) }), z2.object({ type: z2.literal("content_block_start"), index: z2.number(), content_block: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("text"), text: z2.string() }), z2.object({ type: z2.literal("thinking"), thinking: z2.string() }), z2.object({ type: z2.literal("tool_use"), id: z2.string(), name: z2.string(), // Programmatic tool calling: input may be present directly for deferred tool calls input: z2.record(z2.string(), z2.unknown()).optional(), // Programmatic tool calling: caller info when triggered from code execution caller: z2.union([ z2.object({ type: z2.literal("code_execution_20250825"), tool_id: z2.string() }), z2.object({ type: z2.literal("code_execution_20260120"), tool_id: z2.string() }), z2.object({ type: z2.literal("direct") }) ]).optional() }), z2.object({ type: z2.literal("redacted_thinking"), data: z2.string() }), z2.object({ type: z2.literal("compaction"), content: z2.string().nullish() }), z2.object({ type: z2.literal("server_tool_use"), id: z2.string(), name: z2.string(), input: z2.record(z2.string(), z2.unknown()).nullish(), caller: z2.union([ z2.object({ type: z2.literal("code_execution_20260120"), tool_id: z2.string() }), z2.object({ type: z2.literal("direct") }) ]).optional() }), z2.object({ type: z2.literal("mcp_tool_use"), id: z2.string(), name: z2.string(), input: z2.unknown(), server_name: z2.string() }), z2.object({ type: z2.literal("mcp_tool_result"), tool_use_id: z2.string(), is_error: z2.boolean(), content: z2.array( z2.union([ z2.string(), z2.object({ type: z2.literal("text"), text: z2.string() }) ]) ) }), z2.object({ type: z2.literal("web_fetch_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("web_fetch_result"), url: z2.string(), retrieved_at: z2.string(), content: z2.object({ type: z2.literal("document"), title: z2.string().nullable(), citations: z2.object({ enabled: z2.boolean() }).optional(), source: z2.union([ z2.object({ type: z2.literal("base64"), media_type: z2.literal("application/pdf"), data: z2.string() }), z2.object({ type: z2.literal("text"), media_type: z2.literal("text/plain"), data: z2.string() }) ]) }) }), z2.object({ type: z2.literal("web_fetch_tool_result_error"), error_code: z2.string() }) ]) }), z2.object({ type: z2.literal("web_search_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.array( z2.object({ type: z2.literal("web_search_result"), url: z2.string(), title: z2.string(), encrypted_content: z2.string(), page_age: z2.string().nullish() }) ), z2.object({ type: z2.literal("web_search_tool_result_error"), error_code: z2.string() }) ]) }), // code execution results for code_execution_20250522 tool: z2.object({ type: z2.literal("code_execution_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("code_execution_result"), stdout: z2.string(), stderr: z2.string(), return_code: z2.number(), content: z2.array( z2.object({ type: z2.literal("code_execution_output"), file_id: z2.string() }) ).optional().default([]) }), z2.object({ type: z2.literal("encrypted_code_execution_result"), encrypted_stdout: z2.string(), stderr: z2.string(), return_code: z2.number(), content: z2.array( z2.object({ type: z2.literal("code_execution_output"), file_id: z2.string() }) ).optional().default([]) }), z2.object({ type: z2.literal("code_execution_tool_result_error"), error_code: z2.string() }) ]) }), // bash code execution results for code_execution_20250825 tool: z2.object({ type: z2.literal("bash_code_execution_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("bash_code_execution_result"), content: z2.array( z2.object({ type: z2.literal("bash_code_execution_output"), file_id: z2.string() }) ), stdout: z2.string(), stderr: z2.string(), return_code: z2.number() }), z2.object({ type: z2.literal("bash_code_execution_tool_result_error"), error_code: z2.string() }) ]) }), // text editor code execution results for code_execution_20250825 tool: z2.object({ type: z2.literal("text_editor_code_execution_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("text_editor_code_execution_tool_result_error"), error_code: z2.string() }), z2.object({ type: z2.literal("text_editor_code_execution_view_result"), content: z2.string(), file_type: z2.string(), num_lines: z2.number().nullable(), start_line: z2.number().nullable(), total_lines: z2.number().nullable() }), z2.object({ type: z2.literal("text_editor_code_execution_create_result"), is_file_update: z2.boolean() }), z2.object({ type: z2.literal( "text_editor_code_execution_str_replace_result" ), lines: z2.array(z2.string()).nullable(), new_lines: z2.number().nullable(), new_start: z2.number().nullable(), old_lines: z2.number().nullable(), old_start: z2.number().nullable() }) ]) }), // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119: z2.object({ type: z2.literal("tool_search_tool_result"), tool_use_id: z2.string(), content: z2.union([ z2.object({ type: z2.literal("tool_search_tool_search_result"), tool_references: z2.array( z2.object({ type: z2.literal("tool_reference"), tool_name: z2.string() }) ) }), z2.object({ type: z2.literal("tool_search_tool_result_error"), error_code: z2.string() }) ]) }), // advisor results for advisor_20260301: z2.object({ type: z2.literal("advisor_tool_result"), tool_use_id: z2.string(), content: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("advisor_result"), text: z2.string() }), z2.object({ type: z2.literal("advisor_redacted_result"), encrypted_content: z2.string() }), z2.object({ type: z2.literal("advisor_tool_result_error"), error_code: z2.string() }) ]) }) ]) }), z2.object({ type: z2.literal("content_block_delta"), index: z2.number(), delta: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("input_json_delta"), partial_json: z2.string() }), z2.object({ type: z2.literal("text_delta"), text: z2.string() }), z2.object({ type: z2.literal("thinking_delta"), thinking: z2.string() }), z2.object({ type: z2.literal("signature_delta"), signature: z2.string() }), z2.object({ type: z2.literal("compaction_delta"), content: z2.string().nullish() }), z2.object({ type: z2.literal("citations_delta"), citation: z2.discriminatedUnion("type", [ z2.object({ type: z2.literal("web_search_result_location"), cited_text: z2.string(), url: z2.string(), title: z2.string(), encrypted_index: z2.string() }), z2.object({ type: z2.literal("page_location"), cited_text: z2.string(), document_index: z2.number(), document_title: z2.string().nullable(), start_page_number: z2.number(), end_page_number: z2.number() }), z2.object({ type: z2.literal("char_location"), cited_text: z2.string(), document_index: z2.number(), document_title: z2.string().nullable(), start_char_index: z2.number(), end_char_index: z2.number() }) ]) }) ]) }), z2.object({ type: z2.literal("content_block_stop"), index: z2.number() }), z2.object({ type: z2.literal("error"), error: z2.object({ type: z2.string(), message: z2.string() }) }), z2.object({ type: z2.literal("message_delta"), delta: z2.object({ stop_reason: z2.string().nullish(), stop_sequence: z2.string().nullish(), container: z2.object({ expires_at: z2.string(), id: z2.string(), skills: z2.array( z2.object({ type: z2.union([ z2.literal("anthropic"), z2.literal("custom") ]), skill_id: z2.string(), version: z2.string() }) ).nullish() }).nullish() }), usage: z2.looseObject({ input_tokens: z2.number().nullish(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish(), iterations: z2.array( z2.union([ z2.object({ type: z2.union([ z2.literal("compaction"), z2.literal("message") ]), input_tokens: z2.number(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish() }), z2.object({ type: z2.literal("advisor_message"), model: z2.string(), input_tokens: z2.number(), output_tokens: z2.number(), cache_creation_input_tokens: z2.number().nullish(), cache_read_input_tokens: z2.number().nullish() }) ]) ).nullish() }), context_management: z2.object({ applied_edits: z2.array( z2.union([ z2.object({ type: z2.literal("clear_tool_uses_20250919"), cleared_tool_uses: z2.number(), cleared_input_tokens: z2.number() }), z2.object({ type: z2.literal("clear_thinking_20251015"), cleared_thinking_turns: z2.number(), cleared_input_tokens: z2.number() }), z2.object({ type: z2.literal("compact_20260112") }) ]) ) }).nullish() }), z2.object({ type: z2.literal("message_stop") }), z2.object({ type: z2.literal("ping") }) ]) ) ); var anthropicReasoningMetadataSchema = lazySchema2( () => zodSchema2( z2.object({ signature: z2.string().optional(), redactedData: z2.string().optional() }) ) ); // src/anthropic-messages-options.ts import { z as z3 } from "zod/v4"; var anthropicFilePartProviderOptions = z3.object({ /** * Citation configuration for this document. * When enabled, this document will generate citations in the response. */ citations: z3.object({ /** * Enable citations for this document */ enabled: z3.boolean() }).optional(), /** * Custom title for the document. * If not provided, the filename will be used. */ title: z3.string().optional(), /** * Context about the document that will be passed to the model * but not used towards cited content. * Useful for storing document metadata as text or stringified JSON. */ context: z3.string().optional() }); var anthropicLanguageModelOptions = z3.object({ /** * Whether to send reasoning to the model. * * This allows you to deactivate reasoning inputs for models that do not support them. */ sendReasoning: z3.boolean().optional(), /** * Determines how structured outputs are generated. * * - `outputFormat`: Use the `output_config.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 use 'jsonTool' (default). */ structuredOutputMode: z3.enum(["outputFormat", "jsonTool", "auto"]).optional(), /** * Configuration for enabling Claude's extended thinking. * * When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer. * Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit. */ thinking: z3.discriminatedUnion("type", [ z3.object({ /** for Sonnet 4.6, Opus 4.6, and newer models */ type: z3.literal("adaptive"), /** * Controls whether thinking content is included in the response. * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+). * - `"summarized"`: Thinking content is returned. Required to see reasoning output. */ display: z3.enum(["omitted", "summarized"]).optional() }), z3.object({ /** for models before Opus 4.6, except Sonnet 4.6 still supports it */ type: z3.literal("enabled"), budgetTokens: z3.number().optional() }), z3.object({ type: z3.literal("disabled") }) ]).optional(), /** * Whether to disable parallel function calling during tool use. Default is false. * When set to true, Claude will use at most one tool per response. */ disableParallelToolUse: z3.boolean().optional(), /** * Cache control settings for this message. * See https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching */ cacheControl: z3.object({ type: z3.literal("ephemeral"), ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional() }).optional(), /** * Metadata to include with the request. * * See https://platform.claude.com/docs/en/api/messages/create for details. */ metadata: z3.object({ /** * An external identifier for the user associated with the request. * * Should be a UUID, hash value, or other opaque identifier. * Must not contain PII (name, email, phone number, etc.). */ userId: z3.string().optional() }).optional(), /** * MCP servers to be utilized in this request. */ mcpServers: z3.array( z3.object({ type: z3.literal("url"), name: z3.string(), url: z3.string(), authorizationToken: z3.string().nullish(), toolConfiguration: z3.object({ enabled: z3.boolean().nullish(), allowedTools: z3.array(z3.string()).nullish() }).nullish() }) ).optional(), /** * Agent Skills configuration. Skills enable Claude to perform specialized tasks * like document processing (PPTX, DOCX, PDF, XLSX) and data analysis. * Requires code execution tool to be enabled. */ container: z3.object({ id: z3.string().optional(), skills: z3.array( z3.object({ type: z3.union([z3.literal("anthropic"), z3.literal("custom")]), skillId: z3.string(), version: z3.string().optional() }) ).optional() }).optional(), /** * Whether to enable fine-grained (eager) streaming of tool call inputs * and structured outputs for every function tool in the request. When * true (the default), each function tool receives a default of * `eager_input_streaming: true` unless it explicitly sets * `providerOptions.anthropic.eagerInputStreaming`. * * @default true */ toolStreaming: z3.boolean().optional(), /** * @default 'high' */ effort: z3.enum(["low", "medium", "high", "xhigh", "max"]).optional(), /** * Task budget for agentic turns. Informs the model of the total token budget * available for the current task, allowing it to prioritize work and wind down * gracefully as the budget is consumed. * * Advisory only — does not enforce a hard token limit. */ taskBudget: z3.object({ type: z3.literal("tokens"), total: z3.number().int().min(2e4), remaining: z3.number().int().min(0).optional() }).optional(), /** * Enable fast mode for faster inference (2.5x faster output token speeds). * Only supported with claude-opus-4-6. */ speed: z3.enum(["fast", "standard"]).optional(), /** * Controls where model inference runs for this request. * * - `"global"`: Inference may run in any available geography (default). * - `"us"`: Inference runs only in US-based infrastructure. * * See https://platform.claude.com/docs/en/build-with-claude/data-residency */ inferenceGeo: z3.enum(["us", "global"]).optional(), /** * A set of beta features to enable. * Allow a provider to receive the full `betas` set if it needs it. */ anthropicBeta: z3.array(z3.string()).optional(), contextManagement: z3.object({ edits: z3.array( z3.discriminatedUnion("type", [ z3.object({ type: z3.literal("clear_tool_uses_20250919"), trigger: z3.discriminatedUnion("type", [ z3.object({ type: z3.literal("input_tokens"), value: z3.number() }), z3.object({ type: z3.literal("tool_uses"), value: z3.number() }) ]).optional(), keep: z3.object({ type: z3.literal("tool_uses"), value: z3.number() }).optional(), clearAtLeast: z3.object({ type: z3.literal("input_tokens"), value: z3.number() }).optional(), clearToolInputs: z3.boolean().optional(), excludeTools: z3.array(z3.string()).optional() }), z3.object({ type: z3.literal("clear_thinking_20251015"), keep: z3.union([ z3.literal("all"), z3.object({ type: z3.literal("thinking_turns"), value: z3.number() }) ]).optional() }), z3.object({ type: z3.literal("compact_20260112"), trigger: z3.object({ type: z3.literal("input_tokens"), value: z3.number() }).optional(), pauseAfterCompaction: z3.boolean().optional(), instructions: z3.string().optional() }) ]) ) }).optional() }); // src/anthropic-prepare-tools.ts import { UnsupportedFunctionalityError } from "@ai-sdk/provider"; // src/get-cache-control.ts var MAX_CACHE_BREAKPOINTS = 4; function getCacheControl(providerMetadata) { var _a; const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic; const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control; return cacheControlValue; } var CacheControlValidator = class { constructor() { this.breakpointCount = 0; this.warnings = []; } getCacheControl(providerMetadata, context) { const cacheControlValue = getCacheControl(providerMetadata); if (!cacheControlValue) { return void 0; } if (!context.canCache) { this.warnings.push({ type: "unsupported", feature: "cache_control on non-cacheable context", details: `cache_control cannot be set on ${context.type}. It will be ignored.` }); return void 0; } this.breakpointCount++; if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) { this.warnings.push({ type: "unsupported", feature: "cacheControl breakpoint limit", details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.` }); return void 0; } return cacheControlValue; } getWarnings() { return this.warnings; } }; // src/tool/advisor_20260301.ts import { createProviderToolFactoryWithOutputSchema, lazySchema as lazySchema3, zodSchema as zodSchema3 } from "@ai-sdk/provider-utils"; import { z as z4 } from "zod/v4"; var advisor_20260301ArgsSchema = lazySchema3( () => zodSchema3( z4.object({ model: z4.string(), maxUses: z4.number().optional(), caching: z4.object({ type: z4.literal("ephemeral"), ttl: z4.union([z4.literal("5m"), z4.literal("1h")]) }).optional() }) ) ); var advisor_20260301OutputSchema = lazySchema3( () => zodSchema3( z4.discriminatedUnion("type", [ z4.object({ type: z4.literal("advisor_result"), text: z4.string() }), z4.object({ type: z4.literal("advisor_redacted_result"), encryptedContent: z4.string() }), z4.object({ type: z4.literal("advisor_tool_result_error"), errorCode: z4.string() }) ]) ) ); var advisor_20260301InputSchema = lazySchema3( () => zodSchema3(z4.object({}).strict()) ); var factory = createProviderToolFactoryWithOutputSchema({ id: "anthropic.advisor_20260301", inputSchema: advisor_20260301InputSchema, outputSchema: advisor_20260301OutputSchema, supportsDeferredResults: true }); var advisor_20260301 = (args) => { return factory(args); }; // src/tool/text-editor_20250728.ts import { createProviderToolFactory, lazySchema as lazySchema4, zodSchema as zodSchema4 } from "@ai-sdk/provider-utils"; import { z as z5 } from "zod/v4"; var textEditor_20250728ArgsSchema = lazySchema4( () => zodSchema4( z5.object({ maxCharacters: z5.number().optional() }) ) ); var textEditor_20250728InputSchema = lazySchema4( () => zodSchema4( z5.object({ command: z5.enum(["view", "create", "str_replace", "insert"]), path: z5.string(), file_text: z5.string().optional(), insert_line: z5.number().int().optional(), new_str: z5.string().optional(), insert_text: z5.string().optional(), old_str: z5.string().optional(), view_range: z5.array(z5.number().int()).optional() }) ) ); var factory2 = createProviderToolFactory({ id: "anthropic.text_editor_20250728", inputSchema: textEditor_20250728InputSchema }); var textEditor_20250728 = (args = {}) => { return factory2(args); }; // src/tool/web-search_20260209.ts import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema2, lazySchema as lazySchema5, zodSchema as zodSchema5 } from "@ai-sdk/provider-utils"; import { z as z6 } from "zod/v4"; var webSearch_20260209ArgsSchema = lazySchema5( () => zodSchema5( z6.object({ maxUses: z6.number().optional(), allowedDomains: z6.array(z6.string()).optional(), blockedDomains: z6.array(z6.string()).optional(), userLocation: z6.object({ type: z6.literal("approximate"), city: z6.string().optional(), region: z6.string().optional(), country: z6.string().optional(), timezone: z6.string().optional() }).optional() }) ) ); var webSearch_20260209OutputSchema = lazySchema5( () => zodSchema5( z6.array( z6.object({ url: z6.string(), title: z6.string().nullable(), pageAge: z6.string().nullable(), encryptedContent: z6.string(), type: z6.literal("web_search_result") }) ) ) ); var webSearch_20260209InputSchema = lazySchema5( () => zodSchema5( z6.object({ query: z6.string() }) ) ); var factory3 = createProviderToolFactoryWithOutputSchema2({ id: "anthropic.web_search_20260209", inputSchema: webSearch_20260209InputSchema, outputSchema: webSearch_20260209OutputSchema, supportsDeferredResults: true }); var webSearch_20260209 = (args = {}) => { return factory3(args); }; // src/tool/web-search_20250305.ts import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema3, lazySchema as lazySchema6, zodSchema as zodSchema6 } from "@ai-sdk/provider-utils"; import { z as z7 } from "zod/v4"; var webSearch_20250305ArgsSchema = lazySchema6( () => zodSchema6( z7.object({ maxUses: z7.number().optional(), allowedDomains: z7.array(z7.string()).optional(), blockedDomains: z7.array(z7.string()).optional(), userLocation: z7.object({ type: z7.literal("approximate"), city: z7.string().optional(), region: z7.string().optional(), country: z7.string().optional(), timezone: z7.string().optional() }).optional() }) ) ); var webSearch_20250305OutputSchema = lazySchema6( () => zodSchema6( z7.array( z7.object({ url: z7.string(), title: z7.string().nullable(), pageAge: z7.string().nullable(), encryptedContent: z7.string(), type: z7.literal("web_search_result") }) ) ) ); var webSearch_20250305InputSchema = lazySchema6( () => zodSchema6( z7.object({ query: z7.string() }) ) ); var factory4 = createProviderToolFactoryWithOutputSchema3({ id: "anthropic.web_search_20250305", inputSchema: webSearch_20250305InputSchema, outputSchema: webSearch_20250305OutputSchema, supportsDeferredResults: true }); var webSearch_20250305 = (args = {}) => { return factory4(args); }; // src/tool/web-fetch-20260209.ts import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema4, lazySchema as lazySchema7, zodSchema as zodSchema7 } from "@ai-sdk/provider-utils"; import { z as z8 } from "zod/v4"; var webFetch_20260209ArgsSchema = lazySchema7( () => zodSchema7( z8.object({ maxUses: z8.number().optional(), allowedDomains: z8.array(z8.string()).optional(), blockedDomains: z8.array(z8.string()).optional(), citations: z8.object({ enabled: z8.boolean() }).optional(), maxContentTokens: z8.number().optional() }) ) ); var webFetch_20260209OutputSchema = lazySchema7( () => zodSchema7( z8.object({ type: z8.literal("web_fetch_result"), url: z8.string(), content: z8.object({ type: z8.literal("document"), title: z8.string().nullable(), citations: z8.object({ enabled: z8.boolean() }).optional(), source: z8.union([ z8.object({ type: z8.literal("base64"), mediaType: z8.literal("application/pdf"), data: z8.string() }), z8.object({ type: z8.literal("text"), mediaType: z8.literal("text/plain"), data: z8.string() }) ]) }), retrievedAt: z8.string().nullable() }) ) ); var webFetch_20260209InputSchema = lazySchema7( () => zodSchema7( z8.object({ url: z8.string() }) ) ); var factory5 = createProviderToolFactoryWithOutputSchema4({ id: "anthropic.web_fetch_20260209", inputSchema: webFetch_20260209InputSchema, outputSchema: webFetch_20260209OutputSchema, supportsDeferredResults: true }); var webFetch_20260209 = (args = {}) => { return factory5(args); }; // src/tool/web-fetch-20250910.ts import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema5, lazySchema as lazySchema8, zodSchema as zodSchema8 } from "@ai-sdk/provider-utils"; import { z as z9 } from "zod/v4"; var webFetch_20250910ArgsSchema = lazySchema8( () => zodSchema8( z9.object({ maxUses: z9.number().optional(), allowedDomains: z9.array(z9.string()).optional(), blockedDomains: z9.array(z9.string()).optional(), citations: z9.object({ enabled: z9.boolean() }).optional(), maxContentTokens: z9.number().optional() }) ) ); var webFetch_20250910OutputSchema = lazySchema8( () => zodSchema8( z9.object({ type: z9.literal("web_fetch_result"), url: z9.string(), content: z9.object({ type: z9.literal("document"), title: z9.string().nullable(), citations: z9.object({ enabled: z9.boolean() }).optional(), source: z9.union([ z9.object({ type: z9.literal("base64"), mediaType: z9.literal("application/pdf"), data: z9.string() }), z9.object({ type: z9.literal("text"), mediaType: z9.literal("text/plain"), data: z9.string() }) ]) }), retrievedAt: z9.string().nullable() }) ) ); var webFetch_20250910InputSchema = lazySchema8( () => zodSchema8( z9.object({ url: z9.string() }) ) ); var factory6 = createProviderToolFactoryWithOutputSchema5({ id: "anthropic.web_fetch_20250910", inputSchema: webFetch_20250910InputSchema, outputSchema: webFetch_20250910OutputSchema, supportsDeferredResults: true }); var webFetch_20250910 = (args = {}) => { return factory6(args); }; // src/anthropic-prepare-tools.ts import { validateTypes } from "@ai-sdk/provider-utils"; async function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, supportsStructuredOutput, supportsStrictTools, defaultEagerInputStreaming = false }) { var _a, _b; tools = (tools == null ? void 0 : tools.length) ? tools : void 0; const toolWarnings = []; const betas = /* @__PURE__ */ new Set(); const validator = cacheControlValidator || new CacheControlValidator(); if (tools == null) { return { tools: void 0, toolChoice: void 0, toolWarnings, betas }; } const anthropicTools2 = []; for (const tool of tools) { switch (tool.type) { case "function": { const cacheControl = validator.getCacheControl(tool.providerOptions, { type: "tool definition", canCache: true }); const anthropicOptions = (_a = tool.providerOptions) == null ? void 0 : _a.anthropic; const eagerInputStreaming = (_b = anthropicOptions == null ? void 0 : anthropicOptions.eagerInputStreaming) != null ? _b : defaultEagerInputStreaming; const deferLoading = anthropicOptions == null ? void 0 : anthropicOptions.deferLoading; const allowedCallers = anthropicOptions == null ? void 0 : anthropicOptions.allowedCallers; if (!supportsStrictTools && tool.strict != null) { toolWarnings.push({ type: "unsupported", feature: "strict", details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.` }); } anthropicTools2.push({ name: tool.name, description: tool.description, input_schema: tool.inputSchema, cache_control: cacheControl, ...eagerInputStreaming ? { eager_input_streaming: true } : {}, ...supportsStrictTools === true && tool.strict != null ? { strict: tool.strict } : {}, ...deferLoading != null ? { defer_loading: deferLoading } : {}, ...allowedCallers != null ? { allowed_callers: allowedCallers } : {}, ...tool.inputExamples != null ? { input_examples: tool.inputExamples.map( (example) => example.input ) } : {} }); if (supportsStructuredOutput === true) { betas.add("structured-outputs-2025-11-13"); } if (tool.inputExamples != null || allo