@ai-sdk/anthropic 4.0.113 → 4.0.115

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.
@@ -1587,6 +1587,8 @@ Shorter prompts cannot be cached, even if marked with `cacheControl`. Any reques
1587
1587
 
1588
1588
  For more on prompt caching with Anthropic, see [Anthropic's Cache Control documentation](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching).
1589
1589
 
1590
+ If the number of cached tokens drops after you load the conversation history from a database, see [Prompt Cache Misses With Stored Messages](/docs/troubleshooting/prompt-cache-misses-with-stored-messages).
1591
+
1590
1592
  <Note type="warning">
1591
1593
  Because the `UIMessage` type (used by AI SDK UI hooks like `useChat`) does not
1592
1594
  support the `providerOptions` property, you can use `convertToModelMessages`
@@ -2836,6 +2838,17 @@ and the `mediaType` should be set to `'application/pdf'`.
2836
2838
 
2837
2839
  ### Model Capabilities
2838
2840
 
2841
+ Use `anthropicModelCapabilities(modelId)` to look up a model's capabilities and
2842
+ provider options for the Messages API. Model IDs have autocomplete, and the lookup
2843
+ requires no API call. It returns `undefined` for unknown model IDs.
2844
+
2845
+ ```ts
2846
+ import { anthropicModelCapabilities } from '@ai-sdk/anthropic';
2847
+
2848
+ const model = anthropicModelCapabilities('claude-sonnet-5-5');
2849
+ console.log(model?.endpoints.messages);
2850
+ ```
2851
+
2839
2852
  <Note>
2840
2853
  `claude-opus-4-0`, `claude-opus-4-1`, and `claude-sonnet-4-0` are hidden from
2841
2854
  this catalog because requests returned 404s. Their historical capability
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.113",
3
+ "version": "4.0.115",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@ai-sdk/provider": "4.0.40",
39
- "@ai-sdk/provider-utils": "5.0.87"
39
+ "@ai-sdk/provider-utils": "5.0.88"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.4",
@@ -1160,6 +1160,206 @@ export const anthropicResponseSchema = lazySchema(() =>
1160
1160
  ),
1161
1161
  );
1162
1162
 
1163
+ function anthropicToolResultSchemas() {
1164
+ return [
1165
+ z.object({
1166
+ type: z.literal('mcp_tool_result'),
1167
+ tool_use_id: z.string(),
1168
+ is_error: z.boolean(),
1169
+ content: anthropicMcpToolResultContentSchema(),
1170
+ }),
1171
+ z.object({
1172
+ type: z.literal('web_fetch_tool_result'),
1173
+ tool_use_id: z.string(),
1174
+ caller: anthropicToolCallCallerSchema().optional(),
1175
+ content: z.union([
1176
+ z.object({
1177
+ type: z.literal('web_fetch_result'),
1178
+ url: z.string(),
1179
+ retrieved_at: z.string().nullable(),
1180
+ content: z.object({
1181
+ type: z.literal('document'),
1182
+ title: z.string().nullable(),
1183
+ citations: z.object({ enabled: z.boolean() }).optional(),
1184
+ source: z.union([
1185
+ z.object({
1186
+ type: z.literal('base64'),
1187
+ media_type: z.literal('application/pdf'),
1188
+ data: z.string(),
1189
+ }),
1190
+ z.object({
1191
+ type: z.literal('text'),
1192
+ media_type: z.literal('text/plain'),
1193
+ data: z.string(),
1194
+ }),
1195
+ ]),
1196
+ }),
1197
+ }),
1198
+ z.object({
1199
+ type: z.literal('web_fetch_tool_result_error'),
1200
+ error_code: z.string(),
1201
+ }),
1202
+ ]),
1203
+ }),
1204
+ z.object({
1205
+ type: z.literal('web_search_tool_result'),
1206
+ tool_use_id: z.string(),
1207
+ caller: anthropicToolCallCallerSchema().optional(),
1208
+ content: z.union([
1209
+ z.array(
1210
+ z.object({
1211
+ type: z.literal('web_search_result'),
1212
+ url: z.string(),
1213
+ title: z.string().nullable(),
1214
+ encrypted_content: z.string(),
1215
+ page_age: z.string().nullish(),
1216
+ }),
1217
+ ),
1218
+ z.object({
1219
+ type: z.literal('web_search_tool_result_error'),
1220
+ error_code: z.string(),
1221
+ }),
1222
+ ]),
1223
+ }),
1224
+ // code execution results for code_execution_20250522 tool:
1225
+ z.object({
1226
+ type: z.literal('code_execution_tool_result'),
1227
+ tool_use_id: z.string(),
1228
+ content: z.union([
1229
+ z.object({
1230
+ type: z.literal('code_execution_result'),
1231
+ stdout: z.string(),
1232
+ stderr: z.string(),
1233
+ return_code: z.number(),
1234
+ content: z
1235
+ .array(
1236
+ z.object({
1237
+ type: z.literal('code_execution_output'),
1238
+ file_id: z.string(),
1239
+ }),
1240
+ )
1241
+ .optional()
1242
+ .default([]),
1243
+ }),
1244
+ z.object({
1245
+ type: z.literal('encrypted_code_execution_result'),
1246
+ encrypted_stdout: z.string(),
1247
+ stderr: z.string(),
1248
+ return_code: z.number(),
1249
+ content: z
1250
+ .array(
1251
+ z.object({
1252
+ type: z.literal('code_execution_output'),
1253
+ file_id: z.string(),
1254
+ }),
1255
+ )
1256
+ .optional()
1257
+ .default([]),
1258
+ }),
1259
+ z.object({
1260
+ type: z.literal('code_execution_tool_result_error'),
1261
+ error_code: z.string(),
1262
+ }),
1263
+ ]),
1264
+ }),
1265
+ // bash code execution results for code_execution_20250825 tool:
1266
+ z.object({
1267
+ type: z.literal('bash_code_execution_tool_result'),
1268
+ tool_use_id: z.string(),
1269
+ content: z.discriminatedUnion('type', [
1270
+ z.object({
1271
+ type: z.literal('bash_code_execution_result'),
1272
+ content: z.array(
1273
+ z.object({
1274
+ type: z.literal('bash_code_execution_output'),
1275
+ file_id: z.string(),
1276
+ }),
1277
+ ),
1278
+ stdout: z.string(),
1279
+ stderr: z.string(),
1280
+ return_code: z.number(),
1281
+ }),
1282
+ z.object({
1283
+ type: z.literal('bash_code_execution_tool_result_error'),
1284
+ error_code: z.string(),
1285
+ }),
1286
+ ]),
1287
+ }),
1288
+ // text editor code execution results for code_execution_20250825 tool:
1289
+ z.object({
1290
+ type: z.literal('text_editor_code_execution_tool_result'),
1291
+ tool_use_id: z.string(),
1292
+ content: z.discriminatedUnion('type', [
1293
+ z.object({
1294
+ type: z.literal('text_editor_code_execution_tool_result_error'),
1295
+ error_code: z.string(),
1296
+ }),
1297
+ z.object({
1298
+ type: z.literal('text_editor_code_execution_view_result'),
1299
+ content: z.string(),
1300
+ file_type: z.string(),
1301
+ num_lines: z.number().nullable(),
1302
+ start_line: z.number().nullable(),
1303
+ total_lines: z.number().nullable(),
1304
+ }),
1305
+ z.object({
1306
+ type: z.literal('text_editor_code_execution_create_result'),
1307
+ is_file_update: z.boolean(),
1308
+ }),
1309
+ z.object({
1310
+ type: z.literal('text_editor_code_execution_str_replace_result'),
1311
+ lines: z.array(z.string()).nullable(),
1312
+ new_lines: z.number().nullable(),
1313
+ new_start: z.number().nullable(),
1314
+ old_lines: z.number().nullable(),
1315
+ old_start: z.number().nullable(),
1316
+ }),
1317
+ ]),
1318
+ }),
1319
+ // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
1320
+ z.object({
1321
+ type: z.literal('tool_search_tool_result'),
1322
+ tool_use_id: z.string(),
1323
+ content: z.union([
1324
+ z.object({
1325
+ type: z.literal('tool_search_tool_search_result'),
1326
+ tool_references: z.array(
1327
+ z.object({
1328
+ type: z.literal('tool_reference'),
1329
+ tool_name: z.string(),
1330
+ }),
1331
+ ),
1332
+ }),
1333
+ z.object({
1334
+ type: z.literal('tool_search_tool_result_error'),
1335
+ error_code: z.string(),
1336
+ }),
1337
+ ]),
1338
+ }),
1339
+ // advisor results for advisor_20260301:
1340
+ z.object({
1341
+ type: z.literal('advisor_tool_result'),
1342
+ tool_use_id: z.string(),
1343
+ content: z.discriminatedUnion('type', [
1344
+ z.object({
1345
+ type: z.literal('advisor_result'),
1346
+ text: z.string(),
1347
+ stop_reason: z.string().nullish(),
1348
+ }),
1349
+ z.object({
1350
+ type: z.literal('advisor_redacted_result'),
1351
+ encrypted_content: z.string(),
1352
+ stop_reason: z.string().nullish(),
1353
+ }),
1354
+ z.object({
1355
+ type: z.literal('advisor_tool_result_error'),
1356
+ error_code: z.string(),
1357
+ }),
1358
+ ]),
1359
+ }),
1360
+ ] as const;
1361
+ }
1362
+
1163
1363
  // limited version of the schema, focused on what is needed for the implementation
1164
1364
  // this approach limits breakages when the API changes and increases efficiency
1165
1365
  export const anthropicChunkSchema = lazySchema(() =>
@@ -1178,7 +1378,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1178
1378
  cache_creation_input_tokens: z.number().nullish(),
1179
1379
  cache_read_input_tokens: z.number().nullish(),
1180
1380
  }),
1181
- // Programmatic tool calling: content may be pre-populated for deferred tool calls
1381
+ // Resumed responses may contain deferred tool calls and server tool results.
1182
1382
  content: z
1183
1383
  .array(
1184
1384
  z.discriminatedUnion('type', [
@@ -1190,6 +1390,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1190
1390
  caller: anthropicToolCallCallerSchema().optional(),
1191
1391
  toolset_name: z.string().nullish(),
1192
1392
  }),
1393
+ ...anthropicToolResultSchemas(),
1193
1394
  ]),
1194
1395
  )
1195
1396
  .nullish(),
@@ -1251,203 +1452,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1251
1452
  input: z.unknown(),
1252
1453
  server_name: z.string(),
1253
1454
  }),
1254
- z.object({
1255
- type: z.literal('mcp_tool_result'),
1256
- tool_use_id: z.string(),
1257
- is_error: z.boolean(),
1258
- content: anthropicMcpToolResultContentSchema(),
1259
- }),
1260
- z.object({
1261
- type: z.literal('web_fetch_tool_result'),
1262
- tool_use_id: z.string(),
1263
- caller: anthropicToolCallCallerSchema().optional(),
1264
- content: z.union([
1265
- z.object({
1266
- type: z.literal('web_fetch_result'),
1267
- url: z.string(),
1268
- retrieved_at: z.string().nullable(),
1269
- content: z.object({
1270
- type: z.literal('document'),
1271
- title: z.string().nullable(),
1272
- citations: z.object({ enabled: z.boolean() }).optional(),
1273
- source: z.union([
1274
- z.object({
1275
- type: z.literal('base64'),
1276
- media_type: z.literal('application/pdf'),
1277
- data: z.string(),
1278
- }),
1279
- z.object({
1280
- type: z.literal('text'),
1281
- media_type: z.literal('text/plain'),
1282
- data: z.string(),
1283
- }),
1284
- ]),
1285
- }),
1286
- }),
1287
- z.object({
1288
- type: z.literal('web_fetch_tool_result_error'),
1289
- error_code: z.string(),
1290
- }),
1291
- ]),
1292
- }),
1293
- z.object({
1294
- type: z.literal('web_search_tool_result'),
1295
- tool_use_id: z.string(),
1296
- caller: anthropicToolCallCallerSchema().optional(),
1297
- content: z.union([
1298
- z.array(
1299
- z.object({
1300
- type: z.literal('web_search_result'),
1301
- url: z.string(),
1302
- title: z.string().nullable(),
1303
- encrypted_content: z.string(),
1304
- page_age: z.string().nullish(),
1305
- }),
1306
- ),
1307
- z.object({
1308
- type: z.literal('web_search_tool_result_error'),
1309
- error_code: z.string(),
1310
- }),
1311
- ]),
1312
- }),
1313
- // code execution results for code_execution_20250522 tool:
1314
- z.object({
1315
- type: z.literal('code_execution_tool_result'),
1316
- tool_use_id: z.string(),
1317
- content: z.union([
1318
- z.object({
1319
- type: z.literal('code_execution_result'),
1320
- stdout: z.string(),
1321
- stderr: z.string(),
1322
- return_code: z.number(),
1323
- content: z
1324
- .array(
1325
- z.object({
1326
- type: z.literal('code_execution_output'),
1327
- file_id: z.string(),
1328
- }),
1329
- )
1330
- .optional()
1331
- .default([]),
1332
- }),
1333
- z.object({
1334
- type: z.literal('encrypted_code_execution_result'),
1335
- encrypted_stdout: z.string(),
1336
- stderr: z.string(),
1337
- return_code: z.number(),
1338
- content: z
1339
- .array(
1340
- z.object({
1341
- type: z.literal('code_execution_output'),
1342
- file_id: z.string(),
1343
- }),
1344
- )
1345
- .optional()
1346
- .default([]),
1347
- }),
1348
- z.object({
1349
- type: z.literal('code_execution_tool_result_error'),
1350
- error_code: z.string(),
1351
- }),
1352
- ]),
1353
- }),
1354
- // bash code execution results for code_execution_20250825 tool:
1355
- z.object({
1356
- type: z.literal('bash_code_execution_tool_result'),
1357
- tool_use_id: z.string(),
1358
- content: z.discriminatedUnion('type', [
1359
- z.object({
1360
- type: z.literal('bash_code_execution_result'),
1361
- content: z.array(
1362
- z.object({
1363
- type: z.literal('bash_code_execution_output'),
1364
- file_id: z.string(),
1365
- }),
1366
- ),
1367
- stdout: z.string(),
1368
- stderr: z.string(),
1369
- return_code: z.number(),
1370
- }),
1371
- z.object({
1372
- type: z.literal('bash_code_execution_tool_result_error'),
1373
- error_code: z.string(),
1374
- }),
1375
- ]),
1376
- }),
1377
- // text editor code execution results for code_execution_20250825 tool:
1378
- z.object({
1379
- type: z.literal('text_editor_code_execution_tool_result'),
1380
- tool_use_id: z.string(),
1381
- content: z.discriminatedUnion('type', [
1382
- z.object({
1383
- type: z.literal('text_editor_code_execution_tool_result_error'),
1384
- error_code: z.string(),
1385
- }),
1386
- z.object({
1387
- type: z.literal('text_editor_code_execution_view_result'),
1388
- content: z.string(),
1389
- file_type: z.string(),
1390
- num_lines: z.number().nullable(),
1391
- start_line: z.number().nullable(),
1392
- total_lines: z.number().nullable(),
1393
- }),
1394
- z.object({
1395
- type: z.literal('text_editor_code_execution_create_result'),
1396
- is_file_update: z.boolean(),
1397
- }),
1398
- z.object({
1399
- type: z.literal(
1400
- 'text_editor_code_execution_str_replace_result',
1401
- ),
1402
- lines: z.array(z.string()).nullable(),
1403
- new_lines: z.number().nullable(),
1404
- new_start: z.number().nullable(),
1405
- old_lines: z.number().nullable(),
1406
- old_start: z.number().nullable(),
1407
- }),
1408
- ]),
1409
- }),
1410
- // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
1411
- z.object({
1412
- type: z.literal('tool_search_tool_result'),
1413
- tool_use_id: z.string(),
1414
- content: z.union([
1415
- z.object({
1416
- type: z.literal('tool_search_tool_search_result'),
1417
- tool_references: z.array(
1418
- z.object({
1419
- type: z.literal('tool_reference'),
1420
- tool_name: z.string(),
1421
- }),
1422
- ),
1423
- }),
1424
- z.object({
1425
- type: z.literal('tool_search_tool_result_error'),
1426
- error_code: z.string(),
1427
- }),
1428
- ]),
1429
- }),
1430
- // advisor results for advisor_20260301:
1431
- z.object({
1432
- type: z.literal('advisor_tool_result'),
1433
- tool_use_id: z.string(),
1434
- content: z.discriminatedUnion('type', [
1435
- z.object({
1436
- type: z.literal('advisor_result'),
1437
- text: z.string(),
1438
- stop_reason: z.string().nullish(),
1439
- }),
1440
- z.object({
1441
- type: z.literal('advisor_redacted_result'),
1442
- encrypted_content: z.string(),
1443
- stop_reason: z.string().nullish(),
1444
- }),
1445
- z.object({
1446
- type: z.literal('advisor_tool_result_error'),
1447
- error_code: z.string(),
1448
- }),
1449
- ]),
1450
- }),
1455
+ ...anthropicToolResultSchemas(),
1451
1456
  anthropicFallbackContentSchema(),
1452
1457
  ]),
1453
1458
  }),
@@ -1154,7 +1154,6 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1154
1154
  type: 'other',
1155
1155
  message: `Unknown context management strategy: ${strategy}`,
1156
1156
  });
1157
- return undefined;
1158
1157
  }
1159
1158
  })
1160
1159
  .filter(edit => edit !== undefined),
@@ -2391,12 +2390,16 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
2391
2390
  controller.enqueue({ type: 'stream-start', warnings });
2392
2391
  },
2393
2392
 
2394
- transform(chunk, controller) {
2393
+ transform: function transformChunk(
2394
+ chunk,
2395
+ controller,
2396
+ emitRawChunks = true,
2397
+ ) {
2395
2398
  if (hasInvalidMessageSequence) {
2396
2399
  return;
2397
2400
  }
2398
2401
 
2399
- if (options.includeRawChunks) {
2402
+ if (options.includeRawChunks && emitRawChunks) {
2400
2403
  controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
2401
2404
  }
2402
2405
 
@@ -3384,8 +3387,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
3384
3387
  },
3385
3388
  });
3386
3389
 
3387
- // Programmatic tool calling: process pre-populated content blocks
3388
- // (for deferred tool calls, content may be in message_start)
3390
+ // Resumed responses may include deferred tool calls and server tool results.
3389
3391
  if (value.message.content != null) {
3390
3392
  for (
3391
3393
  let contentIndex = 0;
@@ -3427,6 +3429,21 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
3427
3429
  },
3428
3430
  }),
3429
3431
  });
3432
+ } else {
3433
+ // Reuse the block handler without emitting a synthetic raw chunk.
3434
+ transformChunk(
3435
+ {
3436
+ success: true,
3437
+ value: {
3438
+ type: 'content_block_start',
3439
+ index: contentIndex,
3440
+ content_block: part,
3441
+ },
3442
+ rawValue: part,
3443
+ },
3444
+ controller,
3445
+ false,
3446
+ );
3430
3447
  }
3431
3448
  }
3432
3449
  }
@@ -689,6 +689,48 @@ export const anthropicModelCatalog: Readonly<
689
689
  .map(([modelId]) => [modelId, listedLanguageModelEntries[modelId]]),
690
690
  );
691
691
 
692
+ /**
693
+ * Returns Messages API catalog metadata, or `undefined` for unlisted IDs.
694
+ */
695
+ export function anthropicModelCapabilities(
696
+ modelId: AnthropicModelIds['language'] | (string & {}),
697
+ ) {
698
+ if (!Object.prototype.hasOwnProperty.call(anthropicModelCatalog, modelId)) {
699
+ return;
700
+ }
701
+
702
+ const entry = anthropicModelCatalog[modelId];
703
+ const { supportsXhighEffort: _supportsXhighEffort, ...reasoning } =
704
+ entry.capabilities.language.reasoning;
705
+ return {
706
+ provider: 'anthropic' as const,
707
+ modelId,
708
+ endpoints: {
709
+ messages: {
710
+ operation: 'language' as const,
711
+ capabilities: { ...entry.capabilities.language, reasoning },
712
+ providerOptions: Object.fromEntries(
713
+ Object.entries(anthropicProviderOptions.language).map(
714
+ ([name, definition]) => [
715
+ name,
716
+ {
717
+ ...definition,
718
+ support:
719
+ entry.providerOptions.language[
720
+ name as AnthropicLanguageProviderOption
721
+ ],
722
+ },
723
+ ],
724
+ ),
725
+ ) as Record<
726
+ AnthropicLanguageProviderOption,
727
+ ProviderOptionDefinition & { support: ModelCapability }
728
+ >,
729
+ },
730
+ },
731
+ };
732
+ }
733
+
692
734
  export function getAnthropicLanguageModelEntry(
693
735
  modelId: string,
694
736
  ): AnthropicLanguageModelEntry {
@@ -819,7 +819,7 @@ export async function convertToAnthropicPrompt({
819
819
  message: `unsupported tool content part type: ${contentPart.type} with media type: ${contentPart.mediaType}`,
820
820
  });
821
821
 
822
- return undefined;
822
+ return;
823
823
  }
824
824
 
825
825
  warnings.push({
@@ -827,7 +827,7 @@ export async function convertToAnthropicPrompt({
827
827
  message: `unsupported tool content part type: ${contentPart.type} with data type: ${contentPart.data.type}`,
828
828
  });
829
829
 
830
- return undefined;
830
+ return;
831
831
  }
832
832
  case 'custom': {
833
833
  const anthropicOptions = contentPart.providerOptions
@@ -844,15 +844,13 @@ export async function convertToAnthropicPrompt({
844
844
  type: 'other',
845
845
  message: `unsupported custom tool content part`,
846
846
  });
847
- return undefined;
847
+ return;
848
848
  }
849
849
  default: {
850
850
  warnings.push({
851
851
  type: 'other',
852
852
  message: `unsupported tool content part type: ${(contentPart as { type: string }).type}`,
853
853
  });
854
-
855
- return undefined;
856
854
  }
857
855
  }
858
856
  })
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export type {
14
14
  } from './anthropic-language-model-options';
15
15
  export type { AnthropicToolOptions } from './anthropic-prepare-tools';
16
16
  export { anthropic, createAnthropic } from './anthropic-provider';
17
+ export { anthropicModelCapabilities } from './anthropic-model-catalog';
17
18
  export type {
18
19
  AnthropicProvider,
19
20
  AnthropicProviderSettings,