@ai-sdk/anthropic 4.0.0-beta.5 → 4.0.0-beta.67
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 +500 -4
- package/README.md +2 -0
- package/dist/index.d.ts +265 -68
- package/dist/index.js +2636 -1427
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +234 -62
- package/dist/internal/index.js +2605 -1413
- package/dist/internal/index.js.map +1 -1
- package/docs/05-anthropic.mdx +303 -20
- package/package.json +16 -17
- package/src/{anthropic-messages-api.ts → anthropic-api.ts} +158 -17
- package/src/anthropic-error.ts +1 -1
- package/src/anthropic-files.ts +95 -0
- package/src/{anthropic-messages-options.ts → anthropic-language-model-options.ts} +104 -11
- package/src/{anthropic-messages-language-model.ts → anthropic-language-model.ts} +494 -96
- package/src/anthropic-message-metadata.ts +69 -9
- package/src/anthropic-prepare-tools.ts +31 -7
- package/src/anthropic-provider.ts +42 -13
- package/src/anthropic-tools.ts +31 -0
- package/src/convert-anthropic-usage.ts +109 -0
- package/src/{convert-to-anthropic-messages-prompt.ts → convert-to-anthropic-prompt.ts} +376 -198
- package/src/forward-anthropic-container-id-from-last-step.ts +2 -2
- package/src/get-cache-control.ts +5 -2
- package/src/index.ts +1 -1
- package/src/internal/index.ts +13 -2
- package/src/map-anthropic-stop-reason.ts +1 -1
- package/src/sanitize-json-schema.ts +203 -0
- package/src/skills/anthropic-skills-api.ts +44 -0
- package/src/skills/anthropic-skills.ts +132 -0
- package/src/tool/advisor_20260301.ts +128 -0
- package/src/tool/bash_20241022.ts +84 -13
- package/src/tool/bash_20250124.ts +84 -13
- package/src/tool/code-execution_20250522.ts +2 -2
- package/src/tool/code-execution_20250825.ts +2 -2
- package/src/tool/code-execution_20260120.ts +2 -2
- package/src/tool/computer_20241022.ts +2 -2
- package/src/tool/computer_20250124.ts +2 -2
- package/src/tool/computer_20251124.ts +2 -2
- package/src/tool/memory_20250818.ts +2 -2
- package/src/tool/text-editor_20241022.ts +2 -2
- package/src/tool/text-editor_20250124.ts +2 -2
- package/src/tool/text-editor_20250429.ts +2 -2
- package/src/tool/text-editor_20250728.ts +6 -3
- package/src/tool/tool-search-bm25_20251119.ts +2 -2
- package/src/tool/tool-search-regex_20251119.ts +2 -2
- package/src/tool/web-fetch-20250910.ts +2 -2
- package/src/tool/web-fetch-20260209.ts +2 -2
- package/src/tool/web-search_20250305.ts +2 -2
- package/src/tool/web-search_20260209.ts +2 -2
- package/dist/index.d.mts +0 -1090
- package/dist/index.mjs +0 -5244
- package/dist/index.mjs.map +0 -1
- package/dist/internal/index.d.mts +0 -969
- package/dist/internal/index.mjs +0 -5136
- package/dist/internal/index.mjs.map +0 -1
- package/src/convert-anthropic-messages-usage.ts +0 -73
package/docs/05-anthropic.mdx
CHANGED
|
@@ -122,14 +122,22 @@ The following optional provider options are available for Anthropic models:
|
|
|
122
122
|
If you are experiencing issues with the model handling requests involving
|
|
123
123
|
reasoning content, you can set this to `false` to omit them from the request.
|
|
124
124
|
|
|
125
|
-
- `effort` _"
|
|
125
|
+
- `effort` _"low" | "medium" | "high" | "xhigh" | "max"_
|
|
126
126
|
|
|
127
127
|
Optional. See [Effort section](#effort) for more details.
|
|
128
128
|
|
|
129
|
+
- `taskBudget` _object_
|
|
130
|
+
|
|
131
|
+
Optional. See [Task Budgets section](#task-budgets) for more details.
|
|
132
|
+
|
|
129
133
|
- `speed` _"fast" | "standard"_
|
|
130
134
|
|
|
131
135
|
Optional. See [Fast Mode section](#fast-mode) for more details.
|
|
132
136
|
|
|
137
|
+
- `inferenceGeo` _"us" | "global"_
|
|
138
|
+
|
|
139
|
+
Optional. See [Data Residency section](#data-residency) for more details.
|
|
140
|
+
|
|
133
141
|
- `thinking` _object_
|
|
134
142
|
|
|
135
143
|
Optional. See [Reasoning section](#reasoning) for more details.
|
|
@@ -141,11 +149,15 @@ The following optional provider options are available for Anthropic models:
|
|
|
141
149
|
- `structuredOutputMode` _"outputFormat" | "jsonTool" | "auto"_
|
|
142
150
|
|
|
143
151
|
Determines how structured outputs are generated. Optional.
|
|
144
|
-
|
|
145
152
|
- `"outputFormat"`: Use the `output_format` parameter to specify the structured output format.
|
|
146
153
|
- `"jsonTool"`: Use a special `"json"` tool to specify the structured output format.
|
|
147
154
|
- `"auto"`: Use `"outputFormat"` when supported, otherwise fall back to `"jsonTool"` (default).
|
|
148
155
|
|
|
156
|
+
- `metadata` _object_
|
|
157
|
+
|
|
158
|
+
Optional. Metadata to include with the request. See the [Anthropic API documentation](https://platform.claude.com/docs/en/api/messages/create) for details.
|
|
159
|
+
- `userId` _string_ - An external identifier for the end-user. Should be a UUID, hash, or other opaque identifier. Must not contain PII.
|
|
160
|
+
|
|
149
161
|
### Structured Outputs and Tool Input Streaming
|
|
150
162
|
|
|
151
163
|
Tool call streaming is enabled by default. You can opt out by setting the
|
|
@@ -177,7 +189,7 @@ const result = streamText({
|
|
|
177
189
|
|
|
178
190
|
### Effort
|
|
179
191
|
|
|
180
|
-
Anthropic introduced an `effort` option with `claude-opus-4-5` that affects thinking, text responses, and function calls. Effort defaults to `high` and you can set it to `medium` or `low` to save tokens and to lower time-to-last-token latency (TTLT).
|
|
192
|
+
Anthropic introduced an `effort` option with `claude-opus-4-5` that affects thinking, text responses, and function calls. Effort defaults to `high` and you can set it to `medium` or `low` to save tokens and to lower time-to-last-token latency (TTLT). `claude-opus-4-7` additionally supports `xhigh` for maximum reasoning effort.
|
|
181
193
|
|
|
182
194
|
```ts highlight="8-10"
|
|
183
195
|
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
@@ -218,6 +230,125 @@ const { text } = await generateText({
|
|
|
218
230
|
|
|
219
231
|
The `speed` option accepts `'fast'` or `'standard'` (default behavior).
|
|
220
232
|
|
|
233
|
+
### Task Budgets
|
|
234
|
+
|
|
235
|
+
`claude-opus-4-7` supports a `taskBudget` option that informs the model of the total token budget available for an agentic turn. The model uses this information to prioritize work, plan ahead, and wind down gracefully as the budget is consumed.
|
|
236
|
+
|
|
237
|
+
Task budgets are advisory — they do not enforce a hard token limit. The model will attempt to stay within budget, but actual usage may vary.
|
|
238
|
+
|
|
239
|
+
```ts highlight="8-13"
|
|
240
|
+
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
241
|
+
import { generateText } from 'ai';
|
|
242
|
+
|
|
243
|
+
const { text } = await generateText({
|
|
244
|
+
model: anthropic('claude-opus-4-8'),
|
|
245
|
+
prompt: 'Research the pros and cons of Rust vs Go for building CLI tools.',
|
|
246
|
+
providerOptions: {
|
|
247
|
+
anthropic: {
|
|
248
|
+
taskBudget: {
|
|
249
|
+
type: 'tokens',
|
|
250
|
+
total: 400000,
|
|
251
|
+
},
|
|
252
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
For long-running agents that compact and restart context, you can carry the remaining budget forward using the `remaining` field:
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
taskBudget: {
|
|
261
|
+
type: 'tokens',
|
|
262
|
+
total: 400000,
|
|
263
|
+
remaining: 215000, // budget left after prior compacted-away contexts
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
The `taskBudget` object accepts:
|
|
268
|
+
|
|
269
|
+
- `type` _"tokens"_ - Budget type. Currently only `"tokens"` is supported.
|
|
270
|
+
- `total` _number_ - Total task budget for the agentic turn. Minimum 20,000.
|
|
271
|
+
- `remaining` _number_ - Budget left after prior compacted-away contexts. Must be between 0 and `total`. Defaults to `total` if omitted.
|
|
272
|
+
|
|
273
|
+
### Data Residency
|
|
274
|
+
|
|
275
|
+
Anthropic supports an [`inferenceGeo` option](https://platform.claude.com/docs/en/build-with-claude/data-residency) that controls where model inference runs for a request.
|
|
276
|
+
|
|
277
|
+
```ts highlight="8-10"
|
|
278
|
+
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
279
|
+
import { generateText } from 'ai';
|
|
280
|
+
|
|
281
|
+
const { text } = await generateText({
|
|
282
|
+
model: anthropic('claude-opus-4-6'),
|
|
283
|
+
prompt: 'Summarize the key points of this document.',
|
|
284
|
+
providerOptions: {
|
|
285
|
+
anthropic: {
|
|
286
|
+
inferenceGeo: 'us',
|
|
287
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The `inferenceGeo` option accepts `'us'` (US-only infrastructure) or `'global'` (default, any available geography).
|
|
293
|
+
|
|
294
|
+
### Safety Classifiers and Fallbacks
|
|
295
|
+
|
|
296
|
+
Claude Fable 5 has safeguards that limit its performance in certain areas like cybersecurity, biology, and chemistry, and automated safety checks are run on every request.
|
|
297
|
+
|
|
298
|
+
When one of these checks blocks a request, the API does not answer it. Instead it returns a classifier block: a `200` response with a `refusal` stop reason and an optional `stop_details` object describing the category that triggered the block. The AI SDK surfaces this as a `content-filter` finish reason, with the details available on `providerMetadata.anthropic.stopDetails`.
|
|
299
|
+
|
|
300
|
+
A classifier block looks like this:
|
|
301
|
+
|
|
302
|
+
```json
|
|
303
|
+
{
|
|
304
|
+
"type": "message",
|
|
305
|
+
"model": "claude-fable-5",
|
|
306
|
+
"content": [],
|
|
307
|
+
"stop_reason": "refusal",
|
|
308
|
+
"stop_details": {
|
|
309
|
+
"type": "refusal",
|
|
310
|
+
"category": "cyber",
|
|
311
|
+
"explanation": "This request triggered restrictions on violative cyber content and was blocked under Anthropic's Usage Policy."
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
Branch on the finish reason rather than on the presence of `stop_details` — the API may return a refusal with no details at all.
|
|
317
|
+
|
|
318
|
+
To avoid receiving a classifier block, pass the `fallbacks` option. When the primary model's classifiers block a turn, the API automatically retries it server-side on the next model in the chain and returns that model's answer. The required beta header is added for you.
|
|
319
|
+
|
|
320
|
+
```ts highlight="8-10"
|
|
321
|
+
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
322
|
+
import { generateText } from 'ai';
|
|
323
|
+
|
|
324
|
+
const { text } = await generateText({
|
|
325
|
+
model: anthropic('claude-fable-5'),
|
|
326
|
+
prompt: 'Explain the history of cryptography.',
|
|
327
|
+
providerOptions: {
|
|
328
|
+
anthropic: {
|
|
329
|
+
fallbacks: [{ model: 'claude-fable-5' }],
|
|
330
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Each fallback entry requires a `model` and may additionally override `max_tokens`, `thinking`, `output_config`, and `speed` for that attempt only.
|
|
336
|
+
|
|
337
|
+
When a fallback serves the turn, it is recorded in the per-model usage breakdown (`usage.iterations`) as a `fallback_message` entry. The AI SDK exposes this breakdown on `providerMetadata.anthropic.iterations`, so you can detect whether a fallback ran:
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
// set up `result` with a `streamText` call passing `fallbacks` as shown
|
|
341
|
+
// above, then consume the stream
|
|
342
|
+
|
|
343
|
+
const { iterations } =
|
|
344
|
+
(await result.finalStep).providerMetadata?.anthropic ?? {};
|
|
345
|
+
const servedByFallback = iterations?.some(
|
|
346
|
+
iteration => iteration.type === 'fallback_message',
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
console.log('Served by fallback:', servedByFallback);
|
|
350
|
+
```
|
|
351
|
+
|
|
221
352
|
### Reasoning
|
|
222
353
|
|
|
223
354
|
Anthropic models support extended thinking, where Claude shows its reasoning process before providing a final answer.
|
|
@@ -261,6 +392,31 @@ const { text } = await generateText({
|
|
|
261
392
|
});
|
|
262
393
|
```
|
|
263
394
|
|
|
395
|
+
##### Thinking Display (Opus 4.7+)
|
|
396
|
+
|
|
397
|
+
Starting with `claude-opus-4-7`, thinking content is omitted from the response by default — thinking blocks are present in the stream but their text is empty. To receive reasoning output, set `display: 'summarized'`:
|
|
398
|
+
|
|
399
|
+
```ts highlight="5"
|
|
400
|
+
const { text, reasoningText } = await generateText({
|
|
401
|
+
model: anthropic('claude-opus-4-8'),
|
|
402
|
+
providerOptions: {
|
|
403
|
+
anthropic: {
|
|
404
|
+
thinking: { type: 'adaptive', display: 'summarized' },
|
|
405
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
406
|
+
},
|
|
407
|
+
prompt: 'How many people will live in the world in 2040?',
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
console.log(reasoningText); // reasoning text (empty without display: 'summarized')
|
|
411
|
+
console.log(text);
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
<Note>
|
|
415
|
+
If you stream reasoning to users with `claude-opus-4-7`, the default
|
|
416
|
+
`"omitted"` display will cause a long pause before output begins. Set
|
|
417
|
+
`display: "summarized"` to restore visible progress during thinking.
|
|
418
|
+
</Note>
|
|
419
|
+
|
|
264
420
|
#### Budget-Based Thinking
|
|
265
421
|
|
|
266
422
|
For earlier models (`claude-opus-4-20250514`, `claude-sonnet-4-20250514`, `claude-sonnet-4-5-20250929`),
|
|
@@ -408,7 +564,7 @@ When compaction occurs, the model generates a summary of the earlier context. Th
|
|
|
408
564
|
When using `streamText`, you can detect compaction summaries by checking the `providerMetadata` on `text-start` events:
|
|
409
565
|
|
|
410
566
|
```ts
|
|
411
|
-
for await (const part of result.
|
|
567
|
+
for await (const part of result.stream) {
|
|
412
568
|
switch (part.type) {
|
|
413
569
|
case 'text-start': {
|
|
414
570
|
const isCompaction =
|
|
@@ -484,13 +640,12 @@ For more details, see [Anthropic's Context Management documentation](https://doc
|
|
|
484
640
|
In the messages and message parts, you can use the `providerOptions` property to set cache control breakpoints.
|
|
485
641
|
You need to set the `anthropic` property in the `providerOptions` object to `{ cacheControl: { type: 'ephemeral' } }` to set a cache control breakpoint.
|
|
486
642
|
|
|
487
|
-
|
|
488
|
-
for `generateText
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
`onFinish` callback.
|
|
643
|
+
Cache read and cache write (creation) token counts are returned on the standard
|
|
644
|
+
`usage` object for both `generateText` and `streamText`. You can access them at
|
|
645
|
+
`result.usage.inputTokenDetails.cacheReadTokens` and
|
|
646
|
+
`result.usage.inputTokenDetails.cacheWriteTokens`.
|
|
492
647
|
|
|
493
|
-
```ts highlight="8,18-20,29-
|
|
648
|
+
```ts highlight="8,18-20,29-32"
|
|
494
649
|
import { anthropic } from '@ai-sdk/anthropic';
|
|
495
650
|
import { generateText } from 'ai';
|
|
496
651
|
|
|
@@ -517,8 +672,14 @@ const result = await generateText({
|
|
|
517
672
|
});
|
|
518
673
|
|
|
519
674
|
console.log(result.text);
|
|
520
|
-
console.log(
|
|
521
|
-
|
|
675
|
+
console.log(
|
|
676
|
+
'Cache read tokens:',
|
|
677
|
+
result.usage.inputTokenDetails.cacheReadTokens,
|
|
678
|
+
);
|
|
679
|
+
console.log(
|
|
680
|
+
'Cache write tokens:',
|
|
681
|
+
result.usage.inputTokenDetails.cacheWriteTokens,
|
|
682
|
+
);
|
|
522
683
|
```
|
|
523
684
|
|
|
524
685
|
You can also use cache control on system messages by providing multiple system messages at the head of your messages array:
|
|
@@ -621,14 +782,31 @@ For more on prompt caching with Anthropic, see [Anthropic's Cache Control docume
|
|
|
621
782
|
|
|
622
783
|
### Bash Tool
|
|
623
784
|
|
|
624
|
-
The Bash Tool allows running bash commands.
|
|
785
|
+
The Bash Tool allows running bash commands. By default, the tool executes
|
|
786
|
+
commands through the `experimental_sandbox` that you pass to `generateText` or `streamText`:
|
|
625
787
|
|
|
626
788
|
```ts
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
789
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
790
|
+
import { generateText, isStepCount } from 'ai';
|
|
791
|
+
|
|
792
|
+
const result = await generateText({
|
|
793
|
+
model: anthropic('claude-opus-4-8'),
|
|
794
|
+
tools: {
|
|
795
|
+
bash: anthropic.tools.bash_20250124(),
|
|
631
796
|
},
|
|
797
|
+
experimental_sandbox: {
|
|
798
|
+
description: 'A sandboxed shell environment.',
|
|
799
|
+
run: async ({ command }) => {
|
|
800
|
+
// Run the command in your sandbox and return the result.
|
|
801
|
+
return {
|
|
802
|
+
exitCode: 0,
|
|
803
|
+
stdout: `Executed: ${command}`,
|
|
804
|
+
stderr: '',
|
|
805
|
+
};
|
|
806
|
+
},
|
|
807
|
+
},
|
|
808
|
+
stopWhen: isStepCount(2),
|
|
809
|
+
prompt: 'List the files in the current directory.',
|
|
632
810
|
});
|
|
633
811
|
```
|
|
634
812
|
|
|
@@ -642,6 +820,9 @@ Parameters:
|
|
|
642
820
|
Only certain Claude versions are supported.
|
|
643
821
|
</Note>
|
|
644
822
|
|
|
823
|
+
You can also provide a custom `execute` function when you want to handle bash
|
|
824
|
+
execution directly instead of using the request experimental sandbox.
|
|
825
|
+
|
|
645
826
|
### Memory Tool
|
|
646
827
|
|
|
647
828
|
The [Memory Tool](https://docs.claude.com/en/docs/agents-and-tools/tool-use/memory-tool) allows Claude to use a local memory, e.g. in the filesystem.
|
|
@@ -741,7 +922,7 @@ const computerTool = anthropic.tools.computer_20251124({
|
|
|
741
922
|
toModelOutput({ output }) {
|
|
742
923
|
return typeof output === 'string'
|
|
743
924
|
? [{ type: 'text', text: output }]
|
|
744
|
-
: [{ type: '
|
|
925
|
+
: [{ type: 'file-data', data: output.data, mediaType: 'image/png' }];
|
|
745
926
|
},
|
|
746
927
|
});
|
|
747
928
|
```
|
|
@@ -854,6 +1035,63 @@ const result = await generateText({
|
|
|
854
1035
|
});
|
|
855
1036
|
```
|
|
856
1037
|
|
|
1038
|
+
### Advisor Tool
|
|
1039
|
+
|
|
1040
|
+
Anthropic provides a provider-executed advisor tool that lets an executor model consult a stronger advisor model during generation. This is useful for long-running coding, research, and agentic tasks where a stronger model can provide planning or course correction while the main response is generated by a faster or lower-cost model.
|
|
1041
|
+
|
|
1042
|
+
You can enable the advisor tool using `anthropic.tools.advisor_20260301`:
|
|
1043
|
+
|
|
1044
|
+
```ts
|
|
1045
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
1046
|
+
import { generateText } from 'ai';
|
|
1047
|
+
|
|
1048
|
+
const result = await generateText({
|
|
1049
|
+
model: anthropic('claude-sonnet-4-6'),
|
|
1050
|
+
system: 'You have access to an `advisor` tool backed by a stronger reviewer model.'
|
|
1051
|
+
prompt:
|
|
1052
|
+
'Build a concurrent worker pool in Go with graceful shutdown. Outline the design first.',
|
|
1053
|
+
tools: {
|
|
1054
|
+
advisor: anthropic.tools.advisor_20260301({
|
|
1055
|
+
model: 'claude-opus-4-8',
|
|
1056
|
+
maxUses: 3,
|
|
1057
|
+
}),
|
|
1058
|
+
},
|
|
1059
|
+
});
|
|
1060
|
+
```
|
|
1061
|
+
|
|
1062
|
+
<Note>
|
|
1063
|
+
The AI SDK automatically sends the required `advisor-tool-2026-03-01` beta
|
|
1064
|
+
header when you include this tool.
|
|
1065
|
+
</Note>
|
|
1066
|
+
|
|
1067
|
+
#### Configuration Options
|
|
1068
|
+
|
|
1069
|
+
The advisor tool supports the following configuration options:
|
|
1070
|
+
|
|
1071
|
+
- **model** _string_
|
|
1072
|
+
|
|
1073
|
+
Required. The advisor model ID, such as `claude-opus-4-8`. The advisor model must be at least as capable as the executor model.
|
|
1074
|
+
|
|
1075
|
+
- **maxUses** _number_
|
|
1076
|
+
|
|
1077
|
+
Optional. Maximum number of advisor calls allowed in a single request. Once the executor reaches this cap, further advisor calls return an `advisor_tool_result_error` with `errorCode: 'max_uses_exceeded'`, and the executor continues without further advice.
|
|
1078
|
+
|
|
1079
|
+
- **caching** _object_
|
|
1080
|
+
|
|
1081
|
+
Optional. Enables prompt caching for the advisor's own transcript across calls within a conversation. Use `{ type: 'ephemeral', ttl: '5m' | '1h' }`. This is most useful for longer agent loops where you expect at least three advisor calls.
|
|
1082
|
+
|
|
1083
|
+
#### How It Works
|
|
1084
|
+
|
|
1085
|
+
The advisor tool is provider-executed, so you do not provide an `execute` function. When the executor model calls `advisor`, Anthropic runs a separate server-side inference with the advisor model. The advisor sees the transcript and returns guidance to the executor, which then continues generating within the same request.
|
|
1086
|
+
|
|
1087
|
+
The advisor tool input is always empty. Anthropic constructs the advisor's view from the full transcript automatically.
|
|
1088
|
+
|
|
1089
|
+
For multi-turn conversations, pass the full assistant content, including advisor tool results, back on subsequent turns. If you remove the advisor tool from a later request, also remove previous advisor tool results from the message history; otherwise the Anthropic API returns an invalid request error.
|
|
1090
|
+
|
|
1091
|
+
#### Streaming
|
|
1092
|
+
|
|
1093
|
+
Advisor sub-inferences do not stream. When using `streamText`, the executor stream pauses while the advisor runs, then the advisor result arrives as a single tool result before executor output resumes.
|
|
1094
|
+
|
|
857
1095
|
### Tool Search
|
|
858
1096
|
|
|
859
1097
|
Anthropic provides provider-defined tool search tools that enable Claude to work with hundreds or thousands of tools by dynamically discovering and loading them on-demand. Instead of loading all tool definitions into the context window upfront, Claude searches your tool catalog and loads only the tools it needs.
|
|
@@ -1096,6 +1334,48 @@ const result = await generateText({
|
|
|
1096
1334
|
file operations), and `codeExecution_20250522` (supports Bash only).
|
|
1097
1335
|
</Note>
|
|
1098
1336
|
|
|
1337
|
+
#### Uploading Files for Code Execution
|
|
1338
|
+
|
|
1339
|
+
You can upload files via the [Files API](https://docs.anthropic.com/en/docs/build-with-claude/files) and make them available inside the code execution sandbox. First upload the file, then reference it in your message using `containerUpload: true` in the provider options:
|
|
1340
|
+
|
|
1341
|
+
```ts
|
|
1342
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
1343
|
+
import { generateText, uploadFile } from 'ai';
|
|
1344
|
+
import * as fs from 'fs';
|
|
1345
|
+
|
|
1346
|
+
const { providerReference } = await uploadFile({
|
|
1347
|
+
api: anthropic.files(),
|
|
1348
|
+
data: fs.readFileSync('./data.csv'),
|
|
1349
|
+
filename: 'data.csv',
|
|
1350
|
+
mediaType: 'text/csv',
|
|
1351
|
+
});
|
|
1352
|
+
|
|
1353
|
+
const result = await generateText({
|
|
1354
|
+
model: anthropic('claude-sonnet-4-5'),
|
|
1355
|
+
tools: {
|
|
1356
|
+
code_execution: anthropic.tools.codeExecution_20250825(),
|
|
1357
|
+
},
|
|
1358
|
+
messages: [
|
|
1359
|
+
{
|
|
1360
|
+
role: 'user',
|
|
1361
|
+
content: [
|
|
1362
|
+
{ type: 'text', text: 'Analyze this CSV data' },
|
|
1363
|
+
{
|
|
1364
|
+
type: 'file',
|
|
1365
|
+
mediaType: 'text/csv',
|
|
1366
|
+
data: { type: 'reference', reference: providerReference },
|
|
1367
|
+
providerOptions: {
|
|
1368
|
+
anthropic: { containerUpload: true },
|
|
1369
|
+
},
|
|
1370
|
+
},
|
|
1371
|
+
],
|
|
1372
|
+
},
|
|
1373
|
+
],
|
|
1374
|
+
});
|
|
1375
|
+
```
|
|
1376
|
+
|
|
1377
|
+
Supported file types include CSV, Excel, JSON, XML, images (JPEG, PNG, GIF, WebP), and text files.
|
|
1378
|
+
|
|
1099
1379
|
#### Error Handling
|
|
1100
1380
|
|
|
1101
1381
|
Code execution errors are handled differently depending on whether you're using streaming or non-streaming:
|
|
@@ -1155,12 +1435,12 @@ import {
|
|
|
1155
1435
|
anthropic,
|
|
1156
1436
|
forwardAnthropicContainerIdFromLastStep,
|
|
1157
1437
|
} from '@ai-sdk/anthropic';
|
|
1158
|
-
import { generateText, tool,
|
|
1438
|
+
import { generateText, tool, isStepCount } from 'ai';
|
|
1159
1439
|
import { z } from 'zod';
|
|
1160
1440
|
|
|
1161
1441
|
const result = await generateText({
|
|
1162
1442
|
model: anthropic('claude-sonnet-4-5'),
|
|
1163
|
-
stopWhen:
|
|
1443
|
+
stopWhen: isStepCount(10),
|
|
1164
1444
|
prompt:
|
|
1165
1445
|
'Get the weather for Tokyo, Sydney, and London, then calculate the average temperature.',
|
|
1166
1446
|
tools: {
|
|
@@ -1345,6 +1625,9 @@ and the `mediaType` should be set to `'application/pdf'`.
|
|
|
1345
1625
|
|
|
1346
1626
|
| Model | Image Input | Object Generation | Tool Usage | Computer Use | Web Search | Tool Search | Compaction |
|
|
1347
1627
|
| ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
1628
|
+
| `claude-fable-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
1629
|
+
| `claude-opus-4-8` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
1630
|
+
| `claude-opus-4-7` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
1348
1631
|
| `claude-opus-4-6` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
1349
1632
|
| `claude-sonnet-4-6` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
|
|
1350
1633
|
| `claude-opus-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.67",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"license": "Apache-2.0",
|
|
5
6
|
"sideEffects": false,
|
|
6
7
|
"main": "./dist/index.js",
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
@@ -25,41 +25,42 @@
|
|
|
25
25
|
"./package.json": "./package.json",
|
|
26
26
|
".": {
|
|
27
27
|
"types": "./dist/index.d.ts",
|
|
28
|
-
"import": "./dist/index.
|
|
29
|
-
"
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
30
|
},
|
|
31
31
|
"./internal": {
|
|
32
32
|
"types": "./dist/internal/index.d.ts",
|
|
33
|
-
"import": "./dist/internal/index.
|
|
34
|
-
"
|
|
35
|
-
"require": "./dist/internal/index.js"
|
|
33
|
+
"import": "./dist/internal/index.js",
|
|
34
|
+
"default": "./dist/internal/index.js"
|
|
36
35
|
}
|
|
37
36
|
},
|
|
38
37
|
"dependencies": {
|
|
39
|
-
"@ai-sdk/provider": "4.0.0-beta.
|
|
40
|
-
"@ai-sdk/provider-utils": "5.0.0-beta.
|
|
38
|
+
"@ai-sdk/provider": "4.0.0-beta.19",
|
|
39
|
+
"@ai-sdk/provider-utils": "5.0.0-beta.49"
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
|
-
"@types/node": "
|
|
44
|
-
"tsup": "^8",
|
|
42
|
+
"@types/node": "22.19.19",
|
|
43
|
+
"tsup": "^8.5.1",
|
|
45
44
|
"typescript": "5.8.3",
|
|
46
45
|
"zod": "3.25.76",
|
|
47
|
-
"@ai-sdk/test-server": "2.0.0-beta.
|
|
46
|
+
"@ai-sdk/test-server": "2.0.0-beta.7",
|
|
48
47
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
49
48
|
},
|
|
50
49
|
"peerDependencies": {
|
|
51
50
|
"zod": "^3.25.76 || ^4.1.8"
|
|
52
51
|
},
|
|
53
52
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
53
|
+
"node": ">=22"
|
|
55
54
|
},
|
|
56
55
|
"publishConfig": {
|
|
57
|
-
"access": "public"
|
|
56
|
+
"access": "public",
|
|
57
|
+
"provenance": true
|
|
58
58
|
},
|
|
59
59
|
"homepage": "https://ai-sdk.dev/docs",
|
|
60
60
|
"repository": {
|
|
61
61
|
"type": "git",
|
|
62
|
-
"url": "
|
|
62
|
+
"url": "https://github.com/vercel/ai",
|
|
63
|
+
"directory": "packages/anthropic"
|
|
63
64
|
},
|
|
64
65
|
"bugs": {
|
|
65
66
|
"url": "https://github.com/vercel/ai/issues"
|
|
@@ -71,9 +72,7 @@
|
|
|
71
72
|
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
72
73
|
"build:watch": "pnpm clean && tsup --watch --tsconfig tsconfig.build.json",
|
|
73
74
|
"clean": "del-cli dist docs *.tsbuildinfo",
|
|
74
|
-
"lint": "eslint \"./**/*.ts*\"",
|
|
75
75
|
"type-check": "tsc --build",
|
|
76
|
-
"prettier-check": "prettier --check \"./**/*.ts*\"",
|
|
77
76
|
"test": "pnpm test:node && pnpm test:edge",
|
|
78
77
|
"test:update": "pnpm test:node -u",
|
|
79
78
|
"test:watch": "vitest --config vitest.node.config.js",
|