Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K

    第一步

    Claude 介紹快速入門

    模型與定價

    模型概覽選擇模型Claude 4.5 的新功能遷移到 Claude 4.5模型棄用定價

    使用 Claude 建構

    功能概覽使用 Messages API上下文視窗提示詞最佳實踐

    功能

    提示詞快取上下文編輯延伸思考串流訊息批次處理引用多語言支援Token 計數嵌入向量視覺PDF 支援Files API搜尋結果Google Sheets 附加元件

    工具

    概述如何實現工具使用代幣高效工具使用細粒度工具串流Bash 工具代碼執行工具電腦使用工具文字編輯工具網頁擷取工具網路搜尋工具記憶工具

    代理技能

    概述在 API 中開始使用 Agent Skills技能編寫最佳實踐使用 Agent Skills 與 API

    Agent SDK

    概述Agent SDK 參考 - TypeScriptPython SDK

    指南

    串流輸入處理權限會話管理託管 Agent SDK修改系統提示SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的代理技能追蹤成本和使用量待辦事項清單SDK 中的外掛程式

    API 中的 MCP

    MCP 連接器遠端 MCP 伺服器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示工程

    概述提示詞生成器使用提示模板提示詞改進器保持清晰和直接使用範例(多樣提示)讓 Claude 思考(思維鏈)使用 XML 標籤給 Claude 分配角色(系統提示詞)預填 Claude 的回應串接複雜提示長文本技巧延伸思考技巧

    測試與評估

    定義成功標準開發測試案例使用評估工具降低延遲

    加強防護措施

    減少幻覺提高輸出一致性防範越獄handle-streaming-refusals減少提示詞洩漏保持 Claude 的角色特性

    管理和監控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    管理和監控

    使用量和成本 API

    使用使用量和成本管理員 API 以程式化方式存取您組織的 API 使用量和成本資料。

    The Admin API is unavailable for individual accounts. To collaborate with teammates and add members, set up your organization in Console → Settings → Organization.

    使用量和成本管理員 API 提供對您組織歷史 API 使用量和成本資料的程式化和細粒度存取。此資料類似於 Claude Console 的使用量和成本頁面中可用的資訊。

    此 API 讓您能夠更好地監控、分析和最佳化您的 Claude 實作:

    • 準確的使用量追蹤: 取得精確的 token 計數和使用模式,而不是僅依賴回應 token 計數
    • 成本對帳: 將內部記錄與 Anthropic 帳單進行匹配,供財務和會計團隊使用
    • 產品效能和改進: 監控產品效能,同時測量系統變更是否有所改善,或設定警報
    • 速率限制和優先層級最佳化: 最佳化提示快取或特定提示等功能,以充分利用分配的容量,或購買專用容量。
    • 進階分析: 執行比 Console 中可用的更深入的資料分析

    需要管理員 API 金鑰

    此 API 是管理員 API的一部分。這些端點需要管理員 API 金鑰(以 sk-ant-admin... 開頭),與標準 API 金鑰不同。只有具有管理員角色的組織成員才能透過 Claude Console 提供管理員 API 金鑰。

    合作夥伴解決方案

    領先的可觀測性平台提供現成的整合,用於監控您的 Claude API 使用量和成本,無需編寫自訂程式碼。這些整合提供儀表板、警報和分析,幫助您有效管理 API 使用量。

    Datadog

    具有自動追蹤和監控的 LLM 可觀測性

    Grafana Cloud

    無代理整合,提供開箱即用的儀表板和警報,輕鬆實現 LLM 可觀測性

    Honeycomb

    透過 OpenTelemetry 進行進階查詢和視覺化

    快速開始

    取得您組織過去 7 天的每日使用量:

    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-08T00:00:00Z&\
    ending_at=2025-01-15T00:00:00Z&\
    bucket_width=1d" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    為整合設定 User-Agent 標頭

    如果您正在建立整合,請設定您的 User-Agent 標頭以幫助我們了解使用模式:

    User-Agent: YourApp/1.0.0 (https://yourapp.com)

    使用量 API

    使用 /v1/organizations/usage_report/messages 端點追蹤整個組織的 token 消耗,並按模型、工作區和服務層級進行詳細分解。

    關鍵概念

    • 時間桶: 以固定間隔(1m、1h 或 1d)聚合使用量資料
    • Token 追蹤: 測量未快取輸入、快取輸入、快取建立和輸出 token
    • 篩選和分組: 按 API 金鑰、工作區、模型、服務層級或上下文視窗篩選,並按這些維度分組結果
    • 伺服器工具使用量: 追蹤伺服器端工具(如網路搜尋)的使用量

    有關完整的參數詳細資訊和回應架構,請參閱使用量 API 參考。

    基本範例

    按模型的每日使用量

    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-01T00:00:00Z&\
    ending_at=2025-01-08T00:00:00Z&\
    group_by[]=model&\
    bucket_width=1d" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    帶篩選的每小時使用量

    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-15T00:00:00Z&\
    ending_at=2025-01-15T23:59:59Z&\
    models[]=claude-sonnet-4-5-20250929&\
    service_tiers[]=batch&\
    context_window[]=0-200k&\
    bucket_width=1h" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    按 API 金鑰和工作區篩選使用量

    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-01T00:00:00Z&\
    ending_at=2025-01-08T00:00:00Z&\
    api_key_ids[]=apikey_01Rj2N8SVvo6BePZj99NhmiT&\
    api_key_ids[]=apikey_01ABC123DEF456GHI789JKL&\
    workspace_ids[]=wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ&\
    workspace_ids[]=wrkspc_01XYZ789ABC123DEF456MNO&\
    bucket_width=1d" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    要檢索您組織的 API 金鑰 ID,請使用列出 API 金鑰端點。

    要檢索您組織的工作區 ID,請使用列出工作區端點,或在 Anthropic Console 中找到您組織的工作區 ID。

    時間粒度限制

    粒度預設限制最大限制使用案例
    1m60 個桶1440 個桶即時監控
    1h24 個桶168 個桶每日模式
    1d7 個桶31 個桶每週/每月報告

    成本 API

    使用 /v1/organizations/cost_report 端點檢索以美元為單位的服務級成本分解。

    關鍵概念

    • 貨幣: 所有成本以美元計算,以最小單位(分)的十進位字串報告
    • 成本類型: 追蹤 token 使用量、網路搜尋和程式碼執行成本
    • 分組: 按工作區或描述分組成本以進行詳細分解
    • 時間桶: 僅每日粒度(1d)

    有關完整的參數詳細資訊和回應架構,請參閱成本 API 參考。

    優先層級成本使用不同的計費模式,不包含在成本端點中。請透過使用量端點追蹤優先層級使用量。

    基本範例

    curl "https://api.anthropic.com/v1/organizations/cost_report?\
    starting_at=2025-01-01T00:00:00Z&\
    ending_at=2025-01-31T00:00:00Z&\
    group_by[]=workspace_id&\
    group_by[]=description" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    分頁

    兩個端點都支援大型資料集的分頁:

    1. 發出您的初始請求
    2. 如果 has_more 為 true,在下一個請求中使用 next_page 值
    3. 繼續直到 has_more 為 false
    # 第一個請求
    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-01T00:00:00Z&\
    ending_at=2025-01-31T00:00:00Z&\
    limit=7" \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"
    
    # 回應包含:"has_more": true, "next_page": "page_xyz..."
    
    # 帶分頁的下一個請求
    curl "https://api.anthropic.com/v1/organizations/usage_report/messages?\
    starting_at=2025-01-01T00:00:00Z&\
    ending_at=2025-01-31T00:00:00Z&\
    limit=7&\
    page=page_xyz..." \
      --header "anthropic-version: 2023-06-01" \
      --header "x-api-key: $ADMIN_API_KEY"

    常見使用案例

    在 anthropic-cookbook 中探索詳細實作:

    • 每日使用量報告: 追蹤 token 消耗趨勢
    • 成本歸屬: 按工作區分配費用以進行退費
    • 快取效率: 測量和最佳化提示快取
    • 預算監控: 為支出閾值設定警報
    • CSV 匯出: 為財務團隊產生報告

    常見問題

    資料有多新?

    使用量和成本資料通常在 API 請求完成後 5 分鐘內出現,儘管偶爾可能會有更長的延遲。

    建議的輪詢頻率是多少?

    API 支援每分鐘輪詢一次以持續使用。對於短暫爆發(例如,下載分頁資料),更頻繁的輪詢是可以接受的。為需要頻繁更新的儀表板快取結果。

    如何追蹤程式碼執行使用量?

    程式碼執行成本出現在成本端點中,在描述欄位中分組為 Code Execution Usage。程式碼執行不包含在使用量端點中。

    如何追蹤優先層級使用量?

    在使用量端點中按 service_tier 篩選或分組,並尋找 priority 值。優先層級成本在成本端點中不可用。

    Workbench 使用量會發生什麼?

    來自 Workbench 的 API 使用量不與 API 金鑰關聯,因此即使按該維度分組,api_key_id 也會是 null。

    預設工作區如何表示?

    歸屬於預設工作區的使用量和成本的 workspace_id 值為 null。

    如何取得 Claude Code 的每用戶成本分解?

    使用 Claude Code Analytics API,它提供每用戶估計成本和生產力指標,而不會有按許多 API 金鑰分解成本的效能限制。對於具有許多金鑰的一般 API 使用量,請使用使用量 API追蹤 token 消耗作為成本代理。

    另請參閱

    使用量和成本 API 可用於幫助您為用戶提供更好的體驗、幫助您管理成本並保持您的速率限制。了解更多關於這些其他功能的資訊:

    • 管理員 API 概述
    • 管理員 API 參考
    • 定價
    • 提示快取 - 透過快取最佳化成本
    • 批次處理 - 批次請求享 50% 折扣
    • 速率限制 - 了解使用量層級
    • 使用量 API
    • 成本 API
    • Workbench 使用量會發生什麼?
    • 如何取得 Claude Code 的每用戶成本分解?
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC