@ai-sdk/anthropic 3.0.25 → 3.0.27
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.
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +20 -20
- package/dist/index.d.ts +20 -20
- package/dist/index.js +70 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -24
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +5 -0
- package/dist/internal/index.d.ts +5 -0
- package/dist/internal/index.js +69 -23
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +69 -23
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +37 -9
- package/package.json +1 -1
- package/src/anthropic-messages-language-model.ts +84 -20
- package/src/anthropic-provider.ts +20 -20
package/docs/05-anthropic.mdx
CHANGED
|
@@ -56,6 +56,13 @@ You can use the following optional settings to customize the Anthropic provider
|
|
|
56
56
|
|
|
57
57
|
API key that is being sent using the `x-api-key` header.
|
|
58
58
|
It defaults to the `ANTHROPIC_API_KEY` environment variable.
|
|
59
|
+
Only one of `apiKey` or `authToken` is required.
|
|
60
|
+
|
|
61
|
+
- **authToken** _string_
|
|
62
|
+
|
|
63
|
+
Auth token that is being sent using the `Authorization: Bearer` header.
|
|
64
|
+
It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
|
|
65
|
+
Only one of `apiKey` or `authToken` is required.
|
|
59
66
|
|
|
60
67
|
- **headers** _Record<string,string>_
|
|
61
68
|
|
|
@@ -78,6 +85,12 @@ Some models have multi-modal capabilities.
|
|
|
78
85
|
const model = anthropic('claude-3-haiku-20240307');
|
|
79
86
|
```
|
|
80
87
|
|
|
88
|
+
You can also use the following aliases for model creation:
|
|
89
|
+
|
|
90
|
+
- `anthropic.languageModel('claude-3-haiku-20240307')` - Creates a language model
|
|
91
|
+
- `anthropic.chat('claude-3-haiku-20240307')` - Alias for `languageModel`
|
|
92
|
+
- `anthropic.messages('claude-3-haiku-20240307')` - Alias for `languageModel`
|
|
93
|
+
|
|
81
94
|
You can use Anthropic language models to generate text with the `generateText` function:
|
|
82
95
|
|
|
83
96
|
```ts
|
|
@@ -317,7 +330,7 @@ import { generateText } from 'ai';
|
|
|
317
330
|
const errorMessage = '... long error message ...';
|
|
318
331
|
|
|
319
332
|
const result = await generateText({
|
|
320
|
-
model: anthropic('claude-
|
|
333
|
+
model: anthropic('claude-sonnet-4-5'),
|
|
321
334
|
messages: [
|
|
322
335
|
{
|
|
323
336
|
role: 'user',
|
|
@@ -345,7 +358,7 @@ You can also use cache control on system messages by providing multiple system m
|
|
|
345
358
|
|
|
346
359
|
```ts highlight="3,7-9"
|
|
347
360
|
const result = await generateText({
|
|
348
|
-
model: anthropic('claude-
|
|
361
|
+
model: anthropic('claude-sonnet-4-5'),
|
|
349
362
|
messages: [
|
|
350
363
|
{
|
|
351
364
|
role: 'system',
|
|
@@ -444,7 +457,7 @@ For more on prompt caching with Anthropic, see [Anthropic's Cache Control docume
|
|
|
444
457
|
The Bash Tool allows running bash commands. Here's how to create and use it:
|
|
445
458
|
|
|
446
459
|
```ts
|
|
447
|
-
const bashTool = anthropic.tools.
|
|
460
|
+
const bashTool = anthropic.tools.bash_20250124({
|
|
448
461
|
execute: async ({ command, restart }) => {
|
|
449
462
|
// Implement your bash command execution logic here
|
|
450
463
|
// Return the result of the command execution
|
|
@@ -457,7 +470,10 @@ Parameters:
|
|
|
457
470
|
- `command` (string): The bash command to run. Required unless the tool is being restarted.
|
|
458
471
|
- `restart` (boolean, optional): Specifying true will restart this tool.
|
|
459
472
|
|
|
460
|
-
<Note>
|
|
473
|
+
<Note>
|
|
474
|
+
Two versions are available: `bash_20250124` (recommended) and `bash_20241022`.
|
|
475
|
+
Only certain Claude versions are supported.
|
|
476
|
+
</Note>
|
|
461
477
|
|
|
462
478
|
### Memory Tool
|
|
463
479
|
|
|
@@ -491,8 +507,14 @@ const tools = {
|
|
|
491
507
|
```
|
|
492
508
|
|
|
493
509
|
<Note>
|
|
494
|
-
Different models support different versions of the tool
|
|
495
|
-
|
|
510
|
+
Different models support different versions of the tool:
|
|
511
|
+
|
|
512
|
+
- `textEditor_20250728` - For Claude Sonnet 4, Opus 4, and Opus 4.1 (recommended)
|
|
513
|
+
- `textEditor_20250124` - For Claude Sonnet 3.7
|
|
514
|
+
- `textEditor_20241022` - For Claude Sonnet 3.5
|
|
515
|
+
|
|
516
|
+
Note: `textEditor_20250429` is deprecated. Use `textEditor_20250728` instead.
|
|
517
|
+
|
|
496
518
|
</Note>
|
|
497
519
|
|
|
498
520
|
Parameters:
|
|
@@ -848,6 +870,12 @@ const result = await generateText({
|
|
|
848
870
|
});
|
|
849
871
|
```
|
|
850
872
|
|
|
873
|
+
<Note>
|
|
874
|
+
Two versions are available: `codeExecution_20250825` (recommended, supports
|
|
875
|
+
Python and Bash with enhanced file operations) and `codeExecution_20250522`
|
|
876
|
+
(supports Bash only).
|
|
877
|
+
</Note>
|
|
878
|
+
|
|
851
879
|
#### Error Handling
|
|
852
880
|
|
|
853
881
|
Code execution errors are handled differently depending on whether you're using streaming or non-streaming:
|
|
@@ -1034,14 +1062,14 @@ const result = await generateText({
|
|
|
1034
1062
|
|
|
1035
1063
|
### PDF support
|
|
1036
1064
|
|
|
1037
|
-
Anthropic
|
|
1065
|
+
Anthropic Claude models support reading PDF files.
|
|
1038
1066
|
You can pass PDF files as part of the message content using the `file` type:
|
|
1039
1067
|
|
|
1040
1068
|
Option 1: URL-based PDF document
|
|
1041
1069
|
|
|
1042
1070
|
```ts
|
|
1043
1071
|
const result = await generateText({
|
|
1044
|
-
model: anthropic('claude-
|
|
1072
|
+
model: anthropic('claude-sonnet-4-5'),
|
|
1045
1073
|
messages: [
|
|
1046
1074
|
{
|
|
1047
1075
|
role: 'user',
|
|
@@ -1067,7 +1095,7 @@ Option 2: Base64-encoded PDF document
|
|
|
1067
1095
|
|
|
1068
1096
|
```ts
|
|
1069
1097
|
const result = await generateText({
|
|
1070
|
-
model: anthropic('claude-
|
|
1098
|
+
model: anthropic('claude-sonnet-4-5'),
|
|
1071
1099
|
messages: [
|
|
1072
1100
|
{
|
|
1073
1101
|
role: 'user',
|
package/package.json
CHANGED
|
@@ -152,6 +152,16 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
152
152
|
return this.config.provider;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Extracts the dynamic provider name from the config.provider string.
|
|
157
|
+
* e.g., 'my-custom-anthropic.messages' -> 'my-custom-anthropic'
|
|
158
|
+
*/
|
|
159
|
+
private get providerOptionsName(): string {
|
|
160
|
+
const provider = this.config.provider;
|
|
161
|
+
const dotIndex = provider.indexOf('.');
|
|
162
|
+
return dotIndex === -1 ? provider : provider.substring(0, dotIndex);
|
|
163
|
+
}
|
|
164
|
+
|
|
155
165
|
get supportedUrls() {
|
|
156
166
|
return this.config.supportedUrls?.() ?? {};
|
|
157
167
|
}
|
|
@@ -218,12 +228,34 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
218
228
|
}
|
|
219
229
|
}
|
|
220
230
|
|
|
221
|
-
const
|
|
231
|
+
const providerOptionsName = this.providerOptionsName;
|
|
232
|
+
|
|
233
|
+
// Parse provider options from both canonical 'anthropic' key and custom key
|
|
234
|
+
const canonicalOptions = await parseProviderOptions({
|
|
222
235
|
provider: 'anthropic',
|
|
223
236
|
providerOptions,
|
|
224
237
|
schema: anthropicProviderOptions,
|
|
225
238
|
});
|
|
226
239
|
|
|
240
|
+
const customProviderOptions =
|
|
241
|
+
providerOptionsName !== 'anthropic'
|
|
242
|
+
? await parseProviderOptions({
|
|
243
|
+
provider: providerOptionsName,
|
|
244
|
+
providerOptions,
|
|
245
|
+
schema: anthropicProviderOptions,
|
|
246
|
+
})
|
|
247
|
+
: null;
|
|
248
|
+
|
|
249
|
+
// Track if custom key was explicitly used
|
|
250
|
+
const usedCustomProviderKey = customProviderOptions != null;
|
|
251
|
+
|
|
252
|
+
// Merge options
|
|
253
|
+
const anthropicOptions = Object.assign(
|
|
254
|
+
{},
|
|
255
|
+
canonicalOptions ?? {},
|
|
256
|
+
customProviderOptions ?? {},
|
|
257
|
+
);
|
|
258
|
+
|
|
227
259
|
const {
|
|
228
260
|
maxOutputTokens: maxOutputTokensForModel,
|
|
229
261
|
supportsStructuredOutput: modelSupportsStructuredOutput,
|
|
@@ -567,6 +599,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
567
599
|
betas: new Set([...betas, ...toolsBetas, ...userSuppliedBetas]),
|
|
568
600
|
usesJsonResponseTool: jsonResponseTool != null,
|
|
569
601
|
toolNameMapping,
|
|
602
|
+
providerOptionsName,
|
|
603
|
+
usedCustomProviderKey,
|
|
570
604
|
};
|
|
571
605
|
}
|
|
572
606
|
|
|
@@ -659,12 +693,19 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
659
693
|
async doGenerate(
|
|
660
694
|
options: LanguageModelV3CallOptions,
|
|
661
695
|
): Promise<LanguageModelV3GenerateResult> {
|
|
662
|
-
const {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
696
|
+
const {
|
|
697
|
+
args,
|
|
698
|
+
warnings,
|
|
699
|
+
betas,
|
|
700
|
+
usesJsonResponseTool,
|
|
701
|
+
toolNameMapping,
|
|
702
|
+
providerOptionsName,
|
|
703
|
+
usedCustomProviderKey,
|
|
704
|
+
} = await this.getArgs({
|
|
705
|
+
...options,
|
|
706
|
+
stream: false,
|
|
707
|
+
userSuppliedBetas: await this.getBetasFromHeaders(options.headers),
|
|
708
|
+
});
|
|
668
709
|
|
|
669
710
|
// Extract citation documents for response processing
|
|
670
711
|
const citationDocuments = [
|
|
@@ -1051,8 +1092,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1051
1092
|
body: rawResponse,
|
|
1052
1093
|
},
|
|
1053
1094
|
warnings,
|
|
1054
|
-
providerMetadata: {
|
|
1055
|
-
|
|
1095
|
+
providerMetadata: (() => {
|
|
1096
|
+
const anthropicMetadata = {
|
|
1056
1097
|
usage: response.usage as JSONObject,
|
|
1057
1098
|
cacheCreationInputTokens:
|
|
1058
1099
|
response.usage.cache_creation_input_tokens ?? null,
|
|
@@ -1073,8 +1114,18 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1073
1114
|
mapAnthropicResponseContextManagement(
|
|
1074
1115
|
response.context_management,
|
|
1075
1116
|
) ?? null,
|
|
1076
|
-
} satisfies AnthropicMessageMetadata
|
|
1077
|
-
|
|
1117
|
+
} satisfies AnthropicMessageMetadata;
|
|
1118
|
+
|
|
1119
|
+
const providerMetadata: SharedV3ProviderMetadata = {
|
|
1120
|
+
anthropic: anthropicMetadata,
|
|
1121
|
+
};
|
|
1122
|
+
|
|
1123
|
+
if (usedCustomProviderKey && providerOptionsName !== 'anthropic') {
|
|
1124
|
+
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
return providerMetadata;
|
|
1128
|
+
})(),
|
|
1078
1129
|
};
|
|
1079
1130
|
}
|
|
1080
1131
|
|
|
@@ -1087,6 +1138,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1087
1138
|
betas,
|
|
1088
1139
|
usesJsonResponseTool,
|
|
1089
1140
|
toolNameMapping,
|
|
1141
|
+
providerOptionsName,
|
|
1142
|
+
usedCustomProviderKey,
|
|
1090
1143
|
} = await this.getArgs({
|
|
1091
1144
|
...options,
|
|
1092
1145
|
stream: true,
|
|
@@ -1932,19 +1985,30 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1932
1985
|
}
|
|
1933
1986
|
|
|
1934
1987
|
case 'message_stop': {
|
|
1988
|
+
const anthropicMetadata = {
|
|
1989
|
+
usage: (rawUsage as JSONObject) ?? null,
|
|
1990
|
+
cacheCreationInputTokens,
|
|
1991
|
+
stopSequence,
|
|
1992
|
+
container,
|
|
1993
|
+
contextManagement,
|
|
1994
|
+
} satisfies AnthropicMessageMetadata;
|
|
1995
|
+
|
|
1996
|
+
const providerMetadata: SharedV3ProviderMetadata = {
|
|
1997
|
+
anthropic: anthropicMetadata,
|
|
1998
|
+
};
|
|
1999
|
+
|
|
2000
|
+
if (
|
|
2001
|
+
usedCustomProviderKey &&
|
|
2002
|
+
providerOptionsName !== 'anthropic'
|
|
2003
|
+
) {
|
|
2004
|
+
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
2005
|
+
}
|
|
2006
|
+
|
|
1935
2007
|
controller.enqueue({
|
|
1936
2008
|
type: 'finish',
|
|
1937
2009
|
finishReason,
|
|
1938
2010
|
usage: convertAnthropicMessagesUsage(usage),
|
|
1939
|
-
providerMetadata
|
|
1940
|
-
anthropic: {
|
|
1941
|
-
usage: (rawUsage as JSONObject) ?? null,
|
|
1942
|
-
cacheCreationInputTokens,
|
|
1943
|
-
stopSequence,
|
|
1944
|
-
container,
|
|
1945
|
-
contextManagement,
|
|
1946
|
-
} satisfies AnthropicMessageMetadata,
|
|
1947
|
-
},
|
|
2011
|
+
providerMetadata,
|
|
1948
2012
|
});
|
|
1949
2013
|
return;
|
|
1950
2014
|
}
|
|
@@ -19,13 +19,13 @@ import { anthropicTools } from './anthropic-tools';
|
|
|
19
19
|
|
|
20
20
|
export interface AnthropicProvider extends ProviderV3 {
|
|
21
21
|
/**
|
|
22
|
-
Creates a model for text generation.
|
|
23
|
-
*/
|
|
22
|
+
* Creates a model for text generation.
|
|
23
|
+
*/
|
|
24
24
|
(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
Creates a model for text generation.
|
|
28
|
-
*/
|
|
27
|
+
* Creates a model for text generation.
|
|
28
|
+
*/
|
|
29
29
|
languageModel(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
30
30
|
|
|
31
31
|
chat(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
@@ -38,41 +38,41 @@ Creates a model for text generation.
|
|
|
38
38
|
textEmbeddingModel(modelId: string): never;
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
Anthropic-specific computer use tool.
|
|
41
|
+
* Anthropic-specific computer use tool.
|
|
42
42
|
*/
|
|
43
43
|
tools: typeof anthropicTools;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export interface AnthropicProviderSettings {
|
|
47
47
|
/**
|
|
48
|
-
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
49
|
-
The default prefix is `https://api.anthropic.com/v1`.
|
|
48
|
+
* Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
49
|
+
* The default prefix is `https://api.anthropic.com/v1`.
|
|
50
50
|
*/
|
|
51
51
|
baseURL?: string;
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
API key that is being send using the `x-api-key` header.
|
|
55
|
-
It defaults to the `ANTHROPIC_API_KEY` environment variable.
|
|
56
|
-
Only one of `apiKey` or `authToken` is required.
|
|
54
|
+
* API key that is being send using the `x-api-key` header.
|
|
55
|
+
* It defaults to the `ANTHROPIC_API_KEY` environment variable.
|
|
56
|
+
* Only one of `apiKey` or `authToken` is required.
|
|
57
57
|
*/
|
|
58
58
|
apiKey?: string;
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
Auth token that is being sent using the `Authorization: Bearer` header.
|
|
62
|
-
It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
|
|
63
|
-
Only one of `apiKey` or `authToken` is required.
|
|
61
|
+
* Auth token that is being sent using the `Authorization: Bearer` header.
|
|
62
|
+
* It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
|
|
63
|
+
* Only one of `apiKey` or `authToken` is required.
|
|
64
64
|
*/
|
|
65
65
|
authToken?: string;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
Custom headers to include in the requests.
|
|
69
|
-
|
|
68
|
+
* Custom headers to include in the requests.
|
|
69
|
+
*/
|
|
70
70
|
headers?: Record<string, string>;
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
74
|
-
or to provide a custom fetch implementation for e.g. testing.
|
|
75
|
-
|
|
73
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
74
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
75
|
+
*/
|
|
76
76
|
fetch?: FetchFunction;
|
|
77
77
|
|
|
78
78
|
generateId?: () => string;
|
|
@@ -85,7 +85,7 @@ or to provide a custom fetch implementation for e.g. testing.
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
Create an Anthropic provider instance.
|
|
88
|
+
* Create an Anthropic provider instance.
|
|
89
89
|
*/
|
|
90
90
|
export function createAnthropic(
|
|
91
91
|
options: AnthropicProviderSettings = {},
|
|
@@ -172,6 +172,6 @@ export function createAnthropic(
|
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
|
-
Default Anthropic provider instance.
|
|
175
|
+
* Default Anthropic provider instance.
|
|
176
176
|
*/
|
|
177
177
|
export const anthropic = createAnthropic();
|