Use Anthropic Prompt Caching with OpenRouter
mainTo use Anthropic-specific prompt caching, include providerOptions.openrouter.cacheControl within the message content objects. The provider automatically converts these to the correct format for OpenRouter.
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
import { streamText } from 'ai';
const openrouter = createOpenRouter({ apiKey: 'your-api-key' });
const model = openrouter('anthropic/<supported-caching-model>');
await streamText({
model,
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{
role: 'user',
content: [
{ type: 'text', text: 'Given the text body below:' },
{
type: 'text',
text: `<LARGE BODY OF TEXT>`,
providerOptions: {
openrouter: {
cacheControl: { type: 'ephemeral' },
},
},
},
{ type: 'text', text: 'List the speakers?' },
],
},
],
});