@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,488 lines (1,476 loc) • 190 kB
JavaScript
// 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()
})
])
})
])
),
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.object({
type: z2.union([z2.literal("compaction"), z2.literal("message")]),
input_tokens: z2.number(),
output_tokens: z2.number()
})
).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()
})
])
})
])
}),
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.object({
type: z2.union([z2.literal("compaction"), z2.literal("message")]),
input_tokens: z2.number(),
output_tokens: z2.number()
})
).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")
}),
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(),
/**
* 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 tool streaming (and structured output streaming).
*
* When set to false, the model will return all tool calls and results
* at once after a delay.
*
* @default true
*/
toolStreaming: z3.boolean().optional(),
/**
* @default 'high'
*/
effort: z3.enum(["low", "medium", "high", "max"]).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(),
/**
* 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 anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.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/text-editor_20250728.ts
import { createProviderToolFactory } from "@ai-sdk/provider-utils";
import { z as z4 } from "zod/v4";
import { lazySchema as lazySchema3, zodSchema as zodSchema3 } from "@ai-sdk/provider-utils";
var textEditor_20250728ArgsSchema = lazySchema3(
() => zodSchema3(
z4.object({
maxCharacters: z4.number().optional()
})
)
);
var textEditor_20250728InputSchema = lazySchema3(
() => zodSchema3(
z4.object({
command: z4.enum(["view", "create", "str_replace", "insert"]),
path: z4.string(),
file_text: z4.string().optional(),
insert_line: z4.number().int().optional(),
new_str: z4.string().optional(),
insert_text: z4.string().optional(),
old_str: z4.string().optional(),
view_range: z4.array(z4.number().int()).optional()
})
)
);
var factory = createProviderToolFactory({
id: "anthropic.text_editor_20250728",
inputSchema: textEditor_20250728InputSchema
});
var textEditor_20250728 = (args = {}) => {
return factory(args);
};
// src/tool/web-search_20260209.ts
import {
createProviderToolFactoryWithOutputSchema,
lazySchema as lazySchema4,
zodSchema as zodSchema4
} from "@ai-sdk/provider-utils";
import { z as z5 } from "zod/v4";
var webSearch_20260209ArgsSchema = lazySchema4(
() => zodSchema4(
z5.object({
maxUses: z5.number().optional(),
allowedDomains: z5.array(z5.string()).optional(),
blockedDomains: z5.array(z5.string()).optional(),
userLocation: z5.object({
type: z5.literal("approximate"),
city: z5.string().optional(),
region: z5.string().optional(),
country: z5.string().optional(),
timezone: z5.string().optional()
}).optional()
})
)
);
var webSearch_20260209OutputSchema = lazySchema4(
() => zodSchema4(
z5.array(
z5.object({
url: z5.string(),
title: z5.string().nullable(),
pageAge: z5.string().nullable(),
encryptedContent: z5.string(),
type: z5.literal("web_search_result")
})
)
)
);
var webSearch_20260209InputSchema = lazySchema4(
() => zodSchema4(
z5.object({
query: z5.string()
})
)
);
var factory2 = createProviderToolFactoryWithOutputSchema({
id: "anthropic.web_search_20260209",
inputSchema: webSearch_20260209InputSchema,
outputSchema: webSearch_20260209OutputSchema,
supportsDeferredResults: true
});
var webSearch_20260209 = (args = {}) => {
return factory2(args);
};
// src/tool/web-search_20250305.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_20250305ArgsSchema = 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_20250305OutputSchema = 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_20250305InputSchema = lazySchema5(
() => zodSchema5(
z6.object({
query: z6.string()
})
)
);
var factory3 = createProviderToolFactoryWithOutputSchema2({
id: "anthropic.web_search_20250305",
inputSchema: webSearch_20250305InputSchema,
outputSchema: webSearch_20250305OutputSchema,
supportsDeferredResults: true
});
var webSearch_20250305 = (args = {}) => {
return factory3(args);
};
// src/tool/web-fetch-20260209.ts
import {
createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema3,
lazySchema as lazySchema6,
zodSchema as zodSchema6
} from "@ai-sdk/provider-utils";
import { z as z7 } from "zod/v4";
var webFetch_20260209ArgsSchema = lazySchema6(
() => zodSchema6(
z7.object({
maxUses: z7.number().optional(),
allowedDomains: z7.array(z7.string()).optional(),
blockedDomains: z7.array(z7.string()).optional(),
citations: z7.object({ enabled: z7.boolean() }).optional(),
maxContentTokens: z7.number().optional()
})
)
);
var webFetch_20260209OutputSchema = lazySchema6(
() => zodSchema6(
z7.object({
type: z7.literal("web_fetch_result"),
url: z7.string(),
content: z7.object({
type: z7.literal("document"),
title: z7.string().nullable(),
citations: z7.object({ enabled: z7.boolean() }).optional(),
source: z7.union([
z7.object({
type: z7.literal("base64"),
mediaType: z7.literal("application/pdf"),
data: z7.string()
}),
z7.object({
type: z7.literal("text"),
mediaType: z7.literal("text/plain"),
data: z7.string()
})
])
}),
retrievedAt: z7.string().nullable()
})
)
);
var webFetch_20260209InputSchema = lazySchema6(
() => zodSchema6(
z7.object({
url: z7.string()
})
)
);
var factory4 = createProviderToolFactoryWithOutputSchema3({
id: "anthropic.web_fetch_20260209",
inputSchema: webFetch_20260209InputSchema,
outputSchema: webFetch_20260209OutputSchema,
supportsDeferredResults: true
});
var webFetch_20260209 = (args = {}) => {
return factory4(args);
};
// src/tool/web-fetch-20250910.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_20250910ArgsSchema = 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_20250910OutputSchema = 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_20250910InputSchema = lazySchema7(
() => zodSchema7(
z8.object({
url: z8.string()
})
)
);
var factory5 = createProviderToolFactoryWithOutputSchema4({
id: "anthropic.web_fetch_20250910",
inputSchema: webFetch_20250910InputSchema,
outputSchema: webFetch_20250910OutputSchema,
supportsDeferredResults: true
});
var webFetch_20250910 = (args = {}) => {
return factory5(args);
};
// src/anthropic-prepare-tools.ts
import { validateTypes } from "@ai-sdk/provider-utils";
async function prepareTools({
tools,
toolChoice,
disableParallelToolUse,
cacheControlValidator,
supportsStructuredOutput
}) {
var _a;
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 = anthropicOptions == null ? void 0 : anthropicOptions.eagerInputStreaming;
const deferLoading = anthropicOptions == null ? void 0 : anthropicOptions.deferLoading;
const allowedCallers = anthropicOptions == null ? void 0 : anthropicOptions.allowedCallers;
anthropicTools2.push({
name: tool.name,
description: tool.description,
input_schema: tool.inputSchema,
cache_control: cacheControl,
...eagerInputStreaming ? { eager_input_streaming: true } : {},
...supportsStructuredOutput === 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 || allowedCallers != null) {
betas.add("advanced-tool-use-2025-11-20");
}
break;
}
case "provider": {
switch (tool.id) {
case "anthropic.code_execution_20250522": {
betas.add("code-execution-2025-05-22");
anthropicTools2.push({
type: "code_execution_20250522",
name: "code_execution",
cache_control: void 0
});
break;
}
case "anthropic.code_execution_20250825": {
betas.add("code-execution-2025-08-25");
anthropicTools2.push({
type: "code_execution_20250825",
name: "code_execution"
});
break;
}
case "anthropic.code_execution_20260120": {
anthropicTools2.push({
type: "code_execution_20260120",
name: "code_execution"
});
break;
}
case "anthropic.computer_20250124": {
betas.add("computer-use-2025-01-24");
anthropicTools2.push({
name: "computer",
type: "computer_20250124",
display_width_px: tool.args.displayWidthPx,
display_height_px: tool.args.displayHeightPx,
display_number: tool.args.displayNumber,
cache_control: void 0
});
break;
}
case "anthropic.computer_20251124": {
betas.add("computer-use-2025-11-24");
anthropicTools2.push({
name: "computer",
type: "computer_20251124",
display_width_px: tool.args.displayWidthPx,
display_height_px: tool.args.displayHeightPx,
display_number: tool.args.displayNumber,
enable_zoom: tool.args.enableZoom,
cache_control: void 0
});
break;
}
case "anthropic.computer_20241022": {
betas.add("computer-use-2024-10-22");
anthropicTools2.push({
name: "computer",
type: "computer_20241022",
display_width_px: tool.args.displayWidthPx,
display_height_px: tool.args.displayHeightPx,
display_number: tool.args.displayNumber,
cache_control: void 0
});
break;
}
case "anthropic.text_editor_20250124": {
betas.add("computer-use-2025-01-24");
anthropicTools2.push({
name: "str_replace_editor",
type: "text_editor_20250124",
cache_control: void 0
});
break;
}
case "anthropic.text_editor_20241022": {
betas.add("computer-use-2024-10-22");
anthropicTools2.push({
name: "str_replace_editor",
type: "text_editor_20241022",
cache_control: void 0
});
break;
}
case "anthropic.text_editor_20250429": {
betas.add("computer-use-2025-01-24");
anthropicTools2.push({
name: "str_replace_based_edit_tool",
type: "text_editor_20250429",
cache_control: void 0
});
break;
}
case "anthropic.text_editor_20250728": {
const args = await validateTypes({
value: tool.args,
schema: textEditor_20250728ArgsSchema
});
anthropicTools2.push({
name: "str_replace_based_edit_tool",
type: "text_editor_20250728",
max_characters: args.maxCharacters,
cache_control: void 0
});
break;
}
case "anthropic.bash_20250124": {
betas.add("computer-use-2025-01-24");
anthropicTools2.push({
name: "bash",
type: "bash_20250124",
cache_control: void 0
});
break;
}
case "anthropic.bash_20241022": {
betas.add("computer-use-2024-10-22");
anthropicTools2.push({
name: "bash",
type: "bash_20241022",
cache_control: void 0
});
break;
}
case "anthropic.memory_20250818": {
betas.add("context-management-2025-06-27");
anthropicTools2.push({
name: "memory",
type: "memory_20250818"
});
break;
}
case "anthropic.web_fetch_20250910": {
betas.add("web-fetch-2025-09-10");
const args = await validateTypes({
value: tool.args,
schema: webFetch_20250910ArgsSchema
});
anthropicTools2.push({
type: "web_fetch_20250910",
name: "web_fetch",
max_uses: args.maxUses,
allowed_domains: args.allowedDomains,
blocked_domains: args.blockedDomains,
citations: args.citations,
max_content_tokens: args.maxContentTokens,
cache_control: void 0
});
break;
}
case "anthropic.web_fetch_20260209": {
betas.add("code-execution-web-tools-2026-02-09");
const args = await validateTypes({
value: tool.args,
schema: webFetch_20260209ArgsSchema
});
anthropicTools2.push({
type: "web_fetch_20260209",
name: "web_fetch",
max_uses: args.maxUses,
allowed_domains: args.allowedDomains,
blocked_domains: args.blockedDomains,
citations: args.citations,
max_content_tokens: args.maxContentTokens,
cache_control: void 0
});
break;
}
case "anthropic.web_search_20250305": {
const args = await validateTypes({
value: tool.args,
schema: webSearch_20250305ArgsSchema
});
anthropicTools2.push({
type: "web_search_20250305",
name: "web_search",
max_uses: args.maxUses,