本指南說明如何使用 Anthropic 的用戶端 SDK 或直接發送 HTTP 請求,在 Microsoft Foundry 中設定並對 Claude 進行 API 呼叫。當您透過 Microsoft Foundry 存取 Claude 時,Claude 的使用費用會在 Azure Marketplace 中計費。您可以使用最新的 Claude 模型,包括 Claude Opus 5、Claude Opus 4.8 與 Claude Sonnet 5,以及 100 萬 token 的「context window」(上下文視窗)等功能,同時透過您的 Azure 訂閱管理成本。
Claude 在 Foundry 資源中提供 Global Standard 與 US Data Zone Standard 兩種部署類型,並透過 Azure Marketplace 以 Claude Consumption Units 計費。詳情請參閱 Microsoft Foundry 中的 Claude 定價。
Microsoft Foundry 中的 Claude 模型提供兩種託管選項。您在設定部署時選擇託管選項。
| 託管於 Azure | 託管於 Anthropic | |
|---|---|---|
| 推論執行位置 | 由 Anthropic 營運、在 Azure 基礎設施上執行的服務 | 由 Anthropic 營運、在 Anthropic 基礎設施上執行的服務 |
| 模型可用性 | Opus、Sonnet 與 Haiku 系列的最新模型 | Microsoft Foundry 上提供的所有 Claude 模型 |
| 部署類型 | Global Standard、US Data Zone Standard | Global Standard |
| 建議用途 | 大多數工作負載 | 存取尚未託管於 Azure 的功能或模型 |
開始之前,請確認您具備:
Anthropic 的用戶端 SDK 透過平台專屬套件或用戶端類別支援 Foundry。本頁的範例也展示了使用 cURL 與 ant CLI 的請求。若要設定 CLI,請參閱 CLI 快速入門。
pip install -U "anthropic"
# 若使用 Entra ID 驗證,請一併安裝 Azure Identity 程式庫
pip install azure-identityFoundry 採用兩層階層結構:資源(resources)包含您的安全性與計費設定,而部署(deployments)則是您透過 API 呼叫的模型實例。您會先建立一個 Foundry 資源,然後在其中建立一個或多個 Claude 部署。
建立 Foundry 資源,這是在 Azure 中使用與管理服務所必需的。您可以依照這些說明建立 Foundry 資源。或者,您也可以從建立 Foundry 專案開始,其過程包含建立 Foundry 資源。
若要佈建您的資源:
{resource} 使用它(例如 https://{resource}.services.ai.azure.com/anthropic/v1/*)。建立資源後,部署 Claude 模型以供 API 呼叫使用。以下步驟描述新版 Foundry 入口網站(New Foundry 切換開關為開啟狀態):
my-claude-deployment)。部署名稱在建立後無法變更。inference_geo: "us"。若 New Foundry 切換開關為關閉狀態,表示您處於傳統入口網站版面配置。在該版面中,開啟左側窗格中的 Model catalog 以尋找並部署模型,並開啟 Models + endpoints(位於 My assets 下)以檢視您的部署及其端點詳細資料。
Microsoft Foundry 中的 Claude 支援兩種驗證方式:API 金鑰與 Entra ID 權杖。兩種方式皆使用格式為 https://{resource}.services.ai.azure.com/anthropic/v1/* 的 Azure 託管端點。
佈建 Foundry Claude 資源後,您可以從 Foundry 入口網站取得 API 金鑰:
api-key 或 x-api-key 標頭,或將其提供給 SDK。Foundry SDK 需要 API 金鑰以及資源名稱或基底 URL 其中之一。若已定義下列環境變數,C#、Java、PHP、Python 與 TypeScript SDK 會自動讀取:
ANTHROPIC_FOUNDRY_API_KEY - 您的 API 金鑰ANTHROPIC_FOUNDRY_RESOURCE - 您的資源名稱(例如 example-resource)ANTHROPIC_FOUNDRY_BASE_URL - 資源名稱的替代方案:完整的基底 URL(例如 https://example-resource.services.ai.azure.com/anthropic/)。C# SDK 不會讀取此變數:它一律從資源名稱建構基底 URL。使用 API 金鑰的範例:
import os
from anthropic import AnthropicFoundry
client = AnthropicFoundry(
api_key=os.environ.get("ANTHROPIC_FOUNDRY_API_KEY"),
resource="example-resource", # your resource name
)
message = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)
print(message.content)Entra ID 驗證讓您能以 Azure RBAC 管理存取權、與組織的身分識別管理整合,並避免手動處理 API 金鑰。若要使用 Entra ID 權杖:
Authorization: Bearer {TOKEN} 標頭中使用該權杖。使用 Entra ID 的範例:
from anthropic import AnthropicFoundry
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
# 使用 token provider 模式取得 Microsoft Entra ID 權杖
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://ai.azure.com/.default"
)
# 使用 Entra ID 驗證建立用戶端
client = AnthropicFoundry(
resource="example-resource", # your resource name
azure_ad_token_provider=token_provider, # Use token provider for Entra ID auth
)
# 發送請求
message = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)
print(message.content)Foundry 會在 HTTP 回應標頭中包含請求識別碼,以供偵錯與追蹤。聯絡支援團隊時,請同時提供 request-id 與 apim-request-id(Azure API Management)的值,以協助團隊在 Anthropic 與 Azure 系統中快速找到並調查您的請求。
Microsoft Foundry 中的 Claude 支援大多數 Claude 功能。您可以在功能總覽中找到目前支援的所有功能。
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 在 Microsoft Foundry 上具有 100 萬 token 的上下文視窗。其他 Claude 模型(包括 Claude Sonnet 4.5)則具有 20 萬 token 的上下文視窗。
fallbacks 參數;請改用用戶端備援模式)下列功能可用於託管於 Anthropic 的部署,但不支援託管於 Azure 的部署:
依設計,對託管於 Azure 的部署使用這些功能的請求會傳回 400 Bad Request 錯誤。Claude Code 會偵測託管於 Azure 的部署,並自動調整其功能集。
Microsoft Foundry 中的 Claude 的 API 回應遵循標準的 Claude API 回應格式。這包括回應主體中的 usage 物件,它提供您請求的詳細 token 消耗資訊。usage 物件在所有平台(Claude API、Amazon Bedrock、Claude Platform on AWS、Foundry 與 Google Cloud)上皆一致。
有關 Foundry 專屬回應標頭的詳細資訊,請參閱關聯請求 ID。
生命週期術語(Deprecated、Retired)定義於模型棄用。Microsoft Foundry 遵循 Claude API 的生命週期時程。
下列 Claude 模型可透過 Foundry 使用:
| 模型 | 預設部署名稱 | 託管於 Azure | 託管於 Anthropic |
|---|---|---|---|
| Claude Fable 5 | ✓ | ||
| Claude Opus 5 | ✓ | ✓ | |
| Claude Opus 4.8 | ✓ | ✓ | |
| Claude Opus 4.7 | ✓ | ||
| Claude Opus 4.6 | ✓ | ||
| Claude Opus 4.5 | ✓ | ||
| Claude Sonnet 5 | ✓ | ✓ | |
| Claude Sonnet 4.6 | ✓ | ||
| Claude Sonnet 4.5 | ✓ | ||
| Claude Haiku 4.5 | ✓ | ✓ |
預設情況下,部署名稱與上表所示的模型 ID 相符。不過,您可以在 Foundry 入口網站中建立不同名稱的自訂部署,以管理不同的設定、版本或速率限制。請在 API 請求中使用部署名稱(不一定是模型 ID)。
Microsoft Foundry 中的 Claude 透過 Azure Marketplace 計費。使用量以 Claude Consumption Units(CCU)計價,每小時計量,並於每月事後在您的 Azure 帳單上開立發票。CCU 並非預付點數。沒有 CCU 餘額或承諾用量。
有關 CCU 價格、換算機制與各模型的 token 費率,請參閱 Microsoft Foundry 中的 Claude 定價。
若要將現有部署從一種託管選項移至另一種:
model 參數中傳入新的部署名稱。若新部署位於同一個 Foundry 資源中,您的端點 URL 與驗證方式維持不變。若您建立了新資源,請更新應用程式的端點與憑證以指向該資源。
Azure 透過標準的 Azure 模式為您的 Claude 使用量提供監控與記錄:
Anthropic 建議至少以 30 天滾動方式記錄您的活動,以了解使用模式並調查任何潛在問題。
錯誤:401 Unauthorized 或 Invalid API key
錯誤:403 Forbidden
錯誤:429 Too Many Requests
Foundry 不會在回應中包含 Anthropic 的標準速率限制標頭(anthropic-ratelimit-tokens-limit、anthropic-ratelimit-tokens-remaining、anthropic-ratelimit-tokens-reset、anthropic-ratelimit-input-tokens-limit、anthropic-ratelimit-input-tokens-remaining、anthropic-ratelimit-input-tokens-reset、anthropic-ratelimit-output-tokens-limit、anthropic-ratelimit-output-tokens-remaining 與 anthropic-ratelimit-output-tokens-reset)。請改為透過 Azure 的監控工具管理速率限制。
錯誤:Model not found 或 Deployment not found
錯誤:Invalid model parameter
探索 Claude 的進階功能與能力。
了解 Anthropic 針對模型與功能的定價結構。
隨著更安全、更強大的模型推出,Anthropic 會定期淘汰較舊的模型。查看所有 API 棄用項目以及建議的替代方案。
瀏覽 Foundry 目錄中的 Anthropic 模型。
檢視 Microsoft 的 Azure AI Foundry 定價詳細資料。
檢視 Anthropic 各模型的定價詳細資料。
管理您的 Azure 資源。
Was this page helpful?