Skip to content

Add Anthropic via Vertex AI provider #31

@vzegnameta

Description

@vzegnameta

Add Anthropic via Vertex AI provider

Summary

Add a new anthropic-vertex provider that calls Anthropic Claude models through Google Vertex AI Model Garden, authenticating via ADC (service account) instead of an Anthropic API key. This enables using Claude models on the same cloud deployment and service account as the Google Vertex AI provider.

Motivation

Anthropic Claude models (Opus 4.6, Sonnet 4.6) are available on Google Cloud through the Vertex AI Model Garden. Teams already running workloads on Google Cloud can access Claude without a separate Anthropic API key — the same service account and ADC flow used for Gemini works for Claude.

This means a single Cloud Run deployment can offer both Gemini and Claude models, authenticated by the same service account, with the model selectable at setup time or per request.

Proposed changes

1. New dependency

@ai-sdk/anthropic-vertex

The Vercel AI SDK provides a dedicated package for calling Anthropic models through Vertex AI. It uses ADC for authentication, just like @ai-sdk/google-vertex.

2. New provider: src/providers/anthropic-vertex.ts

A new ProviderConfig that mirrors the existing anthropic.ts provider but authenticates via Vertex AI ADC instead of an Anthropic API key:

  • createModel: Uses createVertex({ project, location }) from @ai-sdk/anthropic-vertex instead of createAnthropic({ apiKey }).
  • preparePdf: Same as the existing anthropic provider — reads PDF bytes inline. No File API, no caching (Claude doesn't support Gemini File API URIs).
  • apiKey parameter: Ignored — ADC handles authentication.
  • id: Set to "anthropic" so that provider-specific behavior (e.g., token limit error detection) is consistent.
import { createVertex } from "@ai-sdk/anthropic-vertex";
import type { ProviderConfig, PdfSource, PreparedPdf } from "./types.js";

const MODELS = [
  {
    id: "claude-sonnet-4-6",
    displayName: "Claude Sonnet 4.6",
    hint: "Fast and cost-effective",
  },
  {
    id: "claude-opus-4-6",
    displayName: "Claude Opus 4.6",
    hint: "Best and most expensive",
  },
];

const DEFAULT_MODEL = "claude-opus-4-6";

function getProject(): string {
  const project = process.env.VERTEX_PROJECT;
  if (!project) throw new Error("VERTEX_PROJECT env var is required.");
  return project;
}

function getLocation(): string {
  return process.env.VERTEX_LOCATION || "us-east5";
}

export const anthropicVertexProvider: ProviderConfig = {
  id: "anthropic",
  displayName: "Anthropic Claude (Vertex AI)",
  models: MODELS,
  defaultModel: DEFAULT_MODEL,
  apiKeyUrl: "",
  createModel: (_apiKey, modelId) => {
    const vertex = createVertex({
      project: getProject(),
      location: getLocation(),
    });
    return vertex(modelId);
  },
  providerOptions: {},
  preparePdf,    // same inline-bytes implementation as anthropic.ts
  isTokenLimitError, // same error detection as anthropic.ts
};

3. Wire it up

src/providers/registry.ts:

import { anthropicVertexProvider } from "./anthropic-vertex.js";

export const providers = {
  google: googleProvider,
  "google-vertex": vertexProvider,
  anthropic: anthropicProvider,
  "anthropic-vertex": anthropicVertexProvider,
  openai: openaiProvider,
};

src/providers/index.ts:

export { anthropicVertexProvider } from "./anthropic-vertex.js";

4. Setup flow

When the user selects "Anthropic Claude (Vertex AI)" during --setup:

  1. Skip the API key prompt — Vertex AI handles auth via ADC.
  2. Prompt for GCP Project ID (required).
  3. Prompt for Region/Location (required — must be a region where Claude models are enabled).
  4. Prompt for SA key file path (optional — only for local development).
  5. Prompt for model — choose between Sonnet 4.6 and Opus 4.6.

5. Required IAM roles

Role Purpose
Vertex AI User Call Claude models via Vertex AI Model Garden

Note: unlike the Google Vertex AI provider, Storage Admin is not needed — Claude uses inline PDF bytes, not the File API.

6. Model availability

Claude models must be enabled in the GCP project's Vertex AI Model Garden before they can be called. The setup flow or documentation should note this prerequisite.

Environment variables

Variable Required Description
VERTEX_PROJECT Yes (or set during --setup) GCP project ID
VERTEX_LOCATION Yes (or set during --setup) GCP region where Claude is enabled
GOOGLE_APPLICATION_CREDENTIALS Local only Path to SA JSON key file

Backward compatibility

  • No breaking changes. The existing anthropic provider (direct API key) is unchanged.
  • This is a new, additive provider choice.

Related

  • Depends on: Streamable HTTP transport (for cloud deployment)
  • Complements: Google Vertex AI provider (same ADC auth pattern, different models)
  • Together, google-vertex + anthropic-vertex allow a single deployment to serve both Gemini and Claude models via the same service account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions