@ai-sdk/anthropic 4.0.62 → 4.0.63

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.
@@ -64,18 +64,18 @@ You can use the following optional settings to customize the Anthropic provider
64
64
  ## Language Models
65
65
 
66
66
  You can create models that call the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post) using the provider instance.
67
- The first argument is the model id, e.g. `claude-3-haiku-20240307`.
67
+ The first argument is the model id, e.g. `claude-haiku-4-5`.
68
68
  Some models have multi-modal capabilities.
69
69
 
70
70
  ```ts
71
- const model = anthropic('claude-3-haiku-20240307');
71
+ const model = anthropic('claude-haiku-4-5');
72
72
  ```
73
73
 
74
74
  You can also use the following aliases for model creation:
75
75
 
76
- - `anthropic.languageModel('claude-3-haiku-20240307')` - Creates a language model
77
- - `anthropic.chat('claude-3-haiku-20240307')` - Alias for `languageModel`
78
- - `anthropic.messages('claude-3-haiku-20240307')` - Alias for `languageModel`
76
+ - `anthropic.languageModel('claude-haiku-4-5')` - Creates a language model
77
+ - `anthropic.chat('claude-haiku-4-5')` - Alias for `languageModel`
78
+ - `anthropic.messages('claude-haiku-4-5')` - Alias for `languageModel`
79
79
 
80
80
  You can use Anthropic language models to generate text with the `generateText` function:
81
81
 
@@ -84,7 +84,7 @@ import { anthropic } from '@ai-sdk/anthropic';
84
84
  import { generateText } from 'ai';
85
85
 
86
86
  const { text } = await generateText({
87
- model: anthropic('claude-3-haiku-20240307'),
87
+ model: anthropic('claude-haiku-4-5'),
88
88
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
89
89
  });
90
90
  ```
@@ -179,7 +179,7 @@ import { streamText, tool } from 'ai';
179
179
  import { z } from 'zod';
180
180
 
181
181
  const result = streamText({
182
- model: anthropic('claude-sonnet-4-20250514'),
182
+ model: anthropic('claude-sonnet-5'),
183
183
  tools: {
184
184
  writeFile: tool({
185
185
  description: 'Write content to a file',
@@ -223,7 +223,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
223
223
  import { generateText } from 'ai';
224
224
 
225
225
  const { text, usage } = await generateText({
226
- model: anthropic('claude-opus-4-20250514'),
226
+ model: anthropic('claude-opus-5-5'),
227
227
  prompt: 'How many people will live in the world in 2040?',
228
228
  providerOptions: {
229
229
  anthropic: {
@@ -268,7 +268,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
268
268
  import { generateText } from 'ai';
269
269
 
270
270
  const { text } = await generateText({
271
- model: anthropic('claude-opus-4-8'),
271
+ model: anthropic('claude-opus-5-5'),
272
272
  prompt: 'Research the pros and cons of Rust vs Go for building CLI tools.',
273
273
  providerOptions: {
274
274
  anthropic: {
@@ -306,7 +306,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
306
306
  import { generateText } from 'ai';
307
307
 
308
308
  const { text } = await generateText({
309
- model: anthropic('claude-opus-4-6'),
309
+ model: anthropic('claude-opus-5-5'),
310
310
  prompt: 'Summarize the key points of this document.',
311
311
  providerOptions: {
312
312
  anthropic: {
@@ -351,7 +351,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
351
351
  import { generateText } from 'ai';
352
352
 
353
353
  const { text } = await generateText({
354
- model: anthropic('claude-fable-5'),
354
+ model: anthropic('claude-fable-5-1'),
355
355
  prompt: 'Explain the history of cryptography.',
356
356
  providerOptions: {
357
357
  anthropic: {
@@ -368,11 +368,11 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
368
368
  import { generateText } from 'ai';
369
369
 
370
370
  const { text } = await generateText({
371
- model: anthropic('claude-fable-5'),
371
+ model: anthropic('claude-fable-5-1'),
372
372
  prompt: 'Explain the history of cryptography.',
373
373
  providerOptions: {
374
374
  anthropic: {
375
- fallbacks: [{ model: 'claude-fable-5' }],
375
+ fallbacks: [{ model: 'claude-fable-5-1' }],
376
376
  } satisfies AnthropicLanguageModelOptions,
377
377
  },
378
378
  });
@@ -405,7 +405,7 @@ import { generateText, tool } from 'ai';
405
405
  import { z } from 'zod';
406
406
 
407
407
  const result = await generateText({
408
- model: anthropic('claude-sonnet-4-5'),
408
+ model: anthropic('claude-sonnet-5'),
409
409
  prompt: 'Use the bash tool to run: echo hello',
410
410
  tools: { bash: tool({ inputSchema: z.object({ command: z.string() }) }) },
411
411
  providerOptions: {
@@ -715,7 +715,89 @@ const result = await generateText({
715
715
  });
716
716
  ```
717
717
 
718
- #### Compaction
718
+ #### On-Demand Compaction
719
+
720
+ Use the `compaction` provider option to ask Anthropic to summarize the supplied
721
+ conversation immediately. The provider automatically adds the
722
+ `compact-2026-09-04` beta header.
723
+
724
+ ```ts highlight="10-15,21-22"
725
+ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
726
+ import { generateText, ModelMessage } from 'ai';
727
+
728
+ const history: ModelMessage[] = [
729
+ { role: 'user', content: 'Help me design a recipe application.' },
730
+ { role: 'assistant', content: 'Start with Recipe and Ingredient.' },
731
+ { role: 'user', content: 'Recipe also needs a title and servings.' },
732
+ ];
733
+
734
+ const summary = await generateText({
735
+ model: anthropic('claude-opus-5'),
736
+ messages: history,
737
+ maxOutputTokens: 4096,
738
+ providerOptions: {
739
+ anthropic: {
740
+ compaction: {
741
+ type: 'summarize',
742
+ instructions: 'Preserve all agreed entities and fields.',
743
+ },
744
+ } satisfies AnthropicLanguageModelOptions,
745
+ },
746
+ });
747
+
748
+ if (summary.rawFinishReason === 'compaction') {
749
+ const [compactionMessage] = summary.finalStep.response.messages;
750
+
751
+ const continuation = await generateText({
752
+ model: anthropic('claude-opus-5'),
753
+ messages: [
754
+ compactionMessage,
755
+ { role: 'user', content: 'Which fields should be required?' },
756
+ ],
757
+ });
758
+
759
+ console.log(continuation.text);
760
+ }
761
+ ```
762
+
763
+ Anthropic returns the summary as a text part whose
764
+ `providerMetadata.anthropic.type` is `compaction`. Its opaque signature is
765
+ available as `providerMetadata.anthropic.signature`. Response messages copy
766
+ that metadata to `providerOptions`, so passing
767
+ `summary.finalStep.response.messages` back to the AI SDK preserves the signed
768
+ block and automatically adds the beta header on the continuation request.
769
+
770
+ Check `rawFinishReason` before reading the response block. Anthropic can return
771
+ a successful HTTP response without a summary when summarization ends for
772
+ another reason, such as `max_tokens`, `model_context_window_exceeded`,
773
+ `tool_use`, `refusal`, or `end_turn`. In that case, keep the existing history
774
+ and retry later; the unsuccessful summarization is still included in
775
+ `providerMetadata.anthropic.iterations`.
776
+
777
+ Send exactly one signed compaction block on continuation requests. It must be
778
+ the first content block in `messages` and must be replayed without changing its
779
+ text or signature. Remove the messages it summarizes before continuing. Keep
780
+ the same system prompt and tool definitions on the compaction request that you
781
+ use for the conversation, especially when preserving later thinking blocks.
782
+
783
+ Before requesting compaction, resolve any unfinished tool call from the last
784
+ assistant turn. Do not combine `compaction` with `contextManagement`,
785
+ `stopSequences`, structured output, a required or named `toolChoice`, or a
786
+ `taskBudget.remaining` value. Also omit `taskBudget.remaining` from later
787
+ requests that carry the signed block.
788
+
789
+ On-demand compaction usage is reported in
790
+ `providerMetadata.anthropic.iterations`. Sum the iteration usage when
791
+ calculating the tokens consumed by the summarization request.
792
+
793
+ On-demand compaction can also be initiated in a Message Batch. Configure the
794
+ `compaction` provider option on the individual batch request; the provider
795
+ automatically adds `compact-2026-09-04` to the batch beta header.
796
+
797
+ For more details, see
798
+ [Anthropic's on-demand compaction documentation](https://platform.claude.com/docs/en/build-with-claude/compaction-on-demand).
799
+
800
+ #### Threshold Compaction
719
801
 
720
802
  The `compact_20260112` edit type automatically summarizes earlier conversation context when token limits are reached. This is useful for long-running conversations where you want to preserve the essence of earlier exchanges while staying within token limits.
721
803
 
@@ -724,7 +806,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
724
806
  import { streamText } from 'ai';
725
807
 
726
808
  const result = streamText({
727
- model: anthropic('claude-opus-4-6'),
809
+ model: anthropic('claude-opus-5-5'),
728
810
  messages: conversationHistory,
729
811
  providerOptions: {
730
812
  anthropic: {
@@ -848,7 +930,7 @@ import { generateText } from 'ai';
848
930
  const errorMessage = '... long error message ...';
849
931
 
850
932
  const result = await generateText({
851
- model: anthropic('claude-sonnet-4-5'),
933
+ model: anthropic('claude-sonnet-5'),
852
934
  messages: [
853
935
  {
854
936
  role: 'user',
@@ -882,7 +964,7 @@ You can also use cache control on system messages by providing multiple system m
882
964
 
883
965
  ```ts highlight="3,7-9"
884
966
  const result = await generateText({
885
- model: anthropic('claude-sonnet-4-5'),
967
+ model: anthropic('claude-sonnet-5'),
886
968
  messages: [
887
969
  {
888
970
  role: 'system',
@@ -1219,7 +1301,7 @@ const webSearchTool = anthropic.tools.webSearch_20260318({
1219
1301
  });
1220
1302
 
1221
1303
  const result = await generateText({
1222
- model: anthropic('claude-opus-5'),
1304
+ model: anthropic('claude-opus-5-5'),
1223
1305
  prompt: 'What are the latest developments in AI?',
1224
1306
  tools: {
1225
1307
  web_search: webSearchTool,
@@ -1278,7 +1360,7 @@ const webSearchTool = anthropic.tools.webSearch_20260318({
1278
1360
  });
1279
1361
 
1280
1362
  const result = await generateText({
1281
- model: anthropic('claude-opus-5'),
1363
+ model: anthropic('claude-opus-5-5'),
1282
1364
  prompt: 'Find local news about technology',
1283
1365
  tools: {
1284
1366
  web_search: webSearchTool,
@@ -1304,7 +1386,7 @@ import { anthropic } from '@ai-sdk/anthropic';
1304
1386
  import { generateText } from 'ai';
1305
1387
 
1306
1388
  const result = await generateText({
1307
- model: anthropic('claude-opus-5'),
1389
+ model: anthropic('claude-opus-5-5'),
1308
1390
  prompt:
1309
1391
  'What is this page about? https://en.wikipedia.org/wiki/Maglemosian_culture',
1310
1392
  tools: {
@@ -1396,7 +1478,7 @@ import { generateText, tool } from 'ai';
1396
1478
  import { z } from 'zod';
1397
1479
 
1398
1480
  const result = await generateText({
1399
- model: anthropic('claude-sonnet-4-5'),
1481
+ model: anthropic('claude-sonnet-5'),
1400
1482
  prompt: 'What is the weather in San Francisco?',
1401
1483
  tools: {
1402
1484
  toolSearch: anthropic.tools.toolSearchBm25_20251119(),
@@ -1426,7 +1508,7 @@ For more precise tool matching, you can use the regex variant:
1426
1508
 
1427
1509
  ```ts
1428
1510
  const result = await generateText({
1429
- model: anthropic('claude-sonnet-4-5'),
1511
+ model: anthropic('claude-sonnet-5'),
1430
1512
  prompt: 'Get the weather data',
1431
1513
  tools: {
1432
1514
  toolSearch: anthropic.tools.toolSearchRegex_20251119(),
@@ -1447,7 +1529,7 @@ import { generateText, tool } from 'ai';
1447
1529
  import { z } from 'zod';
1448
1530
 
1449
1531
  const result = await generateText({
1450
- model: anthropic('claude-sonnet-4-5'),
1532
+ model: anthropic('claude-sonnet-5'),
1451
1533
  prompt: 'What is the weather in San Francisco?',
1452
1534
  tools: {
1453
1535
  // Custom search tool
@@ -1607,7 +1689,7 @@ import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
1607
1689
  import { generateText } from 'ai';
1608
1690
 
1609
1691
  const result = await generateText({
1610
- model: anthropic('claude-sonnet-4-5'),
1692
+ model: anthropic('claude-sonnet-5'),
1611
1693
  prompt: `Call the echo tool with "hello world". what does it respond with back?`,
1612
1694
  providerOptions: {
1613
1695
  anthropic: {
@@ -1679,7 +1761,7 @@ Web search errors throw exceptions that you can catch:
1679
1761
  ```ts
1680
1762
  try {
1681
1763
  const result = await generateText({
1682
- model: anthropic('claude-opus-4-20250514'),
1764
+ model: anthropic('claude-opus-5-5'),
1683
1765
  prompt: 'Search for something',
1684
1766
  tools: {
1685
1767
  web_search: webSearchTool,
@@ -1698,7 +1780,7 @@ Web search errors are delivered as error parts in the stream:
1698
1780
 
1699
1781
  ```ts
1700
1782
  const result = await streamText({
1701
- model: anthropic('claude-opus-4-20250514'),
1783
+ model: anthropic('claude-opus-5-5'),
1702
1784
  prompt: 'Search for something',
1703
1785
  tools: {
1704
1786
  web_search: webSearchTool,
@@ -1793,7 +1875,7 @@ Code execution errors are delivered as tool result parts in the response:
1793
1875
 
1794
1876
  ```ts
1795
1877
  const result = await generateText({
1796
- model: anthropic('claude-opus-4-20250514'),
1878
+ model: anthropic('claude-opus-5-5'),
1797
1879
  prompt: 'Execute some Python script',
1798
1880
  tools: {
1799
1881
  code_execution: codeExecutionTool,
@@ -1818,7 +1900,7 @@ Code execution errors are delivered as error parts in the stream:
1818
1900
 
1819
1901
  ```ts
1820
1902
  const result = await streamText({
1821
- model: anthropic('claude-opus-4-20250514'),
1903
+ model: anthropic('claude-opus-5-5'),
1822
1904
  prompt: 'Execute some Python script',
1823
1905
  tools: {
1824
1906
  code_execution: codeExecutionTool,
@@ -1978,7 +2060,7 @@ Option 1: URL-based PDF document
1978
2060
 
1979
2061
  ```ts
1980
2062
  const result = await generateText({
1981
- model: anthropic('claude-sonnet-4-5'),
2063
+ model: anthropic('claude-sonnet-5'),
1982
2064
  messages: [
1983
2065
  {
1984
2066
  role: 'user',
@@ -2004,7 +2086,7 @@ Option 2: Base64-encoded PDF document
2004
2086
 
2005
2087
  ```ts
2006
2088
  const result = await generateText({
2007
- model: anthropic('claude-sonnet-4-5'),
2089
+ model: anthropic('claude-sonnet-5'),
2008
2090
  messages: [
2009
2091
  {
2010
2092
  role: 'user',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.62",
3
+ "version": "4.0.63",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -78,6 +78,7 @@ export interface AnthropicAssistantMessage {
78
78
  export interface AnthropicCompactionContent {
79
79
  type: 'compaction';
80
80
  content: string;
81
+ signature?: string;
81
82
  cache_control?: AnthropicCacheControl;
82
83
  }
83
84
 
@@ -805,6 +806,7 @@ export const anthropicResponseSchema = lazySchema(() =>
805
806
  z.object({
806
807
  type: z.literal('compaction'),
807
808
  content: z.string().nullish(),
809
+ signature: z.string().nullish(),
808
810
  }),
809
811
  z.object({
810
812
  type: z.literal('tool_use'),
@@ -1187,6 +1189,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1187
1189
  z.object({
1188
1190
  type: z.literal('compaction'),
1189
1191
  content: z.string().nullish(),
1192
+ signature: z.string().nullish(),
1190
1193
  }),
1191
1194
  z.object({
1192
1195
  type: z.literal('server_tool_use'),
@@ -806,7 +806,12 @@ function convertAnthropicBatchResponse(
806
806
  content.push({
807
807
  type: 'text',
808
808
  text: part.content,
809
- providerMetadata: { anthropic: { type: 'compaction' } },
809
+ providerMetadata: {
810
+ anthropic: {
811
+ type: 'compaction',
812
+ ...(part.signature != null && { signature: part.signature }),
813
+ },
814
+ },
810
815
  });
811
816
  break;
812
817
  case 'tool_use':
@@ -384,6 +384,19 @@ export const anthropicLanguageModelOptions = z.object({
384
384
  )
385
385
  .optional(),
386
386
 
387
+ /**
388
+ * Requests an on-demand summary of the supplied conversation.
389
+ *
390
+ * The required `compact-2026-09-04` beta is added automatically.
391
+ * Cannot be combined with `contextManagement`.
392
+ */
393
+ compaction: z
394
+ .object({
395
+ type: z.literal('summarize'),
396
+ instructions: z.string().optional(),
397
+ })
398
+ .optional(),
399
+
387
400
  contextManagement: z
388
401
  .object({
389
402
  edits: z.array(
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  APICallError,
3
+ InvalidArgumentError,
3
4
  InvalidResponseDataError,
4
5
  type JSONObject,
5
6
  type LanguageModelV4,
@@ -508,6 +509,15 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
508
509
  }
509
510
 
510
511
  const contextManagement = anthropicOptions?.contextManagement;
512
+ const compaction = anthropicOptions?.compaction;
513
+
514
+ if (contextManagement != null && compaction != null) {
515
+ throw new InvalidArgumentError({
516
+ argument: 'providerOptions',
517
+ message:
518
+ 'Anthropic provider options `compaction` and `contextManagement` cannot be used together.',
519
+ });
520
+ }
511
521
 
512
522
  // Create a shared cache control validator to track breakpoints across tools and messages
513
523
  const cacheControlValidator = new CacheControlValidator();
@@ -779,6 +789,15 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
779
789
  })),
780
790
  }),
781
791
 
792
+ ...(compaction && {
793
+ compaction: {
794
+ type: compaction.type,
795
+ ...(compaction.instructions !== undefined && {
796
+ instructions: compaction.instructions,
797
+ }),
798
+ },
799
+ }),
800
+
782
801
  ...(contextManagement && {
783
802
  context_management: {
784
803
  edits: contextManagement.edits
@@ -925,6 +944,10 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
925
944
  betas.add('dangerous-tool-use-2026-09-03');
926
945
  }
927
946
 
947
+ if (compaction) {
948
+ betas.add('compact-2026-09-04');
949
+ }
950
+
928
951
  if (contextManagement) {
929
952
  betas.add('context-management-2025-06-27');
930
953
 
@@ -1271,6 +1294,9 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1271
1294
  providerMetadata: {
1272
1295
  anthropic: {
1273
1296
  type: 'compaction',
1297
+ ...(part.signature != null && {
1298
+ signature: part.signature,
1299
+ }),
1274
1300
  },
1275
1301
  },
1276
1302
  });
@@ -1971,9 +1997,27 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1971
1997
  providerMetadata: {
1972
1998
  anthropic: {
1973
1999
  type: 'compaction',
2000
+ ...(part.signature != null && {
2001
+ signature: part.signature,
2002
+ }),
1974
2003
  },
1975
2004
  },
1976
2005
  });
2006
+
2007
+ // On-demand compaction blocks arrive complete in
2008
+ // content_block_start, without compaction_delta events.
2009
+ if (
2010
+ part.signature != null &&
2011
+ part.content != null &&
2012
+ part.content !== ''
2013
+ ) {
2014
+ controller.enqueue({
2015
+ type: 'text-delta',
2016
+ id: String(value.index),
2017
+ delta: part.content,
2018
+ });
2019
+ }
2020
+
1977
2021
  return;
1978
2022
  }
1979
2023
 
@@ -711,7 +711,11 @@ export async function convertToAnthropicPrompt({
711
711
  case 'text': {
712
712
  // Check if this is a compaction block (via providerMetadata)
713
713
  const textMetadata = part.providerOptions?.anthropic as
714
- | { type?: string; citations?: Citation[] }
714
+ | {
715
+ type?: string;
716
+ citations?: Citation[];
717
+ signature?: string;
718
+ }
715
719
  | undefined;
716
720
 
717
721
  if (textMetadata?.type === 'compaction') {
@@ -719,9 +723,16 @@ export async function convertToAnthropicPrompt({
719
723
  break;
720
724
  }
721
725
 
726
+ if (typeof textMetadata.signature === 'string') {
727
+ betas.add('compact-2026-09-04');
728
+ }
729
+
722
730
  anthropicContent.push({
723
731
  type: 'compaction',
724
732
  content: part.text,
733
+ ...(typeof textMetadata.signature === 'string' && {
734
+ signature: textMetadata.signature,
735
+ }),
725
736
  cache_control: cacheControl,
726
737
  });
727
738
  } else {