用於存取 Claude 的 Vertex API 幾乎與 Messages API 相同,並支援所有相同的選項,但有兩個主要差異:
model 不會在請求本體中傳遞。相反,它在 Google Cloud 端點 URL 中指定。anthropic_version 在請求本體中傳遞(而不是作為標頭),並且必須設定為值 vertex-2023-10-16。Vertex 也受到 Anthropic 官方 client SDK 的支援。本指南將引導您完成在 Python 或 TypeScript 中向 Vertex AI 上的 Claude 發出請求的過程。
請注意,本指南假設您已經擁有能夠使用 Vertex AI 的 GCP 專案。有關所需設定的更多資訊以及完整的逐步說明,請參閱 使用 Anthropic 的 Claude 3 模型。
首先,為您選擇的語言安裝 Anthropic 的 client SDK。
pip install -U google-cloud-aiplatform "anthropic[vertex]"請注意,Anthropic 模型的可用性因地區而異。在 Vertex AI Model Garden 中搜尋「Claude」,或前往 使用 Claude 3 以取得最新資訊。
| 模型 | Vertex AI API 模型 ID |
|---|---|
| Claude Sonnet 4.5 | claude-sonnet-4-5@20250929 |
| Claude Sonnet 4 | claude-sonnet-4@20250514 |
| Claude Sonnet 3.7 ⚠️ | claude-3-7-sonnet@20250219 |
| Claude Opus 4.5 | claude-opus-4-5@20251101 |
| Claude Opus 4.1 | claude-opus-4-1@20250805 |
| Claude Opus 4 | claude-opus-4@20250514 |
| Claude Opus 3 ⚠️ | claude-3-opus@20240229 |
| Claude Haiku 4.5 | claude-haiku-4-5@20251001 |
| Claude Haiku 3.5 ⚠️ | claude-3-5-haiku@20241022 |
| Claude Haiku 3 | claude-3-haiku@20240307 |
在執行請求之前,您可能需要執行 gcloud auth application-default login 以使用 GCP 進行驗證。
以下範例顯示如何從 Vertex AI 上的 Claude 產生文字:
from anthropic import AnthropicVertex
project_id = "MY_PROJECT_ID"
region = "global"
client = AnthropicVertex(project_id=project_id, region=region)
message = client.messages.create(
model="claude-sonnet-4-5@20250929",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)有關更多詳細資訊,請參閱我們的 client SDK 和官方 Vertex AI 文件。
Vertex 提供 請求-回應記錄服務,允許客戶記錄與您的使用相關聯的提示和完成。
Anthropic 建議您至少在 30 天滾動基礎上記錄您的活動,以便了解您的活動並調查任何潛在的濫用。
啟用此服務不會給 Google 或 Anthropic 任何存取您內容的權限。
您可以在 此處 找到 Vertex 上目前支援的所有功能。
從 Claude Sonnet 4.5 和所有未來模型 開始,Google Vertex AI 提供兩種端點類型:
區域端點的定價比全球端點高出 10%。
這僅適用於 Claude Sonnet 4.5 和未來的模型。較舊的模型(Claude Sonnet 4、Opus 4 及更早版本)保持其現有的定價結構。
全球端點(建議):
區域端點:
使用全球端點(建議):
初始化用戶端時,將 region 參數設定為 "global":
from anthropic import AnthropicVertex
project_id = "MY_PROJECT_ID"
region = "global"
client = AnthropicVertex(project_id=project_id, region=region)
message = client.messages.create(
model="claude-sonnet-4-5@20250929",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)使用區域端點:
指定特定區域,例如 "us-east1" 或 "europe-west1":
from anthropic import AnthropicVertex
project_id = "MY_PROJECT_ID"
region = "us-east1" # Specify a specific region
client = AnthropicVertex(project_id=project_id, region=region)
message = client.messages.create(
model="claude-sonnet-4-5@20250929",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)