@ai-sdk/anthropic 4.0.0-beta.4 → 4.0.0-beta.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +305 -4
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +83 -58
  4. package/dist/index.js +2043 -1356
  5. package/dist/index.js.map +1 -1
  6. package/dist/internal/index.d.ts +85 -58
  7. package/dist/internal/index.js +1804 -1342
  8. package/dist/internal/index.js.map +1 -1
  9. package/docs/05-anthropic.mdx +116 -13
  10. package/package.json +14 -15
  11. package/src/{anthropic-messages-api.ts → anthropic-api.ts} +14 -6
  12. package/src/anthropic-error.ts +1 -1
  13. package/src/anthropic-files.ts +95 -0
  14. package/src/{anthropic-messages-language-model.ts → anthropic-language-model.ts} +263 -78
  15. package/src/anthropic-message-metadata.ts +1 -4
  16. package/src/{anthropic-messages-options.ts → anthropic-options.ts} +68 -11
  17. package/src/anthropic-prepare-tools.ts +14 -7
  18. package/src/anthropic-provider.ts +42 -13
  19. package/src/{convert-anthropic-messages-usage.ts → convert-anthropic-usage.ts} +4 -4
  20. package/src/{convert-to-anthropic-messages-prompt.ts → convert-to-anthropic-prompt.ts} +190 -149
  21. package/src/forward-anthropic-container-id-from-last-step.ts +2 -2
  22. package/src/get-cache-control.ts +5 -2
  23. package/src/index.ts +1 -1
  24. package/src/internal/index.ts +11 -2
  25. package/src/map-anthropic-stop-reason.ts +1 -1
  26. package/src/sanitize-json-schema.ts +203 -0
  27. package/src/skills/anthropic-skills-api.ts +44 -0
  28. package/src/skills/anthropic-skills.ts +132 -0
  29. package/src/tool/bash_20241022.ts +2 -2
  30. package/src/tool/bash_20250124.ts +2 -2
  31. package/src/tool/code-execution_20250522.ts +2 -2
  32. package/src/tool/code-execution_20250825.ts +2 -2
  33. package/src/tool/code-execution_20260120.ts +2 -2
  34. package/src/tool/computer_20241022.ts +2 -2
  35. package/src/tool/computer_20250124.ts +2 -2
  36. package/src/tool/computer_20251124.ts +2 -2
  37. package/src/tool/memory_20250818.ts +2 -2
  38. package/src/tool/text-editor_20241022.ts +2 -2
  39. package/src/tool/text-editor_20250124.ts +2 -2
  40. package/src/tool/text-editor_20250429.ts +2 -2
  41. package/src/tool/text-editor_20250728.ts +6 -3
  42. package/src/tool/tool-search-bm25_20251119.ts +2 -2
  43. package/src/tool/tool-search-regex_20251119.ts +2 -2
  44. package/src/tool/web-fetch-20250910.ts +2 -2
  45. package/src/tool/web-fetch-20260209.ts +2 -2
  46. package/src/tool/web-search_20250305.ts +2 -2
  47. package/src/tool/web-search_20260209.ts +2 -2
  48. package/dist/index.d.mts +0 -1090
  49. package/dist/index.mjs +0 -5244
  50. package/dist/index.mjs.map +0 -1
  51. package/dist/internal/index.d.mts +0 -969
  52. package/dist/internal/index.mjs +0 -5136
  53. package/dist/internal/index.mjs.map +0 -1
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod/v4';
2
2
 
3
3
  // https://docs.claude.com/en/docs/about-claude/models/overview
4
- export type AnthropicMessagesModelId =
4
+ export type AnthropicModelId =
5
5
  | 'claude-3-haiku-20240307'
6
6
  | 'claude-haiku-4-5-20251001'
7
7
  | 'claude-haiku-4-5'
@@ -17,6 +17,7 @@ export type AnthropicMessagesModelId =
17
17
  | 'claude-sonnet-4-5'
18
18
  | 'claude-sonnet-4-6'
19
19
  | 'claude-opus-4-6'
20
+ | 'claude-opus-4-7'
20
21
  | (string & {});
21
22
 
22
23
  /**
@@ -83,6 +84,12 @@ export const anthropicLanguageModelOptions = z.object({
83
84
  z.object({
84
85
  /** for Sonnet 4.6, Opus 4.6, and newer models */
85
86
  type: z.literal('adaptive'),
87
+ /**
88
+ * Controls whether thinking content is included in the response.
89
+ * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+).
90
+ * - `"summarized"`: Thinking content is returned. Required to see reasoning output.
91
+ */
92
+ display: z.enum(['omitted', 'summarized']).optional(),
86
93
  }),
87
94
  z.object({
88
95
  /** for models before Opus 4.6, except Sonnet 4.6 still supports it */
@@ -112,6 +119,23 @@ export const anthropicLanguageModelOptions = z.object({
112
119
  })
113
120
  .optional(),
114
121
 
122
+ /**
123
+ * Metadata to include with the request.
124
+ *
125
+ * See https://platform.claude.com/docs/en/api/messages/create for details.
126
+ */
127
+ metadata: z
128
+ .object({
129
+ /**
130
+ * An external identifier for the user associated with the request.
131
+ *
132
+ * Should be a UUID, hash value, or other opaque identifier.
133
+ * Must not contain PII (name, email, phone number, etc.).
134
+ */
135
+ userId: z.string().optional(),
136
+ })
137
+ .optional(),
138
+
115
139
  /**
116
140
  * MCP servers to be utilized in this request.
117
141
  */
@@ -142,21 +166,29 @@ export const anthropicLanguageModelOptions = z.object({
142
166
  id: z.string().optional(),
143
167
  skills: z
144
168
  .array(
145
- z.object({
146
- type: z.union([z.literal('anthropic'), z.literal('custom')]),
147
- skillId: z.string(),
148
- version: z.string().optional(),
149
- }),
169
+ z.discriminatedUnion('type', [
170
+ z.object({
171
+ type: z.literal('anthropic'),
172
+ skillId: z.string(),
173
+ version: z.string().optional(),
174
+ }),
175
+ z.object({
176
+ type: z.literal('custom'),
177
+ providerReference: z.record(z.string(), z.string()),
178
+ version: z.string().optional(),
179
+ }),
180
+ ]),
150
181
  )
151
182
  .optional(),
152
183
  })
153
184
  .optional(),
154
185
 
155
186
  /**
156
- * Whether to enable tool streaming (and structured output streaming).
157
- *
158
- * When set to false, the model will return all tool calls and results
159
- * at once after a delay.
187
+ * Whether to enable fine-grained (eager) streaming of tool call inputs
188
+ * and structured outputs for every function tool in the request. When
189
+ * true (the default), each function tool receives a default of
190
+ * `eager_input_streaming: true` unless it explicitly sets
191
+ * `providerOptions.anthropic.eagerInputStreaming`.
160
192
  *
161
193
  * @default true
162
194
  */
@@ -165,7 +197,22 @@ export const anthropicLanguageModelOptions = z.object({
165
197
  /**
166
198
  * @default 'high'
167
199
  */
168
- effort: z.enum(['low', 'medium', 'high', 'max']).optional(),
200
+ effort: z.enum(['low', 'medium', 'high', 'xhigh', 'max']).optional(),
201
+
202
+ /**
203
+ * Task budget for agentic turns. Informs the model of the total token budget
204
+ * available for the current task, allowing it to prioritize work and wind down
205
+ * gracefully as the budget is consumed.
206
+ *
207
+ * Advisory only — does not enforce a hard token limit.
208
+ */
209
+ taskBudget: z
210
+ .object({
211
+ type: z.literal('tokens'),
212
+ total: z.number().int().min(20000),
213
+ remaining: z.number().int().min(0).optional(),
214
+ })
215
+ .optional(),
169
216
 
170
217
  /**
171
218
  * Enable fast mode for faster inference (2.5x faster output token speeds).
@@ -173,6 +220,16 @@ export const anthropicLanguageModelOptions = z.object({
173
220
  */
174
221
  speed: z.enum(['fast', 'standard']).optional(),
175
222
 
223
+ /**
224
+ * Controls where model inference runs for this request.
225
+ *
226
+ * - `"global"`: Inference may run in any available geography (default).
227
+ * - `"us"`: Inference runs only in US-based infrastructure.
228
+ *
229
+ * See https://platform.claude.com/docs/en/build-with-claude/data-residency
230
+ */
231
+ inferenceGeo: z.enum(['us', 'global']).optional(),
232
+
176
233
  /**
177
234
  * A set of beta features to enable.
178
235
  * Allow a provider to receive the full `betas` set if it needs it.
@@ -1,9 +1,9 @@
1
1
  import {
2
- LanguageModelV4CallOptions,
3
- SharedV4Warning,
4
2
  UnsupportedFunctionalityError,
3
+ type LanguageModelV4CallOptions,
4
+ type SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
- import { AnthropicTool, AnthropicToolChoice } from './anthropic-messages-api';
6
+ import type { AnthropicTool, AnthropicToolChoice } from './anthropic-api';
7
7
  import { CacheControlValidator } from './get-cache-control';
8
8
  import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
9
9
  import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
@@ -27,6 +27,7 @@ export async function prepareTools({
27
27
  cacheControlValidator,
28
28
  supportsStructuredOutput,
29
29
  supportsStrictTools,
30
+ defaultEagerInputStreaming = false,
30
31
  }: {
31
32
  tools: LanguageModelV4CallOptions['tools'];
32
33
  toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
@@ -42,6 +43,12 @@ export async function prepareTools({
42
43
  * Whether the model supports strict mode on tool definitions.
43
44
  */
44
45
  supportsStrictTools: boolean;
46
+
47
+ /**
48
+ * Default for `eager_input_streaming` on function tools that do not set
49
+ * it explicitly. Driven by the model-level `toolStreaming` option.
50
+ */
51
+ defaultEagerInputStreaming?: boolean;
45
52
  }): Promise<{
46
53
  tools: Array<AnthropicTool> | undefined;
47
54
  toolChoice: AnthropicToolChoice | undefined;
@@ -73,8 +80,10 @@ export async function prepareTools({
73
80
  const anthropicOptions = tool.providerOptions?.anthropic as
74
81
  | AnthropicToolOptions
75
82
  | undefined;
76
- // eager_input_streaming is only supported on custom (function) tools
77
- const eagerInputStreaming = anthropicOptions?.eagerInputStreaming;
83
+ // eager_input_streaming is only supported on custom (function) tools.
84
+ // Fall back to the model-level default when the tool doesn't set it.
85
+ const eagerInputStreaming =
86
+ anthropicOptions?.eagerInputStreaming ?? defaultEagerInputStreaming;
78
87
  const deferLoading = anthropicOptions?.deferLoading;
79
88
  const allowedCallers = anthropicOptions?.allowedCallers;
80
89
 
@@ -322,7 +331,6 @@ export async function prepareTools({
322
331
  }
323
332
 
324
333
  case 'anthropic.tool_search_regex_20251119': {
325
- betas.add('advanced-tool-use-2025-11-20');
326
334
  anthropicTools.push({
327
335
  type: 'tool_search_tool_regex_20251119',
328
336
  name: 'tool_search_tool_regex',
@@ -331,7 +339,6 @@ export async function prepareTools({
331
339
  }
332
340
 
333
341
  case 'anthropic.tool_search_bm25_20251119': {
334
- betas.add('advanced-tool-use-2025-11-20');
335
342
  anthropicTools.push({
336
343
  type: 'tool_search_tool_bm25_20251119',
337
344
  name: 'tool_search_tool_bm25',
@@ -1,42 +1,53 @@
1
1
  import {
2
2
  InvalidArgumentError,
3
- LanguageModelV4,
4
3
  NoSuchModelError,
5
- ProviderV4,
4
+ type FilesV4,
5
+ type LanguageModelV4,
6
+ type ProviderV4,
7
+ type SkillsV4,
6
8
  } from '@ai-sdk/provider';
7
9
  import {
8
- FetchFunction,
9
10
  generateId,
10
11
  loadApiKey,
11
12
  loadOptionalSetting,
12
13
  withoutTrailingSlash,
13
14
  withUserAgentSuffix,
15
+ type FetchFunction,
14
16
  } from '@ai-sdk/provider-utils';
15
- import { VERSION } from './version';
16
- import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-model';
17
- import { AnthropicMessagesModelId } from './anthropic-messages-options';
17
+ import { AnthropicFiles } from './anthropic-files';
18
+ import { AnthropicLanguageModel } from './anthropic-language-model';
19
+ import type { AnthropicModelId } from './anthropic-options';
18
20
  import { anthropicTools } from './anthropic-tools';
21
+ import { AnthropicSkills } from './skills/anthropic-skills';
22
+ import { VERSION } from './version';
19
23
 
20
24
  export interface AnthropicProvider extends ProviderV4 {
21
25
  /**
22
26
  * Creates a model for text generation.
23
27
  */
24
- (modelId: AnthropicMessagesModelId): LanguageModelV4;
28
+ (modelId: AnthropicModelId): LanguageModelV4;
25
29
 
26
30
  /**
27
31
  * Creates a model for text generation.
28
32
  */
29
- languageModel(modelId: AnthropicMessagesModelId): LanguageModelV4;
33
+ languageModel(modelId: AnthropicModelId): LanguageModelV4;
30
34
 
31
- chat(modelId: AnthropicMessagesModelId): LanguageModelV4;
35
+ chat(modelId: AnthropicModelId): LanguageModelV4;
32
36
 
33
- messages(modelId: AnthropicMessagesModelId): LanguageModelV4;
37
+ messages(modelId: AnthropicModelId): LanguageModelV4;
34
38
 
35
39
  /**
36
40
  * @deprecated Use `embeddingModel` instead.
37
41
  */
38
42
  textEmbeddingModel(modelId: string): never;
39
43
 
44
+ files(): FilesV4;
45
+
46
+ /**
47
+ * Returns a SkillsV4 interface for uploading skills to Anthropic.
48
+ */
49
+ skills(): SkillsV4;
50
+
40
51
  /**
41
52
  * Anthropic-specific computer use tool.
42
53
  */
@@ -130,8 +141,8 @@ export function createAnthropic(
130
141
  );
131
142
  };
132
143
 
133
- const createChatModel = (modelId: AnthropicMessagesModelId) =>
134
- new AnthropicMessagesLanguageModel(modelId, {
144
+ const createChatModel = (modelId: AnthropicModelId) =>
145
+ new AnthropicLanguageModel(modelId, {
135
146
  provider: providerName,
136
147
  baseURL,
137
148
  headers: getHeaders,
@@ -143,7 +154,15 @@ export function createAnthropic(
143
154
  }),
144
155
  });
145
156
 
146
- const provider = function (modelId: AnthropicMessagesModelId) {
157
+ const createSkills = () =>
158
+ new AnthropicSkills({
159
+ provider: `${providerName.replace('.messages', '')}.skills`,
160
+ baseURL,
161
+ headers: getHeaders,
162
+ fetch: options.fetch,
163
+ });
164
+
165
+ const provider = function (modelId: AnthropicModelId) {
147
166
  if (new.target) {
148
167
  throw new Error(
149
168
  'The Anthropic model function cannot be called with the new keyword.',
@@ -166,6 +185,16 @@ export function createAnthropic(
166
185
  throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
167
186
  };
168
187
 
188
+ provider.files = () =>
189
+ new AnthropicFiles({
190
+ provider: providerName,
191
+ baseURL,
192
+ headers: getHeaders,
193
+ fetch: options.fetch,
194
+ });
195
+
196
+ provider.skills = createSkills;
197
+
169
198
  provider.tools = anthropicTools;
170
199
 
171
200
  return provider;
@@ -1,4 +1,4 @@
1
- import { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
1
+ import type { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * Represents a single iteration in the usage breakdown.
@@ -11,7 +11,7 @@ export type AnthropicUsageIteration = {
11
11
  output_tokens: number;
12
12
  };
13
13
 
14
- export type AnthropicMessagesUsage = {
14
+ export type AnthropicUsage = {
15
15
  input_tokens: number;
16
16
  output_tokens: number;
17
17
  cache_creation_input_tokens?: number | null;
@@ -25,11 +25,11 @@ export type AnthropicMessagesUsage = {
25
25
  iterations?: AnthropicUsageIteration[] | null;
26
26
  };
27
27
 
28
- export function convertAnthropicMessagesUsage({
28
+ export function convertAnthropicUsage({
29
29
  usage,
30
30
  rawUsage,
31
31
  }: {
32
- usage: AnthropicMessagesUsage;
32
+ usage: AnthropicUsage;
33
33
  rawUsage?: JSONObject;
34
34
  }): LanguageModelV4Usage {
35
35
  const cacheCreationTokens = usage.cache_creation_input_tokens ?? 0;