@ai-sdk/anthropic 4.0.0-beta.32 → 4.0.0-beta.33

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.
@@ -584,13 +584,12 @@ For more details, see [Anthropic's Context Management documentation](https://doc
584
584
  In the messages and message parts, you can use the `providerOptions` property to set cache control breakpoints.
585
585
  You need to set the `anthropic` property in the `providerOptions` object to `{ cacheControl: { type: 'ephemeral' } }` to set a cache control breakpoint.
586
586
 
587
- The cache creation input tokens are then returned in the `providerMetadata` object
588
- for `generateText`, again under the `anthropic` property.
589
- When you use `streamText`, the response contains a promise
590
- that resolves to the metadata. Alternatively you can receive it in the
591
- `onFinish` callback.
587
+ Cache read and cache write (creation) token counts are returned on the standard
588
+ `usage` object for both `generateText` and `streamText`. You can access them at
589
+ `result.usage.inputTokenDetails.cacheReadTokens` and
590
+ `result.usage.inputTokenDetails.cacheWriteTokens`.
592
591
 
593
- ```ts highlight="8,18-20,29-30"
592
+ ```ts highlight="8,18-20,29-32"
594
593
  import { anthropic } from '@ai-sdk/anthropic';
595
594
  import { generateText } from 'ai';
596
595
 
@@ -617,8 +616,11 @@ const result = await generateText({
617
616
  });
618
617
 
619
618
  console.log(result.text);
620
- console.log(result.providerMetadata?.anthropic);
621
- // e.g. { cacheCreationInputTokens: 2118 }
619
+ console.log('Cache read tokens:', result.usage.inputTokenDetails.cacheReadTokens);
620
+ console.log(
621
+ 'Cache write tokens:',
622
+ result.usage.inputTokenDetails.cacheWriteTokens,
623
+ );
622
624
  ```
623
625
 
624
626
  You can also use cache control on system messages by providing multiple system messages at the head of your messages array:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.0-beta.32",
3
+ "version": "4.0.0-beta.33",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -21,9 +21,6 @@ export interface AnthropicUsageIteration {
21
21
 
22
22
  export interface AnthropicMessageMetadata {
23
23
  usage: JSONObject;
24
- // TODO remove cacheCreationInputTokens in AI SDK 6
25
- // (use value in usage object instead)
26
- cacheCreationInputTokens: number | null;
27
24
  stopSequence: string | null;
28
25
 
29
26
  /**
@@ -1303,8 +1303,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
1303
1303
  providerMetadata: (() => {
1304
1304
  const anthropicMetadata = {
1305
1305
  usage: response.usage as JSONObject,
1306
- cacheCreationInputTokens:
1307
- response.usage.cache_creation_input_tokens ?? null,
1308
1306
  stopSequence: response.stop_sequence ?? null,
1309
1307
 
1310
1308
  iterations: response.usage.iterations
@@ -1425,7 +1423,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
1425
1423
  | AnthropicMessageMetadata['contextManagement']
1426
1424
  | null = null;
1427
1425
  let rawUsage: JSONObject | undefined = undefined;
1428
- let cacheCreationInputTokens: number | null = null;
1429
1426
  let stopSequence: string | null = null;
1430
1427
  let container: AnthropicMessageMetadata['container'] | null = null;
1431
1428
  let isJsonResponseFromTool = false;
@@ -2145,9 +2142,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
2145
2142
  ...(value.message.usage as JSONObject),
2146
2143
  };
2147
2144
 
2148
- cacheCreationInputTokens =
2149
- value.message.usage.cache_creation_input_tokens ?? null;
2150
-
2151
2145
  if (value.message.container != null) {
2152
2146
  container = {
2153
2147
  expiresAt: value.message.container.expires_at,
@@ -2245,8 +2239,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
2245
2239
  if (value.usage.cache_creation_input_tokens != null) {
2246
2240
  usage.cache_creation_input_tokens =
2247
2241
  value.usage.cache_creation_input_tokens;
2248
- cacheCreationInputTokens =
2249
- value.usage.cache_creation_input_tokens;
2250
2242
  }
2251
2243
  if (value.usage.iterations != null) {
2252
2244
  usage.iterations = value.usage.iterations;
@@ -2292,7 +2284,6 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
2292
2284
  case 'message_stop': {
2293
2285
  const anthropicMetadata = {
2294
2286
  usage: (rawUsage as JSONObject) ?? null,
2295
- cacheCreationInputTokens,
2296
2287
  stopSequence,
2297
2288
  iterations: usage.iterations
2298
2289
  ? usage.iterations.map(iter => ({