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
    配置智能体环境

    云环境设置

    为您的会话自定义云容器。

    Was this page helpful?

    环境定义了代理运行的容器配置。您创建一次环境,然后每次启动会话时引用其 ID。多个会话可以共享同一环境,但每个会话都会获得自己的隔离容器实例。

    所有 Managed Agents API 请求都需要 managed-agents-2026-04-01 beta 标头。SDK 会自动设置 beta 标头。

    创建环境

    environment=$(curl -fsS 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" \
      --data @- <<'EOF'
    {
      "name": "python-dev",
      "config": {
        "type": "cloud",
        "networking": {"type": "unrestricted"}
      }
    }
    EOF
    )
    environment_id=$(jq -r '.id' <<< "$environment")
    
    echo "Environment ID: $environment_id"

    name 必须在您的组织和工作区内唯一。

    在会话中使用环境

    创建会话时,将环境 ID 作为字符串传递。

    配置选项

    包

    packages 字段在代理启动前将包预安装到容器中。包由各自的包管理器安装,并在共享同一环境的会话中缓存。指定多个包管理器时,它们按字母顺序运行(apt、cargo、gem、go、npm、pip)。您可以选择固定特定版本;默认为最新版本。

    支持的包管理器:

    字段包管理器示例
    apt系统包(apt-get)"ffmpeg"
    cargoRust(cargo)"[email protected]"
    gemRuby(gem)"rails:7.1.0"
    goGo 模块"golang.org/x/tools/cmd/goimports@latest"
    npmNode.js(npm)"[email protected]"

    网络

    networking 字段控制容器的出站网络访问。它不影响 web_search 或 web_fetch 工具的允许域。

    模式描述
    unrestricted完全出站网络访问,除了一般安全黑名单。这是默认值。
    limited将容器网络访问限制为 allowed_hosts 列表。进一步的访问通过 allow_package_managers 和 allow_mcp_servers 布尔值启用。

    对于生产部署,使用 limited 网络和明确的 allowed_hosts 列表。遵循最小权限原则,仅授予代理所需的最小网络访问权限,并定期审计您的允许域。

    使用 limited 网络时:

    • allowed_hosts 指定容器可以访问的域。这些必须以 HTTPS 为前缀。
    • allow_mcp_servers 允许出站访问在代理上配置的 MCP 服务器端点,超出 allowed_hosts 数组中列出的端点。默认为 false。
    • allow_package_managers 允许出站访问公共包注册表(PyPI、npm 等),超出 allowed_hosts 数组中列出的端点。默认为 false。

    环境生命周期

    • 环境持续存在,直到明确存档或删除。
    • 多个会话可以引用同一环境。
    • 每个会话都获得自己的容器实例。会话不共享文件系统状态。
    • 环境不进行版本控制。如果您经常更新环境,您可能希望在您这一侧记录这些更新,以将环境状态与会话进行映射。

    管理环境

    预安装的运行时

    云容器开箱即用地包含常见运行时。有关预安装语言、数据库和实用程序的完整列表,请参阅容器参考。

    session=$(curl -fsS 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" \
      --data @- <<EOF
    {
      "agent": "$agent_id",
      "environment_id": "$environment_id"
    }
    EOF
    )
    environment=$(curl -fsS 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" \
      --data @- <<'EOF'
    {
      "name": "data-analysis",
      "config": {
        "type": "cloud",
        "packages": {
          "pip": ["pandas", "numpy", "scikit-learn"],
          "npm": ["express"]
        },
        "networking": {"type": "unrestricted"}
      }
    }
    EOF
    )
    pipPython(pip)"pandas==2.2.0"
    config=$(cat <<'EOF'
    {
      "type": "cloud",
      "networking": {
        "type": "limited",
        "allowed_hosts": ["api.example.com"],
        "allow_mcp_servers": true,
        "allow_package_managers": true
      }
    }
    EOF
    )
    # List environments
    environments=$(curl -fsS 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")
    
    # Retrieve a specific environment
    env=$(curl -fsS "https://api.anthropic.com/v1/environments/$environment_id" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01")
    
    # Archive an environment (read-only, existing sessions continue)
    curl -fsS -X POST "https://api.anthropic.com/v1/environments/$environment_id/archive" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01"
    
    # Delete an environment (only if no sessions reference it)
    curl -fsS -X DELETE "https://api.anthropic.com/v1/environments/$environment_id" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01"