在 Google Cloud's Agent Platform 上存取 Claude 的 API 與 Messages API 幾乎相同,但在請求格式上有兩個主要差異:
model 不會在請求主體中傳遞,而是在 Google Cloud 端點 URL 中指定。anthropic_version 會在請求主體中傳遞(而非作為標頭),且必須設定為 vertex-2023-10-16 這個值。Anthropic 的官方用戶端 SDK 也支援 Agent Platform。本指南將引導您使用 Anthropic 的其中一個用戶端 SDK 向 Agent Platform 上的 Claude 發出請求。
請注意,本指南假設您已擁有可使用 Agent Platform 的 Google Cloud 專案。如需有關所需設定的更多資訊及完整操作說明,請參閱 Agent Platform 上的 Anthropic Claude 模型。
首先,請安裝您所選語言的 Anthropic 用戶端 SDK。
pip install -U "anthropic[vertex]"請注意,Anthropic 模型的可用性因地區而異。請在 Model Garden 中搜尋「Claude」,或前往 Anthropic Claude 模型以取得最新資訊。
生命週期術語(已棄用、已停用)的定義請參閱模型棄用。合作夥伴營運平台上的生命週期日期由合作夥伴設定,可能與 Claude API 的時程不同。如需 Agent Platform 上任何模型的目前停用日期,請參閱 Google Cloud 關於 Agent Platform 上 Claude 模型的文件。
| 模型 | Agent Platform API 模型 ID |
|---|---|
| Claude Fable 5 | |
| Claude Opus 5 | |
| Claude Opus 4.8 | |
| Claude Opus 4.7 | |
| Claude Opus 4.6 | |
| Claude Sonnet 5 | claude-sonnet-5 |
| Claude Sonnet 4.6 | |
| Claude Sonnet 4.5 | |
| Claude Sonnet 4 已棄用。 | |
| Claude Sonnet 3.7 已停用。 | |
| Claude Opus 4.5 | |
| Claude Opus 4.1 已棄用。 | |
| Claude Opus 4 已棄用。 | |
| Claude Haiku 4.5 | |
| Claude Haiku 3.5 已棄用。 |
在執行請求之前,您可能需要執行 gcloud auth application-default login 以向 Google Cloud 進行驗證。
以下範例展示如何在 Agent Platform 上使用 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-opus-5",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)如需更多詳細資訊,請參閱用戶端 SDK 和官方 Agent Platform 文件。
Claude 也可透過 Amazon Bedrock、AWS 上的 Claude Platform 和 Microsoft Foundry 取得。
此服務的資料處理由 Google Cloud 管理。如需詳細資訊,請參閱 Agent Platform 與零資料保留。
Agent Platform 提供請求-回應記錄服務,讓您可以記錄與您的使用情況相關的提示和完成內容。
Anthropic 建議您至少以 30 天滾動方式記錄您的活動,以便了解您的活動並調查任何潛在的濫用情況。
如需完整的功能清單及其在 Google Cloud 上的可用性,請參閱功能總覽。
fallbacks 參數;請改用用戶端備援模式)Claude Fable 5、Claude Opus 5、Claude Opus 4.8、Claude Opus 4.7、Claude Opus 4.6、Claude Sonnet 5 和 Claude Sonnet 4.6 在 Agent Platform 上具有 100 萬 token 的上下文視窗。其他 Claude 模型,包括 Sonnet 4.5 和 Sonnet 4(已棄用),則具有 20 萬 token 的上下文視窗。
Agent Platform 將請求酬載限制為 30 MB。當傳送大型文件或大量圖片時,您可能會在達到 token 限制之前先達到此限制。
Agent Platform 提供三種端點類型:
地區和多地區端點的價格比全域端點高 10%。
全域端點(建議):
多地區端點:
us 和 eu)跨地區動態路由請求地區端點:
使用全域端點(建議):
在初始化用戶端時,將 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-opus-5",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)使用多地區端點:
將 region 參數設定為多地區識別碼:美國為 "us",歐盟為 "eu"。SDK 會將請求路由至對應的多地區端點(https://aiplatform.us.rep.googleapis.com 或 https://aiplatform.eu.rep.googleapis.com),該端點會在該地理範圍內的各地區之間動態平衡流量。
from anthropic import AnthropicVertex
project_id = "MY_PROJECT_ID"
region = "us" # Multi-region identifier: "us" or "eu"
client = AnthropicVertex(project_id=project_id, region=region)
message = client.messages.create(
model="claude-opus-5",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)使用地區端點:
指定特定地區,例如 "us-east5" 或 "europe-west1":
from anthropic import AnthropicVertex
project_id = "MY_PROJECT_ID"
region = "us-east5" # Specify a specific region
client = AnthropicVertex(project_id=project_id, region=region)
message = client.messages.create(
# 特定區域端點支援 Claude Sonnet 4.6 及更早版本;較新的模型使用全球或多區域端點
model="claude-sonnet-4-6",
max_tokens=100,
messages=[
{
"role": "user",
"content": "Hey Claude!",
}
],
)
print(message)Was this page helpful?