Skip to content

[JS] Add support to anthropic prompt caching #2885

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
amondnet opened this issue May 8, 2025 · 0 comments · May be fixed by #2886
Open

[JS] Add support to anthropic prompt caching #2885

amondnet opened this issue May 8, 2025 · 0 comments · May be fixed by #2886

Comments

@amondnet
Copy link

amondnet commented May 8, 2025

Describe the solution you'd like

Anthropic cache control is in a Pre-Generally Available (GA) state on Google Vertex. For more see Google Vertex Anthropic prompt caching documentation.

const llmResponse = await ai.generate({
  model: claude3Sonnet, // or another Anthropic model
  messages: [
    {
      role: 'system',
      content: [
        {
          text: 'This is an important instruction that can be cached.',
          custom: {
            cacheControl: {
              type: 'ephemeral',
            },
          },
        },
      ],
    },
    {
      role: 'user',
      content: [{ text: 'What should I do when I visit Melbourne?' }],
    },
  ],
});

Additional context
The Anthropic Claude models offer prompt caching to reduce latency and costs
when reusing the same content in multiple requests. When you send a query, you
can cache all or specific parts of your input so that subsequent queries can use
the cached results from the previous request. This avoids additional compute and
network costs. Caches are unique to your Google Cloud project and cannot be used
by other projects.

For details about how to structure your prompts, see the Anthropic Prompt
caching

documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

1 participant