Loading...
    • 建構
    • 管理
    • 模型與定價
    • 客戶端 SDK
    • API 參考
    Search...
    ⌘K
    第一步
    Claude 簡介快速入門
    使用 Claude 建構
    功能概覽使用 Messages API處理停止原因
    模型功能
    延伸思考自適應思考效能快速模式(測試版:研究預覽)結構化輸出引用來源串流訊息批次處理搜尋結果串流拒絕多語言支援嵌入向量
    工具
    概覽工具使用方式網路搜尋工具網路擷取工具程式碼執行工具記憶體工具Bash 工具電腦使用工具文字編輯器工具
    工具基礎架構
    工具搜尋程式化工具呼叫細粒度工具串流
    上下文管理
    上下文視窗壓縮上下文編輯提示快取Token 計數
    處理檔案
    Files APIPDF 支援圖像與視覺
    技能
    概覽快速入門最佳實踐企業版技能API 中的技能
    MCP
    遠端 MCP 伺服器MCP 連接器
    提示工程
    概覽提示最佳實踐Console 提示工具
    測試與評估
    定義成功標準並建立評估在 Console 中使用評估工具降低延遲
    強化防護欄
    減少幻覺提高輸出一致性防範越獄減少提示洩漏
    資源
    詞彙表
    版本說明
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

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

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    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

    Learn

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

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    將工作委派給代理

    新增檔案

    透過 Files API 上傳檔案並將其掛載至容器中以供讀取和處理。

    您可以透過 Files API 上傳檔案並將其掛載至工作階段的容器中,藉此提供檔案給您的代理程式。

    所有 Managed Agents API 請求都需要 managed-agents-2026-04-01 beta 標頭。SDK 會自動設定 beta 標頭。

    上傳檔案

    首先,使用 Files API 上傳檔案:

    file=$(curl --fail-with-body -sS "${auth[@]}" \
      "${base_url}/files" \
      -F [email protected])
    file_id=$(jq -er '.id' <<<"${file}")
    printf 'File ID: %s\n' "${file_id}"

    在工作階段中掛載檔案

    建立工作階段時,將已上傳的檔案新增至 resources 陣列,即可將其掛載至容器中:

    mount_path 為選填項目,但請確保上傳的檔案具有描述性名稱,以便代理程式知道它在尋找什麼。

    session=$(
      jq -n \
        --arg agent_id "${agent_id}" \
        --arg environment_id "${environment_id}" \
        --arg file_id "${file_id}" \
        '{
          agent: $agent_id,
          environment_id: $environment_id,
          resources: [
            {
              type: "file",
              file_id: $file_id,
              mount_path: "/workspace/data.csv"
            }
          ]
        }' | curl --fail-with-body -sS "${auth[@]}" "${base_url}/sessions" --json @-
    )
    session_id=$(jq -er '.id' <<<"${session}")

    系統將建立一個新的 file_id,用於參照工作階段中的檔案實例。這些副本不會計入您的儲存空間限制。

    多個檔案

    透過在 resources 陣列中新增項目來掛載多個檔案:

    "resources": [
      { "type": "file", "file_id": "file_abc123", "mount_path": "/workspace/data.csv" },
      { "type": "file", "file_id": "file_def456", "mount_path": "/workspace/config.json" },
      { "type": "file", "file_id": "file_ghi789", "mount_path": "/workspace/src/main.py" }
    ]

    每個工作階段最多支援 100 個檔案。

    管理執行中工作階段的檔案

    您可以在建立工作階段後,使用工作階段資源 API 新增或移除檔案。每個資源在新增(或列出)時都會傳回一個 id,您可使用此 id 進行刪除操作。

    resource=$(
      jq -n --arg file_id "${file_id}" '{type: "file", file_id: $file_id}' \
        | curl --fail-with-body -sS "${auth[@]}" \
            "${base_url}/sessions/${session_id}/resources" --json @-
    )
    resource_id=$(jq -er '.id' <<<"${resource}")
    printf '%s\n' "${resource_id}"  # "sesrsc_01ABC..."

    使用 resources.list 列出工作階段上的所有資源。若要移除檔案,請使用資源 ID 呼叫 resources.delete:

    curl --fail-with-body -sS "${auth[@]}" \
      "${base_url}/sessions/${session_id}/resources" \
      | jq -r '.data[] | "\(.id) \(.type)"'
    
    curl --fail-with-body -sS "${auth[@]}" -X DELETE \
      "${base_url}/sessions/${session_id}/resources/${resource_id}" >/dev/null

    列出及下載工作階段檔案

    使用 Files API 列出工作階段範圍內的檔案並下載它們。

    # 列出與工作階段相關聯的檔案
    curl -fsSL "https://api.anthropic.com/v1/files?scope_id=sess_abc123" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: files-api-2025-04-14"
    
    # 下載檔案
    curl -fsSL "https://api.anthropic.com/v1/files/$FILE_ID/content" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: files-api-2025-04-14" \
      -o output.txt

    支援的檔案類型

    代理程式可以處理任何檔案類型,包括:

    • 原始碼(.py、.js、.ts、.go、.rs 等)
    • 資料檔案(.csv、.json、.xml、.yaml)
    • 文件(.txt、.md)
    • 壓縮檔(.zip、.tar.gz)— 代理程式可使用 bash 解壓縮這些檔案
    • 二進位檔案 — 代理程式可使用適當的工具處理這些檔案

    檔案路徑

    掛載至容器中的檔案為唯讀副本。代理程式可以讀取這些檔案,但無法修改原始上傳的檔案。若要使用修改後的版本,代理程式會在容器內的新路徑中寫入。

    • 檔案會掛載至您指定的確切路徑
    • 父目錄會自動建立
    • 路徑應為絕對路徑(以 / 開頭)

    Was this page helpful?