Skip to content

Enable Token Caching with Vercel AI and Anthropic #263

@planger

Description

@planger

Vercel doesn't handle caching in a generic way, so we would need to set caching explicitly if we access an Anthropic model with Vercel.

Additional Info

To control caching with Vercel AI SDK when using Anthropic models, you can utilize the providerOptions to set cache control for specific message parts. Here's how you can do it:

Setting Cache Control

You can specify cache control for individual message parts by including the providerOptions with the cacheControl setting. For example:

import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const result = await generateText({
  model: anthropic('claude-3-5-sonnet-20240620'),
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'You are a JavaScript expert.',
        },
        {
          type: 'text',
          text: 'Explain the error message.',
          providerOptions: {
            anthropic: {
              cacheControl: { type: 'ephemeral' },
            },
          },
        },
      ],
    },
  ],
});

This setup marks the specific message part for ephemeral caching.

Important Considerations

  • Minimum Token Threshold: Ensure that the content marked for caching meets the minimum token requirement. For instance, Claude 3.5 Haiku requires at least 2048 tokens for caching to be effective.

  • Structured Content (parts): If you're using structured content with parts, be aware that there have been issues where providerOptions are not correctly applied. It's advisable to use plain content strings in such cases.

  • System Messages: You can also apply cache control to system messages by including them at the beginning of your messages array and specifying the providerOptions.

If you need further assistance or examples, feel free to ask!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions