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
    第一步

    開始使用 Claude Managed Agents

    建立您的第一個自主代理程式。

    Was this page helpful?

    • 安裝 CLI
    • 安裝 SDK

    本指南將引導您建立代理程式、設定環境、啟動工作階段,以及串流代理程式回應。

    核心概念

    ConceptDescription
    AgentThe model, system prompt, tools, MCP servers, and skills
    EnvironmentA configured container template (packages, network access)
    SessionA running agent instance within an environment, performing a specific task and generating outputs
    EventsMessages exchanged between your application and the agent (user turns, tool results, status updates)

    先決條件

    • 一個 Anthropic Console 帳戶
    • 一個 API 金鑰

    安裝 CLI

    確認安裝:

    ant --version

    安裝 SDK

    將您的 API 金鑰設定為環境變數:

    export ANTHROPIC_API_KEY="your-api-key-here"

    建立您的第一個工作階段

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

    運作原理

    當您傳送使用者事件時,Claude Managed Agents 會:

    1. 佈建容器: 您的環境設定決定了容器的建置方式。
    2. 執行代理程式迴圈: Claude 根據您的訊息決定使用哪些工具
    3. 執行工具: 檔案寫入、bash 命令和其他工具呼叫在容器內執行
    4. 串流事件: 您會在代理程式工作時收到即時更新
    5. 進入閒置: 當代理程式沒有更多事情要做時,會發出 session.status_idle 事件

    後續步驟

    定義您的代理程式

    建立可重複使用的版本化代理程式設定

    1. 1

      建立代理程式

      建立一個定義模型、系統提示和可用工具的代理程式。

      set -euo pipefail
      
      agent=$(
        curl -sS --fail-with-body https://api.anthropic.com/v1/agents \
          -H "x-api-key: $ANTHROPIC_API_KEY" \
          -H "anthropic-version: 2023-06-01" \
          -H "anthropic-beta: managed-agents-2026-04-01" \
          -H "content-type: application/json" \
          -d @- <<'EOF'
      {
        "name": "Coding Assistant",
        "model": "claude-sonnet-4-6",
        "system": "You are a helpful coding assistant. Write clean, well-documented code.",
        "tools": [
          {"type": "agent_toolset_20260401"}
        ]
      }
      EOF
      )
      
      AGENT_ID=$(jq -er '.id' <<<"$agent")
      AGENT_VERSION=$(jq -er '.version' <<<"$agent")
      
      echo "Agent ID: $AGENT_ID, version: $AGENT_VERSION"

      agent_toolset_20260401 工具類型可啟用完整的預建代理程式工具集(bash、檔案操作、網路搜尋等)。請參閱工具以取得完整清單和每個工具的設定選項。

      儲存返回的 agent.id。您將在每個建立的工作階段中參照它。

    2. 2

      建立環境

      環境定義了代理程式執行所在的容器。

      environment=$(
        curl -sS --fail-with-body https://api.anthropic.com/v1/environments \
          -H "x-api-key: $ANTHROPIC_API_KEY" \
          -H "anthropic-version: 2023-06-01" \
          -H "anthropic-beta: managed-agents-2026-04-01" \
          -H "content-type: application/json" \
          -d @- <<'EOF'
      {
        "name": "quickstart-env",
        "config": {
          "type": "cloud",
          "networking": {"type": "unrestricted"}
        }
      }
      EOF
      )
      
      ENVIRONMENT_ID=$(jq -er '.id' <<<"$environment")
      
      echo "Environment ID: $ENVIRONMENT_ID"

      儲存返回的 environment.id。您將在每個建立的工作階段中參照它。

    3. 3

      啟動工作階段

      建立一個參照您的代理程式和環境的工作階段。

      session=$(
        curl -sS --fail-with-body https://api.anthropic.com/v1/sessions \
          -H "x-api-key: $ANTHROPIC_API_KEY" \
          -H "anthropic-version: 2023-06-01" \
          -H "anthropic-beta: managed-agents-2026-04-01" \
          -H "content-type: application/json" \
          -d @- <<EOF
      {
        "agent": "$AGENT_ID",
        "environment_id": "$ENVIRONMENT_ID",
        "title": "Quickstart session"
      }
      EOF
      )
      
      SESSION_ID=$(jq -er '.id' <<<"$session")
      
      echo "Session ID: $SESSION_ID"
    4. 4

      傳送訊息並串流回應

      開啟串流、傳送使用者事件,然後在事件到達時處理它們:

      # Send the user message first; the API buffers events until the stream attaches
      curl -sS --fail-with-body \
        "https://api.anthropic.com/v1/sessions/$SESSION_ID/events" \
        -H "x-api-key: $ANTHROPIC_API_KEY" \
        -H "anthropic-version: 2023-06-01" \
        -H "anthropic-beta: managed-agents-2026-04-01" \
        -H "content-type: application/json" \
        -d @- >/dev/null <<'EOF'
      {
        "events": [
          {
            "type": "user.message",
            "content": [
              {
                "type": "text",
                "text": "Create a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt"
              }
            ]
          }
        ]
      }
      EOF
      
      # Open the SSE stream and process events as they arrive
      while IFS= read -r line; do
        [[ $line == data:* ]] || continue
        json=${line#data: }
        case $(jq -r '.type' <<<"$json") in
          agent.message)
            jq -j '.content[] | select(.type == "text") | .text' <<<"$json"
            ;;
          agent.tool_use)
            printf '\n[Using tool: %s]\n' "$(jq -r '.name' <<<"$json")"
            ;;
          session.status_idle)
            printf '\n\nAgent finished.\n'
            break
            ;;
        esac
      done < <(
        curl -sS -N --fail-with-body \
          "https://api.anthropic.com/v1/sessions/$SESSION_ID/stream" \
          -H "x-api-key: $ANTHROPIC_API_KEY" \
          -H "anthropic-version: 2023-06-01" \
          -H "anthropic-beta: managed-agents-2026-04-01" \
          -H "Accept: text/event-stream"
      )

      代理程式將編寫一個 Python 腳本,在容器中執行它,並驗證輸出檔案是否已建立。您的輸出將類似於以下內容:

      I'll create a Python script that generates the first 20 Fibonacci numbers and saves them to a file.
      [Using tool: write]
      [Using tool: bash]
      The script ran successfully. Let me verify the output file.
      [Using tool: bash]
      fibonacci.txt contains the first 20 Fibonacci numbers (0 through 4181).
      
      Agent finished.
    設定環境

    自訂網路和容器設定

    代理程式工具

    為您的代理程式啟用特定工具

    事件與串流

    處理事件並在執行過程中引導代理程式