@ai-sdk/anthropic 3.0.76 → 3.0.77
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 +6 -0
- package/dist/index.d.mts +99 -4
- package/dist/index.d.ts +99 -4
- package/dist/index.js +810 -454
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +790 -430
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +63 -0
- package/dist/internal/index.d.ts +63 -0
- package/dist/internal/index.js +803 -447
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +789 -429
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +57 -0
- package/package.json +1 -1
- package/src/anthropic-message-metadata.ts +63 -13
- package/src/anthropic-messages-api.ts +113 -11
- package/src/anthropic-messages-language-model.ts +186 -11
- package/src/anthropic-prepare-tools.ts +17 -0
- package/src/anthropic-tools.ts +31 -0
- package/src/convert-anthropic-messages-usage.ts +50 -22
- package/src/convert-to-anthropic-messages-prompt.ts +62 -0
- package/src/tool/advisor_20260301.ts +128 -0
package/docs/05-anthropic.mdx
CHANGED
|
@@ -954,6 +954,63 @@ const result = await generateText({
|
|
|
954
954
|
});
|
|
955
955
|
```
|
|
956
956
|
|
|
957
|
+
### Advisor Tool
|
|
958
|
+
|
|
959
|
+
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.
|
|
960
|
+
|
|
961
|
+
You can enable the advisor tool using `anthropic.tools.advisor_20260301`:
|
|
962
|
+
|
|
963
|
+
```ts
|
|
964
|
+
import { anthropic } from '@ai-sdk/anthropic';
|
|
965
|
+
import { generateText } from 'ai';
|
|
966
|
+
|
|
967
|
+
const result = await generateText({
|
|
968
|
+
model: anthropic('claude-sonnet-4-6'),
|
|
969
|
+
system: 'You have access to an `advisor` tool backed by a stronger reviewer model.'
|
|
970
|
+
prompt:
|
|
971
|
+
'Build a concurrent worker pool in Go with graceful shutdown. Outline the design first.',
|
|
972
|
+
tools: {
|
|
973
|
+
advisor: anthropic.tools.advisor_20260301({
|
|
974
|
+
model: 'claude-opus-4-7',
|
|
975
|
+
maxUses: 3,
|
|
976
|
+
}),
|
|
977
|
+
},
|
|
978
|
+
});
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
<Note>
|
|
982
|
+
The AI SDK automatically sends the required `advisor-tool-2026-03-01` beta
|
|
983
|
+
header when you include this tool.
|
|
984
|
+
</Note>
|
|
985
|
+
|
|
986
|
+
#### Configuration Options
|
|
987
|
+
|
|
988
|
+
The advisor tool supports the following configuration options:
|
|
989
|
+
|
|
990
|
+
- **model** _string_
|
|
991
|
+
|
|
992
|
+
Required. The advisor model ID, such as `claude-opus-4-7`. The advisor model must be at least as capable as the executor model.
|
|
993
|
+
|
|
994
|
+
- **maxUses** _number_
|
|
995
|
+
|
|
996
|
+
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.
|
|
997
|
+
|
|
998
|
+
- **caching** _object_
|
|
999
|
+
|
|
1000
|
+
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.
|
|
1001
|
+
|
|
1002
|
+
#### How It Works
|
|
1003
|
+
|
|
1004
|
+
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.
|
|
1005
|
+
|
|
1006
|
+
The advisor tool input is always empty. Anthropic constructs the advisor's view from the full transcript automatically.
|
|
1007
|
+
|
|
1008
|
+
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.
|
|
1009
|
+
|
|
1010
|
+
#### Streaming
|
|
1011
|
+
|
|
1012
|
+
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.
|
|
1013
|
+
|
|
957
1014
|
### Tool Search
|
|
958
1015
|
|
|
959
1016
|
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.
|
package/package.json
CHANGED
|
@@ -2,22 +2,72 @@ import type { JSONObject } from '@ai-sdk/provider';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents a single iteration in the usage breakdown.
|
|
5
|
-
*
|
|
6
|
-
* usage for each sampling
|
|
5
|
+
*
|
|
6
|
+
* The API returns an iterations array showing usage for each sampling
|
|
7
|
+
* iteration. Iterations can be:
|
|
8
|
+
* - `compaction`: a context compaction step (billed at executor rates).
|
|
9
|
+
* - `message`: an executor sampling iteration (billed at executor rates).
|
|
10
|
+
* - `advisor_message`: an advisor sub-inference (billed at the advisor
|
|
11
|
+
* model's rates; `model` carries the advisor model ID). Advisor token
|
|
12
|
+
* usage is NOT rolled into the top-level usage totals because it bills
|
|
13
|
+
* at a different rate; inspect this array directly for advisor billing.
|
|
7
14
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
15
|
+
export type AnthropicUsageIteration =
|
|
16
|
+
| {
|
|
17
|
+
type: 'compaction' | 'message';
|
|
10
18
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Number of input tokens consumed in this iteration.
|
|
21
|
+
*/
|
|
22
|
+
inputTokens: number;
|
|
15
23
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Number of output tokens generated in this iteration.
|
|
26
|
+
*/
|
|
27
|
+
outputTokens: number;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Number of cache-creation input tokens consumed in this iteration.
|
|
31
|
+
*/
|
|
32
|
+
cacheCreationInputTokens?: number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Number of cache-read input tokens consumed in this iteration.
|
|
36
|
+
*/
|
|
37
|
+
cacheReadInputTokens?: number;
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
type: 'advisor_message';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The advisor model that produced this iteration.
|
|
44
|
+
*/
|
|
45
|
+
model: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Number of input tokens consumed in this iteration.
|
|
49
|
+
*/
|
|
50
|
+
inputTokens: number;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Number of output tokens generated in this iteration.
|
|
54
|
+
*/
|
|
55
|
+
outputTokens: number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Number of cache-creation input tokens consumed by this advisor
|
|
59
|
+
* sub-inference. Nonzero when advisor-side caching is enabled and
|
|
60
|
+
* the advisor writes a fresh cache entry.
|
|
61
|
+
*/
|
|
62
|
+
cacheCreationInputTokens?: number;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Number of cache-read input tokens consumed by this advisor
|
|
66
|
+
* sub-inference. Nonzero on the second and later advisor calls
|
|
67
|
+
* when advisor-side caching is enabled.
|
|
68
|
+
*/
|
|
69
|
+
cacheReadInputTokens?: number;
|
|
70
|
+
};
|
|
21
71
|
|
|
22
72
|
export interface AnthropicMessageMetadata {
|
|
23
73
|
usage: JSONObject;
|
|
@@ -42,6 +42,7 @@ export interface AnthropicAssistantMessage {
|
|
|
42
42
|
| AnthropicToolSearchToolResultContent
|
|
43
43
|
| AnthropicBashCodeExecutionToolResultContent
|
|
44
44
|
| AnthropicTextEditorCodeExecutionToolResultContent
|
|
45
|
+
| AnthropicAdvisorToolResultContent
|
|
45
46
|
| AnthropicMcpToolUseContent
|
|
46
47
|
| AnthropicMcpToolResultContent
|
|
47
48
|
| AnthropicCompactionContent
|
|
@@ -151,7 +152,9 @@ export interface AnthropicServerToolUseContent {
|
|
|
151
152
|
| 'text_editor_code_execution'
|
|
152
153
|
// tool search:
|
|
153
154
|
| 'tool_search_tool_regex'
|
|
154
|
-
| 'tool_search_tool_bm25'
|
|
155
|
+
| 'tool_search_tool_bm25'
|
|
156
|
+
// advisor:
|
|
157
|
+
| 'advisor';
|
|
155
158
|
input: unknown;
|
|
156
159
|
cache_control: AnthropicCacheControl | undefined;
|
|
157
160
|
}
|
|
@@ -310,6 +313,30 @@ export interface AnthropicBashCodeExecutionToolResultContent {
|
|
|
310
313
|
cache_control: AnthropicCacheControl | undefined;
|
|
311
314
|
}
|
|
312
315
|
|
|
316
|
+
export interface AnthropicAdvisorToolResultContent {
|
|
317
|
+
type: 'advisor_tool_result';
|
|
318
|
+
tool_use_id: string;
|
|
319
|
+
content:
|
|
320
|
+
| {
|
|
321
|
+
type: 'advisor_result';
|
|
322
|
+
text: string;
|
|
323
|
+
}
|
|
324
|
+
| {
|
|
325
|
+
type: 'advisor_redacted_result';
|
|
326
|
+
encrypted_content: string;
|
|
327
|
+
}
|
|
328
|
+
| {
|
|
329
|
+
type: 'advisor_tool_result_error';
|
|
330
|
+
/**
|
|
331
|
+
* Available options: `max_uses_exceeded`, `too_many_requests`,
|
|
332
|
+
* `overloaded`, `prompt_too_long`, `execution_time_exceeded`,
|
|
333
|
+
* `unavailable`.
|
|
334
|
+
*/
|
|
335
|
+
error_code: string;
|
|
336
|
+
};
|
|
337
|
+
cache_control: AnthropicCacheControl | undefined;
|
|
338
|
+
}
|
|
339
|
+
|
|
313
340
|
export interface AnthropicWebFetchToolResultContent {
|
|
314
341
|
type: 'web_fetch_tool_result';
|
|
315
342
|
tool_use_id: string;
|
|
@@ -459,6 +486,16 @@ export type AnthropicTool =
|
|
|
459
486
|
| {
|
|
460
487
|
type: 'tool_search_tool_bm25_20251119';
|
|
461
488
|
name: string;
|
|
489
|
+
}
|
|
490
|
+
| {
|
|
491
|
+
type: 'advisor_20260301';
|
|
492
|
+
name: 'advisor';
|
|
493
|
+
model: string;
|
|
494
|
+
max_uses?: number;
|
|
495
|
+
caching?: {
|
|
496
|
+
type: 'ephemeral';
|
|
497
|
+
ttl: '5m' | '1h';
|
|
498
|
+
};
|
|
462
499
|
};
|
|
463
500
|
|
|
464
501
|
export type AnthropicSpeed = 'fast' | 'standard';
|
|
@@ -832,6 +869,25 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
832
869
|
}),
|
|
833
870
|
]),
|
|
834
871
|
}),
|
|
872
|
+
// advisor results for advisor_20260301:
|
|
873
|
+
z.object({
|
|
874
|
+
type: z.literal('advisor_tool_result'),
|
|
875
|
+
tool_use_id: z.string(),
|
|
876
|
+
content: z.discriminatedUnion('type', [
|
|
877
|
+
z.object({
|
|
878
|
+
type: z.literal('advisor_result'),
|
|
879
|
+
text: z.string(),
|
|
880
|
+
}),
|
|
881
|
+
z.object({
|
|
882
|
+
type: z.literal('advisor_redacted_result'),
|
|
883
|
+
encrypted_content: z.string(),
|
|
884
|
+
}),
|
|
885
|
+
z.object({
|
|
886
|
+
type: z.literal('advisor_tool_result_error'),
|
|
887
|
+
error_code: z.string(),
|
|
888
|
+
}),
|
|
889
|
+
]),
|
|
890
|
+
}),
|
|
835
891
|
]),
|
|
836
892
|
),
|
|
837
893
|
stop_reason: z.string().nullish(),
|
|
@@ -843,11 +899,23 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
843
899
|
cache_read_input_tokens: z.number().nullish(),
|
|
844
900
|
iterations: z
|
|
845
901
|
.array(
|
|
846
|
-
z.
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
902
|
+
z.union([
|
|
903
|
+
z.object({
|
|
904
|
+
type: z.union([z.literal('compaction'), z.literal('message')]),
|
|
905
|
+
input_tokens: z.number(),
|
|
906
|
+
output_tokens: z.number(),
|
|
907
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
908
|
+
cache_read_input_tokens: z.number().nullish(),
|
|
909
|
+
}),
|
|
910
|
+
z.object({
|
|
911
|
+
type: z.literal('advisor_message'),
|
|
912
|
+
model: z.string(),
|
|
913
|
+
input_tokens: z.number(),
|
|
914
|
+
output_tokens: z.number(),
|
|
915
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
916
|
+
cache_read_input_tokens: z.number().nullish(),
|
|
917
|
+
}),
|
|
918
|
+
]),
|
|
851
919
|
)
|
|
852
920
|
.nullish(),
|
|
853
921
|
}),
|
|
@@ -1190,6 +1258,25 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1190
1258
|
}),
|
|
1191
1259
|
]),
|
|
1192
1260
|
}),
|
|
1261
|
+
// advisor results for advisor_20260301:
|
|
1262
|
+
z.object({
|
|
1263
|
+
type: z.literal('advisor_tool_result'),
|
|
1264
|
+
tool_use_id: z.string(),
|
|
1265
|
+
content: z.discriminatedUnion('type', [
|
|
1266
|
+
z.object({
|
|
1267
|
+
type: z.literal('advisor_result'),
|
|
1268
|
+
text: z.string(),
|
|
1269
|
+
}),
|
|
1270
|
+
z.object({
|
|
1271
|
+
type: z.literal('advisor_redacted_result'),
|
|
1272
|
+
encrypted_content: z.string(),
|
|
1273
|
+
}),
|
|
1274
|
+
z.object({
|
|
1275
|
+
type: z.literal('advisor_tool_result_error'),
|
|
1276
|
+
error_code: z.string(),
|
|
1277
|
+
}),
|
|
1278
|
+
]),
|
|
1279
|
+
}),
|
|
1193
1280
|
]),
|
|
1194
1281
|
}),
|
|
1195
1282
|
z.object({
|
|
@@ -1288,11 +1375,26 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1288
1375
|
cache_read_input_tokens: z.number().nullish(),
|
|
1289
1376
|
iterations: z
|
|
1290
1377
|
.array(
|
|
1291
|
-
z.
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1378
|
+
z.union([
|
|
1379
|
+
z.object({
|
|
1380
|
+
type: z.union([
|
|
1381
|
+
z.literal('compaction'),
|
|
1382
|
+
z.literal('message'),
|
|
1383
|
+
]),
|
|
1384
|
+
input_tokens: z.number(),
|
|
1385
|
+
output_tokens: z.number(),
|
|
1386
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
1387
|
+
cache_read_input_tokens: z.number().nullish(),
|
|
1388
|
+
}),
|
|
1389
|
+
z.object({
|
|
1390
|
+
type: z.literal('advisor_message'),
|
|
1391
|
+
model: z.string(),
|
|
1392
|
+
input_tokens: z.number(),
|
|
1393
|
+
output_tokens: z.number(),
|
|
1394
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
1395
|
+
cache_read_input_tokens: z.number().nullish(),
|
|
1396
|
+
}),
|
|
1397
|
+
]),
|
|
1296
1398
|
)
|
|
1297
1399
|
.nullish(),
|
|
1298
1400
|
}),
|
|
@@ -30,7 +30,10 @@ import {
|
|
|
30
30
|
type Resolvable,
|
|
31
31
|
} from '@ai-sdk/provider-utils';
|
|
32
32
|
import { anthropicFailedResponseHandler } from './anthropic-error';
|
|
33
|
-
import type {
|
|
33
|
+
import type {
|
|
34
|
+
AnthropicMessageMetadata,
|
|
35
|
+
AnthropicUsageIteration,
|
|
36
|
+
} from './anthropic-message-metadata';
|
|
34
37
|
import {
|
|
35
38
|
anthropicMessagesChunkSchema,
|
|
36
39
|
anthropicMessagesResponseSchema,
|
|
@@ -355,6 +358,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
355
358
|
'anthropic.web_fetch_20260209': 'web_fetch',
|
|
356
359
|
'anthropic.tool_search_regex_20251119': 'tool_search_tool_regex',
|
|
357
360
|
'anthropic.tool_search_bm25_20251119': 'tool_search_tool_bm25',
|
|
361
|
+
'anthropic.advisor_20260301': 'advisor',
|
|
358
362
|
},
|
|
359
363
|
});
|
|
360
364
|
|
|
@@ -1000,6 +1004,14 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1000
1004
|
input: JSON.stringify(part.input),
|
|
1001
1005
|
providerExecuted: true,
|
|
1002
1006
|
});
|
|
1007
|
+
} else if (part.name === 'advisor') {
|
|
1008
|
+
content.push({
|
|
1009
|
+
type: 'tool-call',
|
|
1010
|
+
toolCallId: part.id,
|
|
1011
|
+
toolName: toolNameMapping.toCustomToolName('advisor'),
|
|
1012
|
+
input: JSON.stringify(part.input),
|
|
1013
|
+
providerExecuted: true,
|
|
1014
|
+
});
|
|
1003
1015
|
}
|
|
1004
1016
|
|
|
1005
1017
|
break;
|
|
@@ -1218,6 +1230,44 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1218
1230
|
}
|
|
1219
1231
|
break;
|
|
1220
1232
|
}
|
|
1233
|
+
|
|
1234
|
+
// advisor results for advisor_20260301:
|
|
1235
|
+
case 'advisor_tool_result': {
|
|
1236
|
+
const advisorToolName = toolNameMapping.toCustomToolName('advisor');
|
|
1237
|
+
if (part.content.type === 'advisor_result') {
|
|
1238
|
+
content.push({
|
|
1239
|
+
type: 'tool-result',
|
|
1240
|
+
toolCallId: part.tool_use_id,
|
|
1241
|
+
toolName: advisorToolName,
|
|
1242
|
+
result: {
|
|
1243
|
+
type: 'advisor_result',
|
|
1244
|
+
text: part.content.text,
|
|
1245
|
+
},
|
|
1246
|
+
});
|
|
1247
|
+
} else if (part.content.type === 'advisor_redacted_result') {
|
|
1248
|
+
content.push({
|
|
1249
|
+
type: 'tool-result',
|
|
1250
|
+
toolCallId: part.tool_use_id,
|
|
1251
|
+
toolName: advisorToolName,
|
|
1252
|
+
result: {
|
|
1253
|
+
type: 'advisor_redacted_result',
|
|
1254
|
+
encryptedContent: part.content.encrypted_content,
|
|
1255
|
+
},
|
|
1256
|
+
});
|
|
1257
|
+
} else {
|
|
1258
|
+
content.push({
|
|
1259
|
+
type: 'tool-result',
|
|
1260
|
+
toolCallId: part.tool_use_id,
|
|
1261
|
+
toolName: advisorToolName,
|
|
1262
|
+
isError: true,
|
|
1263
|
+
result: {
|
|
1264
|
+
type: 'advisor_tool_result_error',
|
|
1265
|
+
errorCode: part.content.error_code,
|
|
1266
|
+
},
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
break;
|
|
1270
|
+
}
|
|
1221
1271
|
}
|
|
1222
1272
|
}
|
|
1223
1273
|
|
|
@@ -1247,11 +1297,42 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1247
1297
|
stopSequence: response.stop_sequence ?? null,
|
|
1248
1298
|
|
|
1249
1299
|
iterations: response.usage.iterations
|
|
1250
|
-
? response.usage.iterations.map(iter =>
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1300
|
+
? response.usage.iterations.map(iter =>
|
|
1301
|
+
iter.type === 'advisor_message'
|
|
1302
|
+
? ({
|
|
1303
|
+
type: iter.type,
|
|
1304
|
+
model: iter.model,
|
|
1305
|
+
inputTokens: iter.input_tokens,
|
|
1306
|
+
outputTokens: iter.output_tokens,
|
|
1307
|
+
...(iter.cache_creation_input_tokens
|
|
1308
|
+
? {
|
|
1309
|
+
cacheCreationInputTokens:
|
|
1310
|
+
iter.cache_creation_input_tokens,
|
|
1311
|
+
}
|
|
1312
|
+
: {}),
|
|
1313
|
+
...(iter.cache_read_input_tokens
|
|
1314
|
+
? {
|
|
1315
|
+
cacheReadInputTokens: iter.cache_read_input_tokens,
|
|
1316
|
+
}
|
|
1317
|
+
: {}),
|
|
1318
|
+
} satisfies AnthropicUsageIteration)
|
|
1319
|
+
: ({
|
|
1320
|
+
type: iter.type,
|
|
1321
|
+
inputTokens: iter.input_tokens,
|
|
1322
|
+
outputTokens: iter.output_tokens,
|
|
1323
|
+
...(iter.cache_creation_input_tokens
|
|
1324
|
+
? {
|
|
1325
|
+
cacheCreationInputTokens:
|
|
1326
|
+
iter.cache_creation_input_tokens,
|
|
1327
|
+
}
|
|
1328
|
+
: {}),
|
|
1329
|
+
...(iter.cache_read_input_tokens
|
|
1330
|
+
? {
|
|
1331
|
+
cacheReadInputTokens: iter.cache_read_input_tokens,
|
|
1332
|
+
}
|
|
1333
|
+
: {}),
|
|
1334
|
+
} satisfies AnthropicUsageIteration),
|
|
1335
|
+
)
|
|
1255
1336
|
: null,
|
|
1256
1337
|
container: response.container
|
|
1257
1338
|
? {
|
|
@@ -1379,6 +1460,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1379
1460
|
| 'text_editor_code_execution_tool_result'
|
|
1380
1461
|
| 'bash_code_execution_tool_result'
|
|
1381
1462
|
| 'tool_search_tool_result'
|
|
1463
|
+
| 'advisor_tool_result'
|
|
1382
1464
|
| 'mcp_tool_use'
|
|
1383
1465
|
| 'mcp_tool_result'
|
|
1384
1466
|
| 'compaction'
|
|
@@ -1597,6 +1679,26 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1597
1679
|
providerToolName: part.name,
|
|
1598
1680
|
};
|
|
1599
1681
|
|
|
1682
|
+
controller.enqueue({
|
|
1683
|
+
type: 'tool-input-start',
|
|
1684
|
+
id: part.id,
|
|
1685
|
+
toolName: customToolName,
|
|
1686
|
+
providerExecuted: true,
|
|
1687
|
+
});
|
|
1688
|
+
} else if (part.name === 'advisor') {
|
|
1689
|
+
const customToolName =
|
|
1690
|
+
toolNameMapping.toCustomToolName('advisor');
|
|
1691
|
+
|
|
1692
|
+
contentBlocks[value.index] = {
|
|
1693
|
+
type: 'tool-call',
|
|
1694
|
+
toolCallId: part.id,
|
|
1695
|
+
toolName: customToolName,
|
|
1696
|
+
input: '{}',
|
|
1697
|
+
providerExecuted: true,
|
|
1698
|
+
firstDelta: true,
|
|
1699
|
+
providerToolName: part.name,
|
|
1700
|
+
};
|
|
1701
|
+
|
|
1600
1702
|
controller.enqueue({
|
|
1601
1703
|
type: 'tool-input-start',
|
|
1602
1704
|
id: part.id,
|
|
@@ -1808,6 +1910,46 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1808
1910
|
return;
|
|
1809
1911
|
}
|
|
1810
1912
|
|
|
1913
|
+
// advisor results for advisor_20260301:
|
|
1914
|
+
// arrives fully formed in a single content_block_start (no deltas).
|
|
1915
|
+
case 'advisor_tool_result': {
|
|
1916
|
+
const advisorToolName =
|
|
1917
|
+
toolNameMapping.toCustomToolName('advisor');
|
|
1918
|
+
if (part.content.type === 'advisor_result') {
|
|
1919
|
+
controller.enqueue({
|
|
1920
|
+
type: 'tool-result',
|
|
1921
|
+
toolCallId: part.tool_use_id,
|
|
1922
|
+
toolName: advisorToolName,
|
|
1923
|
+
result: {
|
|
1924
|
+
type: 'advisor_result',
|
|
1925
|
+
text: part.content.text,
|
|
1926
|
+
},
|
|
1927
|
+
});
|
|
1928
|
+
} else if (part.content.type === 'advisor_redacted_result') {
|
|
1929
|
+
controller.enqueue({
|
|
1930
|
+
type: 'tool-result',
|
|
1931
|
+
toolCallId: part.tool_use_id,
|
|
1932
|
+
toolName: advisorToolName,
|
|
1933
|
+
result: {
|
|
1934
|
+
type: 'advisor_redacted_result',
|
|
1935
|
+
encryptedContent: part.content.encrypted_content,
|
|
1936
|
+
},
|
|
1937
|
+
});
|
|
1938
|
+
} else {
|
|
1939
|
+
controller.enqueue({
|
|
1940
|
+
type: 'tool-result',
|
|
1941
|
+
toolCallId: part.tool_use_id,
|
|
1942
|
+
toolName: advisorToolName,
|
|
1943
|
+
isError: true,
|
|
1944
|
+
result: {
|
|
1945
|
+
type: 'advisor_tool_result_error',
|
|
1946
|
+
errorCode: part.content.error_code,
|
|
1947
|
+
},
|
|
1948
|
+
});
|
|
1949
|
+
}
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1811
1953
|
case 'mcp_tool_use': {
|
|
1812
1954
|
mcpToolCalls[part.id] = {
|
|
1813
1955
|
type: 'tool-call',
|
|
@@ -2232,11 +2374,44 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
2232
2374
|
cacheCreationInputTokens,
|
|
2233
2375
|
stopSequence,
|
|
2234
2376
|
iterations: usage.iterations
|
|
2235
|
-
? usage.iterations.map(iter =>
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2377
|
+
? usage.iterations.map(iter =>
|
|
2378
|
+
iter.type === 'advisor_message'
|
|
2379
|
+
? ({
|
|
2380
|
+
type: iter.type,
|
|
2381
|
+
model: iter.model,
|
|
2382
|
+
inputTokens: iter.input_tokens,
|
|
2383
|
+
outputTokens: iter.output_tokens,
|
|
2384
|
+
...(iter.cache_creation_input_tokens
|
|
2385
|
+
? {
|
|
2386
|
+
cacheCreationInputTokens:
|
|
2387
|
+
iter.cache_creation_input_tokens,
|
|
2388
|
+
}
|
|
2389
|
+
: {}),
|
|
2390
|
+
...(iter.cache_read_input_tokens
|
|
2391
|
+
? {
|
|
2392
|
+
cacheReadInputTokens:
|
|
2393
|
+
iter.cache_read_input_tokens,
|
|
2394
|
+
}
|
|
2395
|
+
: {}),
|
|
2396
|
+
} satisfies AnthropicUsageIteration)
|
|
2397
|
+
: ({
|
|
2398
|
+
type: iter.type,
|
|
2399
|
+
inputTokens: iter.input_tokens,
|
|
2400
|
+
outputTokens: iter.output_tokens,
|
|
2401
|
+
...(iter.cache_creation_input_tokens
|
|
2402
|
+
? {
|
|
2403
|
+
cacheCreationInputTokens:
|
|
2404
|
+
iter.cache_creation_input_tokens,
|
|
2405
|
+
}
|
|
2406
|
+
: {}),
|
|
2407
|
+
...(iter.cache_read_input_tokens
|
|
2408
|
+
? {
|
|
2409
|
+
cacheReadInputTokens:
|
|
2410
|
+
iter.cache_read_input_tokens,
|
|
2411
|
+
}
|
|
2412
|
+
: {}),
|
|
2413
|
+
} satisfies AnthropicUsageIteration),
|
|
2414
|
+
)
|
|
2240
2415
|
: null,
|
|
2241
2416
|
container,
|
|
2242
2417
|
contextManagement,
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
AnthropicToolChoice,
|
|
9
9
|
} from './anthropic-messages-api';
|
|
10
10
|
import { CacheControlValidator } from './get-cache-control';
|
|
11
|
+
import { advisor_20260301ArgsSchema } from './tool/advisor_20260301';
|
|
11
12
|
import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
|
|
12
13
|
import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
|
|
13
14
|
import { webSearch_20250305ArgsSchema } from './tool/web-search_20250305';
|
|
@@ -349,6 +350,22 @@ export async function prepareTools({
|
|
|
349
350
|
break;
|
|
350
351
|
}
|
|
351
352
|
|
|
353
|
+
case 'anthropic.advisor_20260301': {
|
|
354
|
+
betas.add('advisor-tool-2026-03-01');
|
|
355
|
+
const args = await validateTypes({
|
|
356
|
+
value: tool.args,
|
|
357
|
+
schema: advisor_20260301ArgsSchema,
|
|
358
|
+
});
|
|
359
|
+
anthropicTools.push({
|
|
360
|
+
type: 'advisor_20260301',
|
|
361
|
+
name: 'advisor',
|
|
362
|
+
model: args.model,
|
|
363
|
+
...(args.maxUses !== undefined && { max_uses: args.maxUses }),
|
|
364
|
+
...(args.caching !== undefined && { caching: args.caching }),
|
|
365
|
+
});
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
|
|
352
369
|
default: {
|
|
353
370
|
toolWarnings.push({
|
|
354
371
|
type: 'unsupported',
|
package/src/anthropic-tools.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { advisor_20260301 } from './tool/advisor_20260301';
|
|
1
2
|
import { bash_20241022 } from './tool/bash_20241022';
|
|
2
3
|
import { bash_20250124 } from './tool/bash_20250124';
|
|
3
4
|
import { codeExecution_20250522 } from './tool/code-execution_20250522';
|
|
@@ -19,6 +20,36 @@ import { webSearch_20260209 } from './tool/web-search_20260209';
|
|
|
19
20
|
import { webSearch_20250305 } from './tool/web-search_20250305';
|
|
20
21
|
|
|
21
22
|
export const anthropicTools = {
|
|
23
|
+
/**
|
|
24
|
+
* Pairs a faster executor model with a higher-intelligence advisor model
|
|
25
|
+
* that provides strategic guidance mid-generation.
|
|
26
|
+
*
|
|
27
|
+
* The advisor lets a faster, lower-cost executor model consult a
|
|
28
|
+
* higher-intelligence advisor model server-side. The advisor reads the
|
|
29
|
+
* executor's full transcript and produces a plan or course correction;
|
|
30
|
+
* the executor continues with the task, informed by the advice. All of
|
|
31
|
+
* this happens inside a single `/v1/messages` request.
|
|
32
|
+
*
|
|
33
|
+
* Beta header `advisor-tool-2026-03-01` is added automatically when this
|
|
34
|
+
* tool is included.
|
|
35
|
+
*
|
|
36
|
+
* Multi-turn conversations: pass the full assistant content (including
|
|
37
|
+
* `advisor_tool_result` blocks) back to the API on subsequent turns. If
|
|
38
|
+
* you omit the advisor tool from `tools` on a follow-up turn while the
|
|
39
|
+
* message history still contains `advisor_tool_result` blocks, the API
|
|
40
|
+
* returns a `400 invalid_request_error`.
|
|
41
|
+
*
|
|
42
|
+
* Supported executor models: Claude Haiku 4.5, Sonnet 4.6, Opus 4.6,
|
|
43
|
+
* Opus 4.7. The advisor must be at least as capable as the executor.
|
|
44
|
+
*
|
|
45
|
+
* @param model - The advisor model ID (required), e.g. `"claude-opus-4-7"`.
|
|
46
|
+
* @param maxUses - Maximum advisor calls per request (per-request cap).
|
|
47
|
+
* @param caching - Enables prompt caching for the advisor's transcript
|
|
48
|
+
* across calls within a conversation. Worthwhile from ~3 advisor calls
|
|
49
|
+
* per conversation.
|
|
50
|
+
*/
|
|
51
|
+
advisor_20260301,
|
|
52
|
+
|
|
22
53
|
/**
|
|
23
54
|
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
|
24
55
|
* allowing system operations, script execution, and command-line automation.
|