Skip to content

feat(anthropic): add optional ttl cache control propery, export conversion function (AI SDK v5) #7526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: v5
Choose a base branch
from

Conversation

arielweinberger
Copy link
Contributor

@arielweinberger arielweinberger commented Jul 24, 2025

Background

  • Conversion from Vercel AI SDK Model Messages to Anthropic Messages is not expose. As described in this issue, having it exported from the @ai-sdk/anthropic package would make it easy to leverage Anthropic's own token counting functionalityh.
  • The SDK is not aware of the optional ttl property of Anthropic's cache_control (see Anthropic docs).

Summary

  1. Exposed the conversion function from Vercel AI SDK's model messages to Anthropic messages.
  2. Added an optional ttl property to the Anthropic cache control type

Verification

Tested the output of this function with Anthropic's token counter for a very large conversation history (50+ messages) with a total of 120,000 tokens and it works flawlessly.

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

const anthropic = new Anthropic();

const anthropicPrompt = await convertToAnthropicMessagesPrompt({
    prompt: messages as unknown as LanguageModelV2Prompt,
    sendReasoning: false,
    warnings: []
});

const response = await anthropic.messages.countTokens({
    model: 'claude-4-sonnet-20250514',
    system: anthropicPrompt.prompt.system,
    messages: anthropicPrompt.prompt.messages as any
});

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

Fixes #7522

@arielweinberger arielweinberger changed the title feat(anthropic): add optional ttl cache control propery, export conversion function Jul 24, 2025
@lgrammel
Copy link
Collaborator

please split into 2 prs so we can revert individually if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants