2025年,AI 领域出现了一个关键转折:AI 不仅能回答问题,还能像人一样操作电脑了。
Anthropic 在 2024 年底推出的 Claude Computer Use,以及 OpenAI 的 Operator,标志着 AI 从"对话者"向"执行者"的进化。这篇文章带你深入理解 Computer Use 技术的工作原理、当前能力边界,以及它将如何改变我们的工作方式。
剧透:本文包含完整的 Claude Computer Use API 使用教程和代码示例,可以直接上手实践。
从工具调用到操控电脑
在 Computer Use 出现之前,AI 使用工具的方式是 Function Calling:模型收到用户请求后,按照预定义的格式返回函数名和参数,由外部代码执行后再把结果返回给模型。
# 传统的 Function Calling 方式
response = client.chat.completions.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": "帮我查下北京的天气"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}]
)
# 模型返回的是预定义的函数调用格式
# tool_calls: [{"id": "call_xxx", "function": {"name": "get_weather", "arguments": "{\"city\": \"北京\"}"}}]这种方式有几个明显的局限:
- 必须预先定义 — 每个工具都要开发者手动写好接口
- 无法适应未知界面 — 遇到没见过的 UI 就抓瞎
- 缺乏视觉理解 — 不知道操作后界面变成了什么样
Computer Use 换了一种思路:让 AI 直接看屏幕,自己决定下一步该做什么。
技术原理:AI 是怎么做事的
Claude Computer Use 的工作流程可以概括为一个循环:
1. 截图 → 2. 分析界面 → 3. 决定动作 → 4. 执行 → 5. 重复第一步:截图
API 启用 Computer Use 后,Claude 会持续收到当前界面的截图。这些截图被编码成图像 tokens 输入到模型的上下文窗口中。
关键在于:模型看到的不只是静态图片,而是包含完整 UI 信息的学习样本。Anthropic 投入了大量工程资源训练模型理解按钮、输入框、菜单等常见 UI 元素的空间位置。
第二步:动作空间
模型不是随意猜测下一步操作,而是从一个受限的动作空间中选择:
| 动作 | 描述 | 示例参数 |
|---|---|---|
mouse_move | 移动鼠标到指定坐标 | {"x": 500, "y": 300} |
left_click | 左键点击 | {"x": 500, "y": 300} |
right_click | 右键点击 | {"x": 500, "y": 300} |
type | 键盘输入 | {"text": "hello world"} |
press_key | 组合键 | {"keys": ["Control", "c"]} |
scroll | 滚动页面 | {"direction": "down", "amount": 500} |
screenshot | 截取当前画面 | 无参数 |
这个设计很聪明:既给了 AI 足够的自由度完成复杂任务,又把风险控制在可预测的范围内。
第三步:自我纠错
这是 Computer Use 最惊艳的部分。当 AI 点击了错误的按钮,或者输入框里出现了预期外的提示时,模型会:
- 分析新的截图
- 理解刚才的行动为什么没有达到预期
- 调整策略,重试
这个反馈循环模仿了人类解决问题的方式——不是按照预设脚本执行,而是边做边学。
上手指南:如何使用 Claude Computer Use
准备工作
- 获取 API Key:访问 Anthropic Console,创建 API Key
- 安装依赖:
pip install anthropic基础调用示例
以下是使用 Claude Computer Use API 的完整示例:
import anthropic
from anthropic import Anthropic
import os
# 初始化客户端
client = Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY")
)
# 定义可用的工具(计算机工具)
tools = [
{
"name": "computer",
"description": "Use a computer to interact with applications and perform actions.",
"input_schema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": [
"screenshot",
"mouse_move",
"left_click",
"right_click",
"type",
"press_key",
"scroll"
],
"description": "The action to perform"
},
"coordinate": {
"type": "array",
"items": {"type": "integer"},
"description": "X, Y coordinates for mouse actions"
},
"text": {
"type": "string",
"description": "Text to type or key combination to press"
},
"direction": {
"type": "string",
"enum": ["up", "down"],
"description": "Scroll direction"
}
},
"required": ["action"]
}
}
]
# 启动对话
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
tools=tools,
messages=[
{
"role": "user",
"content": "打开浏览器,搜索 'AI Agent',然后截个图"
}
]
)
# 处理工具调用
for block in message.content:
if block.type == "tool_use":
print(f"需要执行动作: {block.name}")
print(f"参数: {block.input}")
# 这里需要实现实际的计算机操作完整的计算机操作实现
下面是实现 computer 工具的实际代码:
import pyautogui
import time
from PIL import Image
import io
import base64
class ComputerTool:
"""计算机工具的实际实现"""
def __init__(self):
# 设置 pyautogui 的安全参数
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5
def screenshot(self):
"""截取屏幕"""
img = pyautogui.screenshot()
# 转换为 base64
buffered = io.BytesIO()
img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode()
def mouse_move(self, x, y):
"""移动鼠标"""
pyautogui.moveTo(x, y)
return f"鼠标移动到 ({x}, {y})"
def left_click(self, x=None, y=None):
"""左键点击"""
if x is not None and y is not None:
pyautogui.click(x, y)
return f"点击坐标 ({x}, {y})"
pyautogui.click()
return "点击当前位置"
def type_text(self, text):
"""输入文本"""
pyautogui.write(text)
return f"输入文本: {text}"
def press_key(self, key):
"""按键"""
pyautogui.press(key)
return f"按下按键: {key}"
def scroll(self, direction, amount=500):
"""滚动"""
if direction == "down":
pyautogui.scroll(-amount)
else:
pyautogui.scroll(amount)
return f"滚动 {direction} {amount} 像素"
def execute_tool(tool_name, tool_input):
"""执行工具并返回结果"""
computer = ComputerTool()
action = tool_input.get("action")
if action == "screenshot":
return {"type": "image", "source": {"type": "base64", "data": computer.screenshot()}}
elif action == "mouse_move":
x, y = tool_input["coordinate"]
return {"type": "text", "text": computer.mouse_move(x, y)}
elif action == "left_click":
coords = tool_input.get("coordinate")
x, y = coords[0], coords[1] if coords else (None, None)
return {"type": "text", "text": computer.left_click(x, y)}
elif action == "type":
text = tool_input.get("text", "")
return {"type": "text", "text": computer.type_text(text)}
elif action == "press_key":
key = tool_input.get("text", "")
return {"type": "text", "text": computer.press_key(key)}
elif action == "scroll":
direction = tool_input.get("direction", "down")
amount = tool_input.get("amount", 500)
return {"type": "text", "text": computer.scroll(direction, amount)}
return {"type": "text", "text": "Unknown action"}完整的 Agent 循环
下面是整合在一起的完整 Agent 实现:
import anthropic
import os
import json
class ComputerUseAgent:
"""完整的 Claude Computer Use Agent"""
def __init__(self, api_key: str):
self.client = anthropic.Anthropic(api_key=api_key)
self.computer = ComputerTool()
self.tools = [
{
"name": "computer",
"description": "Use a computer to interact with applications, browse the web, and perform actions.",
"input_schema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["screenshot", "mouse_move", "left_click", "right_click", "type", "press_key", "scroll"]
},
"coordinate": {"type": "array", "items": {"type": "integer"}},
"text": {"type": "string"},
"direction": {"type": "string", "enum": ["up", "down"]},
"amount": {"type": "integer"}
},
"required": ["action"]
}
}
]
def run(self, task: str, max_iterations: int = 30):
"""运行 Agent 执行任务"""
messages = [{"role": "user", "content": task}]
for i in range(max_iterations):
# 调用 API
response = self.client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4096,
tools=self.tools,
messages=messages
)
# 处理响应
has_tool_use = False
for block in response.content:
if block.type == "text":
messages.append({"role": "assistant", "content": block.text})
print(f"🤖: {block.text}")
elif block.type == "tool_use":
has_tool_use = True
tool_name = block.name
tool_input = block.input
print(f"\n🔧 执行工具: {tool_name}")
print(f" 参数: {tool_input}")
# 执行工具
result = self.execute_tool(tool_name, tool_input)
# 将结果添加回消息
messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": block.id,
"result": result
}]
})
# 如果没有工具调用,说明任务完成
if not has_tool_use:
print("\n✅ 任务完成")
break
else:
print("\n⚠️ 达到最大迭代次数")
def execute_tool(self, tool_name, tool_input):
"""执行单个工具"""
action = tool_input.get("action")
if action == "screenshot":
screenshot_data = self.computer.screenshot()
return {"type": "image", "source": {"type": "base64", "data": screenshot_data}}
elif action == "mouse_move":
x, y = tool_input["coordinate"]
return {"type": "text", "text": self.computer.mouse_move(x, y)}
elif action == "left_click":
coords = tool_input.get("coordinate")
if coords:
x, y = coords[0], coords[1]
return {"type": "text", "text": self.computer.left_click(x, y)}
return {"type": "text", "text": self.computer.left_click()}
elif action == "right_click":
coords = tool_input.get("coordinate")
if coords:
x, y = coords[0], coords[1]
return {"type": "text", "text": self.computer.right_click(x, y)}
return {"type": "text", "text": self.computer.right_click()}
elif action == "type":
text = tool_input.get("text", "")
return {"type": "text", "text": self.computer.type_text(text)}
elif action == "press_key":
key = tool_input.get("text", "")
return {"type": "text", "text": self.computer.press_key(key)}
elif action == "scroll":
direction = tool_input.get("direction", "down")
amount = tool_input.get("amount", 500)
return {"type": "text", "text": self.computer.scroll(direction, amount)}
return {"type": "text", "text": "Unknown action"}
# 使用示例
if __name__ == "__main__":
agent = ComputerUseAgent(api_key=os.environ.get("ANTHROPIC_API_KEY"))
agent.run("打开浏览器,访问 github.com,然后截个图")使用 Claude Code 体验 Computer Use
如果你不想自己实现上述代码,最简单的方式是使用 Claude Code。
安装 Claude Code
# macOS
brew install claude-code
# 或者直接下载
curl -s https://claude.com/code/install.sh | sh配置 Computer Use
# 启用 Computer Use 模式
claude config set computer-use true
# 或者通过环境变量
export CLAUDE_COMPUTER_USE=true常用命令
# 启动交互式会话
claude
# 执行特定任务
claude "修复 GitHub 上的这个 issue"
# 指定项目目录
claude --dir /path/to/project "分析这个代码库"
# 启用调试模式
claude --debug "帮我重构这段代码"实际使用案例
以下是一些常用的 Claude Code 使用场景:
1. 自动修复 Bug
claude "这个测试失败了,请分析原因并修复"2. 重构代码
claude "将这个 JavaScript 文件重构为 TypeScript"
# Claude 会:
# 1. 分析现有代码结构
# 2. 创建 .ts 版本
# 3. 添加类型定义
# 4. 运行测试确保功能正常3. 自动化测试
claude "为这个函数编写单元测试"
# Claude 会:
# 1. 分析函数逻辑
# 2. 识别边界条件
# 3. 生成测试用例
# 4. 运行测试验证实际能力边界
根据 Anthropic 官方文档和社区测试,Claude Computer Use 目前擅长:
| 场景 | 表现 | 适用任务 |
|---|---|---|
| 浏览器自动化 | ⭐⭐⭐⭐⭐ | 搜索、填表、导航 |
| 文件操作 | ⭐⭐⭐⭐⭐ | 创建、编辑、整理文件 |
| 桌面应用 | ⭐⭐⭐ | 取决于应用复杂度 |
| 复杂多步骤任务 | ⭐⭐⭐ | 需要明确的目标分解 |
| 精确坐标操作 | ⭐⭐ | 依赖屏幕分辨率和 UI 稳定性 |
最成功的用例集中在结构化、重复性的任务上,比如:
- 自动填写表单
- 批量处理文件
- 定时抓取网页数据
- 自动化测试流程
- 批量重命名文件
- 生成报表
容易失败的场景:
- 需要登录验证码的网站
- 复杂的图形验证码
- 非标准的 UI 框架
- 需要多账户切换的操作
安全考量
让 AI 操控电脑当然有安全风险。Anthropic 做了几层防护:
- 沙箱环境 — 官方推荐在隔离的虚拟机中运行
- 人类确认 — 敏感操作可以配置为需要人工批准
- 操作审计 — 所有动作都有日志可追溯
- 权限最小化 — 只给必要的系统权限
企业部署建议
# 安全配置示例
agent = ComputerUseAgent(
api_key=api_key,
# 限制操作权限
allowed_domains=["github.com", "docs.python.org"],
# 启用人工确认
require_confirmation_for=["delete", "send_email", "payment"],
# 设置操作预算
max_iterations=50,
# 启用审计日志
audit_log=True
)企业使用时,建议:
- 隔离网络访问(使用企业 VPN/代理)
- 不在生产环境直接运行
- 设置操作预算(最大步数限制)
- 重要操作前人工复核
- 定期审计操作日志
与 OpenAI Operator 的对比
| Claude Computer Use | OpenAI Operator | |
|---|---|---|
| 发布方 | Anthropic | OpenAI |
| 技术路线 | 纯视觉 + 动作空间 | 视觉 + CUA 模型 |
| 可用性 | API (需要 API Key) | ChatGPT Pro 订阅 |
| 可定制性 | 高 (可自定义工具) | 低 (封闭系统) |
| 开源生态 | 有社区实现的替代方案 | 封闭 |
| 响应速度 | 较快 | 较快 |
| 成本 | 按 token 收费 | 订阅制 |
两者底层思路相似,都是"看屏幕 + 动鼠标"的循环,但产品化和安全策略上各有取舍。
如何选择:
- 需要深度定制 → 选择 Claude Computer Use
- 想要开箱即用 → 选择 OpenAI Operator
- 需要本地部署 → 选择 Claude + 自建基础设施
未来展望
Computer Use 正在快速迭代。几个值得关注的趋势:
1. 多模态理解增强
未来的模型会更好地理解模糊的 UI 元素,包括:
- 阴影和模糊效果
- 动态加载的内容
- 非标准控件
2. 长期任务记忆
当前的循环在长流程中会丢失上下文,这个问题正在被解决:
- 引入外部记忆存储
- 支持任务分解和规划
- 更长的上下文窗口
3. 跨平台原生支持
不再依赖截图,而是直接调用系统 API:
- Windows: Direct API
- macOS: Accessibility API
- Linux: AT-SPI
4. 安全标准化
企业级安全框架会逐渐成形:
- SOC 2 合规
- 权限分级标准
- 操作审计规范
5. 市场预测
Gartner 预测,到 2027 年,Agentic AI 将驱动 580 亿美元的软件市场重构。Computer Use 是这场变革的核心技术之一。
麦肯锡估计,AI Agent 每年能为全球经济带来 2.6-4.4 万亿美元的生产力提升。
结语
Computer Use 代表的不仅是一项技术,更是人机交互范式的转变:从"人指挥机器"到"人告诉机器目标,机器自己完成"。
当然,现在的 AI 电脑 agent 还远非完美——它们会犯错,需要人类监督,距离真正的"自主员工"还有一段路。但当你看到 AI 能自己打开浏览器、搜索信息、填好表格、点击提交的时候,那个未来已经不再是科幻了。
立即尝试:
- 申请 Anthropic API
- 运行上面的代码示例
- 给 Agent 一个简单的任务开始体验
如果你有任何问题或成功案例,欢迎在评论区分享!
本文由 OpenClaw 自动生成并推送至 GitHub。
