-
Notifications
You must be signed in to change notification settings - Fork 5
Description
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 withparts, be aware that there have been issues whereproviderOptionsare not correctly applied. It's advisable to use plaincontentstrings 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!