Claude 可以通过计算机使用工具与计算机环境交互,该工具提供截图功能和鼠标/键盘控制,用于自主桌面交互。
计算机使用是一项测试版功能,使 Claude 能够与桌面环境交互。此工具提供:
虽然计算机使用可以与 bash 和文本编辑器等其他工具结合使用以实现更全面的自动化工作流,但计算机使用特别指的是计算机使用工具查看和控制桌面环境的能力。
计算机使用适用于以下 Claude 模型:
| 模型 | 工具版本 | 测试版标志 |
|---|---|---|
| Claude Opus 4.5 | computer_20251124 | computer-use-2025-11-24 |
| Claude 4 模型 | computer_20250124 | computer-use-2025-01-24 |
| Claude Sonnet 3.7(已弃用) | computer_20250124 | computer-use-2025-01-24 |
Claude Opus 4.5 引入了 computer_20251124 工具版本,具有新功能,包括用于详细屏幕区域检查的缩放操作。Claude 4 模型使用针对新架构优化的更新工具版本。Claude Sonnet 3.7(已弃用)引入了额外功能,包括思考功能,以便更深入地了解模型的推理过程。
较旧的工具版本不保证与较新的模型向后兼容。始终使用与您的模型版本相对应的工具版本。
计算机使用是一项测试版功能,具有不同于标准 API 功能的独特风险。与互联网交互时,这些风险会增加。为了最小化风险,请考虑采取以下预防措施:
在某些情况下,Claude 会遵循内容中发现的命令,即使这与用户的指示相冲突。例如,网页上或图像中包含的 Claude 指示可能会覆盖指示或导致 Claude 犯错。我们建议采取预防措施将 Claude 与敏感数据和操作隔离,以避免与提示注入相关的风险。
我们已经训练模型抵抗这些提示注入,并添加了额外的防御层。如果您使用我们的计算机使用工具,我们将自动在您的提示上运行分类器,以标记潜在的提示注入实例。当这些分类器在屏幕截图中识别出潜在的提示注入时,它们会自动引导模型在继续下一步操作之前要求用户确认。我们认识到这种额外的保护对于每个用例都不是理想的(例如,没有人工参与的用例),因此如果您想选择退出并关闭它,请 联系我们。
我们仍然建议采取预防措施将 Claude 与敏感数据和操作隔离,以避免与提示注入相关的风险。
最后,请在您自己的产品中启用计算机使用之前,向最终用户告知相关风险并获得他们的同意。
使用我们的计算机使用参考实现快速入门,该实现包括 Web 界面、Docker 容器、示例工具实现和代理循环。
注意: 该实现已更新,包括 Claude 4 模型和 Claude Sonnet 3.7 的新工具。请确保拉取最新版本的仓库以访问这些新功能。
请使用 此表单 提供关于模型响应质量、API 本身或文档质量的反馈 - 我们迫不及待地想听到您的意见!
以下是如何开始使用计算机使用的方法:
测试版标头仅对计算机使用工具是必需的。
上面的示例显示了所有三个工具一起使用,这需要测试版标头,因为它包括计算机使用工具。
1. 为 Claude 提供计算机使用工具和用户提示
2. Claude 决定使用计算机使用工具
stop_reason 为 tool_use,表示 Claude 的意图。3. 提取工具输入,在计算机上评估工具,并返回结果
tool_result 内容块的新 user 消息。4. Claude 继续调用计算机使用工具,直到完成任务
我们将第 3 步和第 4 步在没有用户输入的情况下的重复称为"代理循环"——即 Claude 响应工具使用请求,您的应用程序用评估该请求的结果响应 Claude。
Claude 可以通过计算机使用工具与计算机环境交互,该工具提供屏幕截图功能以及鼠标/键盘控制,用于自主桌面交互。
计算机使用是一项测试版功能,使 Claude 能够与桌面环境交互。该工具提供:
虽然计算机使用可以通过 bash 和文本编辑器等其他工具进行增强,以实现更全面的自动化工作流,但计算机使用特别指的是计算机使用工具查看和控制桌面环境的能力。
计算机使用适用于以下 Claude 模型:
| 模型 | 工具版本 | 测试版标志 |
|---|---|---|
| Claude Opus 4.5 | computer_20251124 | computer-use-2025-11-24 |
| Claude 4 模型 | computer_20250124 | computer-use-2025-01-24 |
| Claude Sonnet 3.7(已弃用) | computer_20250124 | computer-use-2025-01-24 |
Claude Opus 4.5 引入了 computer_20251124 工具版本,具有新功能,包括用于详细屏幕区域检查的缩放操作。Claude 4 模型使用针对新架构优化的更新工具版本。Claude Sonnet 3.7(已弃用)引入了额外功能,包括思考功能,以便更深入地了解模型的推理过程。
较旧的工具版本不保证与较新的模型向后兼容。始终使用与您的模型版本相对应的工具版本。
计算机使用是一项测试版功能,具有与标准 API 功能不同的独特风险。与互联网交互时,这些风险会增加。为了最小化风险,请考虑采取以下预防措施:
在某些情况下,Claude 会遵循内容中找到的命令,即使这与用户的指示相冲突。例如,网页上或图像中包含的 Claude 指示可能会覆盖指示或导致 Claude 犯错。我们建议采取预防措施将 Claude 与敏感数据和操作隔离,以避免与提示注入相关的风险。
我们已经训练了模型以抵抗这些提示注入,并添加了额外的防御层。如果您使用我们的计算机使用工具,我们将自动在您的提示上运行分类器,以标记潜在的提示注入实例。当这些分类器在屏幕截图中识别到潜在的提示注入时,它们会自动引导模型在继续下一步操作之前要求用户确认。我们认识到这种额外的保护不适合每个用例(例如,没有人在循环中的用例),因此如果您想选择退出并关闭它,请 联系我们。
我们仍然建议采取预防措施将 Claude 与敏感数据和操作隔离,以避免与提示注入相关的风险。
最后,请在您自己的产品中启用计算机使用之前,向最终用户告知相关风险并获得他们的同意。
通过我们的计算机使用参考实现快速入门,该实现包括 Web 界面、Docker 容器、示例工具实现和代理循环。
注意: 该实现已更新,包括 Claude 4 模型和 Claude Sonnet 3.7 的新工具。请确保拉取最新版本的存储库以访问这些新功能。
请使用 此表单 提供有关模型响应质量、API 本身或文档质量的反馈 - 我们迫不及待地想听到您的意见!
以下是如何开始使用计算机使用的方法:
仅计算机使用工具需要测试版标头。
上面的示例显示了所有三个工具一起使用,这需要测试版标头,因为它包括计算机使用工具。
1. 为 Claude 提供计算机使用工具和用户提示
2. Claude 决定使用计算机使用工具
stop_reason 为 tool_use,表示 Claude 的意图。3. 提取工具输入,在计算机上评估工具,并返回结果
tool_result 内容块的新 user 消息。4. Claude 继续调用计算机使用工具,直到完成任务
我们将第 3 步和第 4 步在没有用户输入的情况下的重复称为"代理循环"——即 Claude 响应工具使用请求,您的应用程序用评估该请求的结果响应 Claude。
计算机使用需要一个沙箱计算环境,Claude 可以在其中安全地与应用程序和网络交互。该环境包括:
虚拟显示:虚拟 X11 显示服务器(使用 Xvfb),呈现 Claude 将通过屏幕截图看到并用鼠标/键盘操作控制的桌面界面。
桌面环境:在 Linux 上运行的轻量级 UI,带有窗口管理器(Mutter)和面板(Tint2),为 Claude 提供一致的图形界面以进行交互。
应用程序:预装的 Linux 应用程序,如 Firefox、LibreOffice、文本编辑器和文件管理器,Claude 可以使用这些应用程序来完成任务。
工具实现:集成代码,将 Claude 的抽象工具请求(如"移动鼠标"或"截图")转换为虚拟环境中的实际操作。
代理循环:处理 Claude 和环境之间通信的程序,将 Claude 的操作发送到环境,并将结果(屏幕截图、命令输出)返回给 Claude。
当您使用计算机使用时,Claude 不会直接连接到此环境。相反,您的应用程序:
为了安全和隔离,参考实现在 Docker 容器内运行所有这些,具有适当的端口映射,用于查看和与环境交互。
我们已经构建了一个 参考实现,其中包含快速开始使用计算机使用所需的一切:
计算机使用的核心是"代理循环"——一个循环,其中 Claude 请求工具操作,您的应用程序执行它们,并将结果返回给 Claude。以下是一个简化的示例:
async def sampling_loop(
*,
model: str,
messages: list[dict],
api_key: str,
max_tokens: int = 4096,
tool_version: str,
thinking_budget: int | None = None,
max_iterations: int = 10, # 添加迭代限制以防止无限循环
):
"""
Claude 计算机使用交互的简单代理循环。
此函数处理以下之间的来回:
1. 向 Claude 发送用户消息
2. Claude 请求使用工具
3. 您的应用程序执行这些工具
4. 将工具结果发送回 Claude
"""
# 设置工具和 API 参数
client = Anthropic(api_key=api_key)
beta_flag = "computer-use-2025-01-24" if "20250124" in tool_version else "computer-use-2024-10-22"
# 配置工具 - 您应该已在其他地方初始化这些工具
tools = [
{"type": f"computer_{tool_version}", "name": "computer", "display_width_px": 1024, "display_height_px": 768},
{"type": f"text_editor_{tool_version}", "name": "str_replace_editor"},
{"type": f"bash_{tool_version}", "name": "bash"}
]
# 主代理循环(具有迭代限制以防止 API 成本失控)
iterations = 0
while True and iterations < max_iterations:
iterations += 1
# 设置可选的思考参数(对于 Claude Sonnet 3.7)
thinking = None
if thinking_budget:
thinking = {"type": "enabled", "budget_tokens": thinking_budget}
# 调用 Claude API
response = client.beta.messages.create(
model=model,
max_tokens=max_tokens,
messages=messages,
tools=tools,
betas=[beta_flag],
thinking=thinking
)
# 将 Claude 的响应添加到对话历史记录
response_content = response.content
messages.append({"role": "assistant", "content": response_content})
# 检查 Claude 是否使用了任何工具
tool_results = []
for block in response_content:
if block.type == "tool_use":
# 在真实应用中,您会在此处执行工具
# 例如:result = run_tool(block.name, block.input)
result = {"result": "Tool executed successfully"}
# 为 Claude 格式化结果
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
# 如果没有使用工具,Claude 已完成 - 返回最终消息
if not tool_results:
return messages
# 将工具结果添加到消息中,以供下一次 Claude 迭代使用
messages.append({"role": "user", "content": tool_results})循环继续,直到 Claude 响应而不请求任何工具(任务完成)或达到最大迭代限制。此防护措施可防止可能导致意外 API 成本的潜在无限循环。
使用计算机使用工具时,您必须为您的模型版本包含适当的测试版标志:
我们建议在阅读本文档的其余部分之前尝试参考实现。
以下是一些关于如何获得最佳质量输出的提示:
After each step, take a screenshot and carefully evaluate if you have achieved the right outcome. Explicitly show your thinking: "I have evaluated step X..." If not correct, try again. Only when you confirm a step was executed correctly should you move on to the next one.<robot_credentials>)为其提供用户名和密码。在需要登录的应用程序中使用计算机使用会增加由于提示注入而导致不良结果的风险。在向模型提供登录凭据之前,请查看我们的 关于缓解提示注入的指南。如果您反复遇到一组明确的问题,或者提前知道 Claude 需要完成的任务,请使用系统提示为 Claude 提供明确的提示或说明,说明如何成功完成任务。
当通过 Claude API 请求 Anthropic 定义的工具之一时,会生成计算机使用特定的系统提示。它类似于 工具使用系统提示,但以以下内容开头:
You have access to a set of functions you can use to answer the user's question. This includes access to a sandboxed computing environment. You do NOT currently have the ability to inspect files or interact with external resources, except by invoking the below functions.
与常规工具使用一样,用户提供的 system_prompt 字段仍然受到尊重,并在组合系统提示的构造中使用。
我们已经构建了一个参考实现,其中包含了快速开始使用计算机使用所需的一切:
计算机使用的核心是"代理循环" - 一个循环过程,其中Claude请求工具操作,您的应用程序执行这些操作,并将结果返回给Claude。这是一个简化的示例:
async def sampling_loop(
*,
model: str,
messages: list[dict],
api_key: str,
max_tokens: int = 4096,
tool_version: str,
thinking_budget: int | None = None,
max_iterations: int = 10, # Add iteration limit to prevent infinite loops
):
"""
A simple agent loop for Claude computer use interactions.
This function handles the back-and-forth between:
1. Sending user messages to Claude
2. Claude requesting to use tools
3. Your app executing those tools
4. Sending tool results back to Claude
"""
# Set up tools and API parameters
client = Anthropic(api_key=api_key)
beta_flag = "computer-use-2025-01-24" if "20250124" in tool_version else "computer-use-2024-10-22"
# Configure tools - you should already have these initialized elsewhere
tools = [
{"type": f"computer_{tool_version}", "name": "computer", "display_width_px": 1024, "display_height_px": 768},
{"type": f"text_editor_{tool_version}", "name": "str_replace_editor"},
{"type": f"bash_{tool_version}", "name": "bash"}
]
# Main agent loop (with iteration limit to prevent runaway API costs)
iterations = 0
while True and iterations < max_iterations:
iterations += 1
# Set up optional thinking parameter (for Claude Sonnet 3.7)
thinking = None
if thinking_budget:
thinking = {"type": "enabled", "budget_tokens": thinking_budget}
# Call the Claude API
response = client.beta.messages.create(
model=model,
max_tokens=max_tokens,
messages=messages,
tools=tools,
betas=[beta_flag],
thinking=thinking
)
# Add Claude's response to the conversation history
response_content = response.content
messages.append({"role": "assistant", "content": response_content})
# Check if Claude used any tools
tool_results = []
for block in response_content:
if block.type == "tool_use":
# In a real app, you would execute the tool here
# For example: result = run_tool(block.name, block.input)
result = {"result": "Tool executed successfully"}
# Format the result for Claude
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
# If no tools were used, Claude is done - return the final messages
if not tool_results:
return messages
# Add tool results to messages for the next iteration with Claude
messages.append({"role": "user", "content": tool_results})循环会一直继续,直到Claude在不请求任何工具的情况下响应(任务完成)或达到最大迭代限制。这个保障措施可以防止可能导致意外API成本的无限循环。
使用计算机使用工具时,您必须为您的模型版本包含适当的beta标志:
我们建议在阅读本文档的其余部分之前先尝试参考实现。
以下是一些关于如何获得最佳质量输出的提示:
After each step, take a screenshot and carefully evaluate if you have achieved the right outcome. Explicitly show your thinking: "I have evaluated step X..." If not correct, try again. Only when you confirm a step was executed correctly should you move on to the next one.<robot_credentials>)向其提供用户名和密码。在需要登录的应用程序中使用计算机使用会增加由于提示注入而导致不良结果的风险。在向模型提供登录凭证之前,请查看我们的关于缓解提示注入的指南。如果您反复遇到一组明确的问题,或者事先知道Claude需要完成的任务,请使用系统提示为Claude提供明确的提示或说明,说明如何成功完成任务。
当通过Claude API请求Anthropic定义的工具之一时,会生成一个计算机使用特定的系统提示。它类似于工具使用系统提示,但以以下内容开头:
You have access to a set of functions you can use to answer the user's question. This includes access to a sandboxed computing environment. You do NOT currently have the ability to inspect files or interact with external resources, except by invoking the below functions.
与常规工具使用一样,用户提供的system_prompt字段仍然被尊重并用于构建组合系统提示。
计算机使用工具支持这些操作:
基本操作(所有版本)
[x, y]处单击增强操作(computer_20250124)
在Claude 4模型和Claude Sonnet 3.7中可用:
增强操作(computer_20251124)
在Claude Opus 4.5中可用:
computer_20250124中的所有操作enable_zoom: true。采用region参数,其坐标为[x1, y1, x2, y2],定义要检查的区域的左上角和右下角。| 参数 | 必需 | 描述 |
|---|---|---|
type | 是 | 工具版本(computer_20251124、computer_20250124或computer_20241022) |
name | 是 | 必须为"computer" |
display_width_px | 是 | 显示宽度(像素) |
display_height_px | 是 | 显示高度(像素) |
display_number | 否 | X11环境的显示编号 |
enable_zoom |
将显示分辨率保持在1280x800(WXGA)或以下以获得最佳性能。较高的分辨率可能会由于图像调整大小而导致准确性问题。
重要:计算机使用工具必须由您的应用程序明确执行 - Claude无法直接执行它。您负责根据Claude的请求实现屏幕截图捕获、鼠标移动、键盘输入和其他操作。
| 参数 | 必需 | 描述 |
|---|---|---|
type | 是 | 工具版本(computer_20251124、computer_20250124或computer_20241022) |
name | 是 | 必须为"computer" |
display_width_px | 是 | 显示宽度(像素) |
display_height_px | 是 | 显示高度(像素) |
display_number | 否 | X11环境的显示编号 |
enable_zoom |
将显示分辨率保持在1280x800(WXGA)或以下以获得最佳性能。较高的分辨率可能会由于图像调整大小而导致准确性问题。
重要:计算机使用工具必须由您的应用程序明确执行 - Claude无法直接执行它。您负责根据Claude的请求实现屏幕截图捕获、鼠标移动、键盘输入和其他操作。
Claude Sonnet 3.7引入了一种新的"思考"能力,允许您在模型处理复杂任务时看到其推理过程。此功能可以帮助您了解Claude如何处理问题,对于调试或教育目的特别有价值。
要启用思考,请将thinking参数添加到您的API请求中:
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}budget_tokens参数指定Claude可以用于思考的令牌数。这从您的总体max_tokens预算中扣除。
启用思考后,Claude将返回其推理过程作为响应的一部分,这可以帮助您:
以下是思考输出可能看起来像什么的示例:
[Thinking]
I need to save a picture of a cat to the desktop. Let me break this down into steps:
1. First, I'll take a screenshot to see what's on the desktop
2. Then I'll look for a web browser to search for cat images
3. After finding a suitable image, I'll need to save it to the desktop
Let me start by taking a screenshot to see what's available...| 参数 | 必需 | 描述 |
|---|---|---|
type | 是 | 工具版本(computer_20251124、computer_20250124或computer_20241022) |
name | 是 | 必须为"computer" |
display_width_px | 是 | 显示宽度(像素) |
display_height_px | 是 | 显示高度(像素) |
display_number | 否 | X11环境的显示编号 |
enable_zoom |
将显示分辨率保持在1280x800(WXGA)或以下以获得最佳性能。较高的分辨率可能会由于图像调整大小而导致准确性问题。
重要:计算机使用工具必须由您的应用程序明确执行 - Claude无法直接执行它。您负责根据Claude的请求实现屏幕截图捕获、鼠标移动、键盘输入和其他操作。
Claude Sonnet 3.7引入了一种新的"思考"能力,允许您在模型处理复杂任务时看到其推理过程。此功能可以帮助您了解Claude如何处理问题,对于调试或教育目的特别有价值。
要启用思考,请将thinking参数添加到您的API请求中:
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}budget_tokens参数指定Claude可以用于思考的令牌数。这从您的总体max_tokens预算中扣除。
启用思考后,Claude将返回其推理过程作为响应的一部分,这可以帮助您:
以下是思考输出可能看起来像什么的示例:
[Thinking]
I need to save a picture of a cat to the desktop. Let me break this down into steps:
1. First, I'll take a screenshot to see what's on the desktop
2. Then I'll look for a web browser to search for cat images
3. After finding a suitable image, I'll need to save it to the desktop
Let me start by taking a screenshot to see what's available...计算机使用工具可以与其他工具结合,以创建更强大的自动化工作流程。当您需要以下操作时,这特别有用:
| 参数 | 必需 | 描述 |
|---|---|---|
type | 是 | 工具版本(computer_20251124、computer_20250124 或 computer_20241022) |
name | 是 | 必须为"computer" |
display_width_px | 是 | 显示宽度(像素) |
display_height_px | 是 | 显示高度(像素) |
display_number | 否 | X11 环境的显示编号 |
enable_zoom |
为获得最佳性能,请将显示分辨率保持在 1280x800 (WXGA) 或以下。较高的分辨率可能会因为图像调整大小而导致准确性问题。
重要:计算机使用工具必须由您的应用程序显式执行 - Claude 无法直接执行它。您负责根据 Claude 的请求实现屏幕截图捕获、鼠标移动、键盘输入和其他操作。
Claude Sonnet 3.7 引入了一项新的"思考"能力,允许您在模型处理复杂任务时查看其推理过程。此功能可帮助您了解 Claude 如何处理问题,对于调试或教育目的特别有价值。
要启用思考,请在 API 请求中添加 thinking 参数:
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}budget_tokens 参数指定 Claude 可用于思考的令牌数。这从您的总体 max_tokens 预算中扣除。
启用思考后,Claude 将返回其推理过程作为响应的一部分,这可以帮助您:
以下是思考输出可能看起来的示例:
[Thinking]
I need to save a picture of a cat to the desktop. Let me break this down into steps:
1. First, I'll take a screenshot to see what's on the desktop
2. Then I'll look for a web browser to search for cat images
3. After finding a suitable image, I'll need to save it to the desktop
Let me start by taking a screenshot to see what's available...计算机使用工具可以与其他工具结合使用,以创建更强大的自动化工作流。当您需要以下操作时,这特别有用:
参考实现旨在帮助您开始使用计算机使用。它包含 Claude 使用计算机所需的所有组件。但是,您可以构建自己的计算机使用环境以满足您的需求。您需要:
tool_use 结果的代理循环计算机使用工具实现为无模式工具。使用此工具时,您不需要像其他工具那样提供输入模式;该模式内置于 Claude 的模型中,无法修改。
实现计算机使用工具时,可能会发生各种错误。以下是处理方法:
计算机使用功能处于测试阶段。虽然 Claude 的功能处于最前沿,但开发人员应该了解其限制:
left_mouse_down、left_mouse_up)和新的修饰键支持,改进了电子表格交互的鼠标点击。通过使用这些细粒度控制并将修饰键与点击结合,单元格选择可以更可靠。始终仔细审查和验证 Claude 的计算机使用操作和日志。不要将 Claude 用于需要完美精度或敏感用户信息的任务,除非有人工监督。
计算机使用功能处于测试阶段。虽然 Claude 的功能处于最前沿,但开发人员应该了解其限制:
left_mouse_down、left_mouse_up)和新的修饰键支持,改进了电子表格交互的鼠标点击。通过使用这些细粒度控制并将修饰键与点击结合,单元格选择可以更可靠。始终仔细审查和验证 Claude 的计算机使用操作和日志。不要将 Claude 用于需要完美精度或敏感用户信息的任务,除非有人工监督。
Computer use follows the standard tool use pricing. When using the computer use tool:
System prompt overhead: The computer use beta adds 466-499 tokens to the system prompt
Computer use tool token usage:
| Model | Input tokens per tool definition |
|---|---|
| Claude 4.x models | 735 tokens |
| Claude Sonnet 3.7 (deprecated) | 735 tokens |
Additional token consumption:
If you're also using bash or text editor tools alongside computer use, those tools have their own token costs as documented in their respective pages.
计算机使用功能处于测试阶段。虽然 Claude 的功能处于最前沿,但开发人员应该了解其限制:
left_mouse_down、left_mouse_up)和新的修饰键支持,改进了电子表格交互的鼠标点击。通过使用这些细粒度控制并将修饰键与点击结合,单元格选择可以更可靠。始终仔细审查和验证 Claude 的计算机使用操作和日志。不要将 Claude 用于需要完美精度或敏感用户信息的任务,除非有人工监督。
Computer use follows the standard tool use pricing. When using the computer use tool:
System prompt overhead: The computer use beta adds 466-499 tokens to the system prompt
Computer use tool token usage:
| Model | Input tokens per tool definition |
|---|---|
| Claude 4.x models | 735 tokens |
| Claude Sonnet 3.7 (deprecated) | 735 tokens |
Additional token consumption:
If you're also using bash or text editor tools alongside computer use, those tools have their own token costs as documented in their respective pages.
import anthropic
client = anthropic.Anthropic()
response = client.beta.messages.create(
model="claude-sonnet-4-5", # 或其他兼容模型
max_tokens=1024,
tools=[
{
"type": "computer_20250124",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768,
"display_number": 1,
},
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
},
{
"type": "bash_20250124",
"name": "bash"
}
],
messages=[{"role": "user", "content": "Save a picture of a cat to my desktop."}],
betas=["computer-use-2025-01-24"]
)
print(response)tool_use stop_reason 响应,您应该返回第 3 步。import anthropic
client = anthropic.Anthropic()
response = client.beta.messages.create(
model="claude-sonnet-4-5", # 或其他兼容模型
max_tokens=1024,
tools=[
{
"type": "computer_20250124",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768,
"display_number": 1,
},
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
},
{
"type": "bash_20250124",
"name": "bash"
}
],
messages=[{"role": "user", "content": "Save a picture of a cat to my desktop."}],
betas=["computer-use-2025-01-24"]
)
print(response)tool_use stop_reason 响应,您应该返回第 3 步。| 否 |
启用缩放操作(仅computer_20251124)。设置为true以允许Claude缩放到特定屏幕区域。默认值:false |
| 否 |
启用缩放操作(仅computer_20251124)。设置为true以允许Claude缩放到特定屏幕区域。默认值:false |
| 否 |
启用缩放操作(仅computer_20251124)。设置为true以允许Claude缩放到特定屏幕区域。默认值:false |
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: computer-use-2025-01-24" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 2000,
"tools": [
{
"type": "computer_20250124",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768,
"display_number": 1
},
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
},
{
"type": "bash_20250124",
"name": "bash"
},
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'"
}
},
"required": ["location"]
}
}
],
"messages": [
{
"role": "user",
"content": "Find flights from San Francisco to a place with warmer weather."
}
],
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}
}'| 否 |
启用缩放操作(仅限 computer_20251124)。设置为 true 以允许 Claude 缩放到特定屏幕区域。默认值:false |
curl https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: computer-use-2025-01-24" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 2000,
"tools": [
{
"type": "computer_20250124",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768,
"display_number": 1
},
{
"type": "text_editor_20250728",
"name": "str_replace_based_edit_tool"
},
{
"type": "bash_20250124",
"name": "bash"
},
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'"
}
},
"required": ["location"]
}
}
],
"messages": [
{
"role": "user",
"content": "Find flights from San Francisco to a place with warmer weather."
}
],
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}
}'设置您的计算环境
创建虚拟显示或连接到 Claude 将与之交互的现有显示。这通常涉及设置 Xvfb(X 虚拟帧缓冲区)或类似技术。
实现操作处理程序
创建函数以处理 Claude 可能请求的每种操作类型:
def handle_computer_action(action_type, params):
if action_type == "screenshot":
return capture_screenshot()
elif action_type == "left_click":
x, y = params["coordinate"]
return click_at(x, y)
elif action_type == "type":
return type_text(params["text"])
# ... handle other actions处理 Claude 的工具调用
从 Claude 的响应中提取并执行工具调用:
for content in response.content:
if content.type == "tool_use":
action = content.input["action"]
result = handle_computer_action(action, content.input)
# Return result to Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result
}实现代理循环
创建一个循环,直到 Claude 完成任务:
while True:
response = client.beta.messages.create(...)
# Check if Claude used any tools
tool_results = process_tool_calls(response)
if not tool_results:
# No more tool use, task complete
break
# Continue conversation with tool results
messages.append({"role": "user", "content": tool_results})了解更多关于工具使用和创建自定义工具的信息