|
| 1 | +# Vertex AI Anthropic Setup Guide |
| 2 | + |
| 3 | +This guide shows how to set up Google Vertex AI with Anthropic's Claude models for your Node.js application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Google Cloud Platform account with billing enabled |
| 8 | +- A GCP project |
| 9 | +- Node.js application with the AI SDK |
| 10 | + |
| 11 | +## Step 1: Install Dependencies |
| 12 | + |
| 13 | +Add the required packages to your project: |
| 14 | + |
| 15 | +```bash |
| 16 | +pnpm add @ai-sdk/google-vertex google-auth-library |
| 17 | +``` |
| 18 | + |
| 19 | +Your `package.json` should include these versions (or newer): |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "dependencies": { |
| 24 | + "@ai-sdk/google-vertex": "^2.2.27", |
| 25 | + "google-auth-library": "^10.2.1" |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Step 2: Configure Google Cloud |
| 31 | + |
| 32 | +### Install and configure gcloud CLI: |
| 33 | + |
| 34 | +```bash |
| 35 | +# Install gcloud CLI (if not already installed) |
| 36 | +# Follow instructions at: https://cloud.google.com/sdk/docs/install |
| 37 | + |
| 38 | +# Set your project |
| 39 | +gcloud config set project YOUR-PROJECT-ID |
| 40 | + |
| 41 | +# Enable Vertex AI API |
| 42 | +gcloud services enable aiplatform.googleapis.com |
| 43 | + |
| 44 | +# Authenticate |
| 45 | +gcloud auth login |
| 46 | +gcloud auth application-default login |
| 47 | +``` |
| 48 | + |
| 49 | +## Step 3: Enable Anthropic Models |
| 50 | + |
| 51 | +1. Go to the [Vertex AI Model Garden](https://console.cloud.google.com/vertex-ai/model-garden) |
| 52 | +2. Navigate to Anthropic models or use this direct link for Claude Sonnet 4: https://console.cloud.google.com/vertex-ai/publishers/anthropic/model-garden/claude-sonnet-4 |
| 53 | +3. Click "Enable" for the models you want to use |
| 54 | +4. Complete the access request form |
| 55 | +5. Wait 1-2 business days for approval |
| 56 | + |
| 57 | +## Step 4: Set Environment Variables |
| 58 | + |
| 59 | +Add these variables to your `.env` file: |
| 60 | + |
| 61 | +```bash |
| 62 | +# Google Vertex AI Configuration |
| 63 | +GOOGLE_VERTEX_LOCATION=us-east5 |
| 64 | +GOOGLE_VERTEX_PROJECT=your-project-id |
| 65 | +GOOGLE_APPLICATION_CREDENTIALS=${PWD}/vertex-ai-credentials.json |
| 66 | +``` |
| 67 | + |
| 68 | +Note: Use `us-east5` as the location since it has the best model availability. |
| 69 | + |
| 70 | +## Step 5: Create Service Account Credentials |
| 71 | + |
| 72 | +### Option A: Using Google Cloud Console |
| 73 | + |
| 74 | +1. Go to [Google Cloud Console IAM](https://console.cloud.google.com/iam-admin/serviceaccounts) |
| 75 | +2. Create a new service account or use an existing one |
| 76 | +3. Add the `Vertex AI User` role |
| 77 | +4. Generate a JSON key file |
| 78 | +5. Save it as `vertex-ai-credentials.json` in your project root |
| 79 | + |
| 80 | +### Option B: Using gcloud CLI |
| 81 | + |
| 82 | +Create a service account and generate credentials using the gcloud CLI: |
| 83 | + |
| 84 | +```bash |
| 85 | +# Create a service account |
| 86 | +gcloud iam service-accounts create vertex-ai \ |
| 87 | + --display-name="Vertex AI Service Account" |
| 88 | + |
| 89 | +# Add the Vertex AI User role |
| 90 | +gcloud projects add-iam-policy-binding YOUR-PROJECT-ID \ |
| 91 | + --member= "serviceAccount:[email protected]" \ |
| 92 | + --role="roles/aiplatform.user" |
| 93 | + |
| 94 | +# Generate and download the credentials JSON file |
| 95 | +gcloud iam service-accounts keys create vertex-ai-credentials.json \ |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +Replace `YOUR-PROJECT-ID` with your actual Google Cloud project ID. |
| 100 | + |
| 101 | +## Step 6: Use in Your Code |
| 102 | + |
| 103 | +Import and use the Vertex AI Anthropic provider: |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic'; |
| 107 | + |
| 108 | +// Use in your agent or application |
| 109 | +const model = vertexAnthropic('claude-sonnet-4@20250514'); |
| 110 | +``` |
| 111 | + |
| 112 | +## Step 7: Test the Setup |
| 113 | + |
| 114 | +Test your configuration with a curl request: |
| 115 | + |
| 116 | +```bash |
| 117 | +curl -X POST \ |
| 118 | + -H "Authorization: Bearer $(gcloud auth print-access-token)" \ |
| 119 | + -H "Content-Type: application/json; charset=utf-8" \ |
| 120 | + -d '{ |
| 121 | + "anthropic_version": "vertex-2023-10-16", |
| 122 | + "messages": [ |
| 123 | + { |
| 124 | + "role": "user", |
| 125 | + "content": "Hello" |
| 126 | + } |
| 127 | + ], |
| 128 | + "max_tokens": 100 |
| 129 | + }' \ |
| 130 | + "https://aiplatform.googleapis.com/v1/projects/YOUR-PROJECT-ID/locations/global/publishers/anthropic/models/claude-sonnet-4@20250514:streamRawPredict" |
| 131 | +``` |
| 132 | + |
| 133 | +Replace `YOUR-PROJECT-ID` with your actual Google Cloud project ID. |
| 134 | + |
| 135 | +## Troubleshooting |
| 136 | + |
| 137 | +**Model not found errors**: Verify you have access to the model in the specified region and that it's enabled in Model Garden. |
| 138 | + |
| 139 | +**Authentication errors**: Check that your service account has the correct permissions and that the credentials file path is correct. |
| 140 | + |
| 141 | +**Quota errors**: Request quota increases in the [Cloud Console](https://console.cloud.google.com/iam-admin/quotas). |
| 142 | + |
| 143 | +## Helpful Resources |
| 144 | + |
| 145 | +- [Vertex AI Documentation](https://cloud.google.com/vertex-ai/docs) |
| 146 | +- [Anthropic Claude Code Vertex AI Guide](https://docs.anthropic.com/en/docs/claude-code/google-vertex-ai) |
| 147 | +- [AI SDK Google Vertex Documentation](https://sdk.vercel.ai/providers/ai-sdk-providers/google-vertex) |
0 commit comments