通过 Vertex API 访问 Claude 与 Messages API 几乎相同,支持所有相同的选项,但有两个关键区别:
model 不在请求体中传递。相反,它在 Google Cloud 端点 URL 中指定。anthropic_version 在请求体中传递(而不是作为标头),并且必须设置为值 vertex-2023-10-16。Vertex 也受到 Anthropic 官方 客户端 SDK 的支持。本指南将引导您完成在 Python 或 TypeScript 中向 Vertex AI 上的 Claude 发出请求的过程。
请注意,本指南假设您已经拥有能够使用 Vertex AI 的 GCP 项目。有关所需的设置以及完整的演练,请参阅 使用 Anthropic 的 Claude 3 模型。
首先,为您选择的语言安装 Anthropic 的 客户端 SDK。
pip install -U google-cloud-aiplatform "anthropic[vertex]"请注意,Anthropic 模型的可用性因地区而异。在 Vertex AI 模型库 中搜索"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)有关更多详细信息,请参阅我们的 客户端 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)